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

AWS Golang SDK fails to copy object when the key contains arabic characters

使用 aws-sdk-go,当密钥包含普通字母数字和少数特殊字符(如 (-,_))时,我已经能够成功复制我的 s3 存储桶中的对象。但是当key包含阿拉伯字符时,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 成功下载图像。

根据文档,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)
}