gpt4 book ai didi

amazon-web-services - 当 key 包含阿拉伯字符时,AWS Golang SDK 无法复制对象

转载 作者:IT王子 更新时间:2023-10-29 02:35:26 24 4
gpt4 key购买 nike

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

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

存储桶中的 key 如下所示:

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)
}

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

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

最佳答案

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

关于amazon-web-services - 当 key 包含阿拉伯字符时,AWS Golang SDK 无法复制对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55487019/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com