当密钥包含阿拉伯字符时,AWS Golang SDK无法复制对象

时间:2019-04-03 04:20:12

标签: amazon-web-services go amazon-s3 aws-sdk aws-sdk-go

使用aws-sdk-go,当键包含正常的字母数字和很少的特殊字符(如(-,_))时,我已经能够成功复制s3存储桶中的对象。但是,当键包含阿拉伯字符时,golang aws-sdk会引发错误。

NoSuchKey: The specified key does not exist.
    status code: 404, request id: 438DC6xxxxxx, host id: Xp+xxxxxxxxxx

存储桶中的密钥如下:

public/10009/img__١٣٤١١١-1600x1200.jpg

代码也很简单:

func copyObject(existingKey, key string, svc *s3.S3) {
    copyObjectInput := &s3.CopyObjectInput{
        Bucket:     aws.String("dummy-bucket"),
        CopySource: aws.String(existingKey),
        Key:        aws.String(key),
    }

    result, err := svc.CopyObject(copyObjectInput)
    if err != nil {
        log.Fatal("Copy failed due to: ", err) // logs the above error here
    }

    spew.Dump(result)
}

我还打印出了密钥,以防万一: dummy-bucket/public/10009/img__١٣٤١١١-1600x1200.jpg

我还能够使用aws-sdk-go使用相同的密钥成功下载图像。

1 个答案:

答案 0 :(得分:2)

根据文档,CopySource必须经过URL编码。

https://docs.aws.amazon.com/sdk-for-go/api/service/s3/#CopyObjectInput

// The name of the source bucket and key name of the source object, separated
// by a slash (/). Must be URL-encoded.
//
// CopySource is a required field
CopySource *string `location:"header" locationName:"x-amz-copy-source" type:"string" required:"true"`

尝试一下

import "net/url"

func copyObject(existingKey, key string, svc *s3.S3) {

    // existingKey is source bucket and key name separated by "/"
    e := url.QueryEscape(existingKey)

    copyObjectInput := &s3.CopyObjectInput{
        Bucket:     aws.String("dummy-bucket"),
        CopySource: aws.String(e),
        Key:        aws.String(key),
    }

    result, err := svc.CopyObject(copyObjectInput)
    if err != nil {
        log.Fatal("Copy failed due to: ", err) // logs the above error here
    }

    spew.Dump(result)
}