0

使用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, 使用相同的密钥成功下载图像。

4

1 回答 1

4

根据文档,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)
}
于 2019-04-03T05:59:45.617 回答