我想将文件从一个文件夹复制到同一个 S3 存储桶上的另一个文件夹。执行此操作时出现错误,NoSuchKey: The specified key does not exist我的代码如下所示。sess, err := session.NewSession(&aws.Config{Region: aws.String("ap-south-1")})if err != nil { return nil, err}Oldpath := "folder1/folder2/a+b.pdf"newBaseFolder := "folder3"svc := s3.New(sess)bucketName := "mybucket.test"source := bucketName + "/" + oldPath // Oldpath = "folder1/folder2/a+b.pdf" //newBaseFolder = "folder3"newPath := newBaseFolder + "/" + strings.SplitN(oldPath, "/", 2)[1] //newPath = "folder3/folder2/a+b.pdf" _, err = svc.CopyObject(&s3.CopyObjectInput{ Bucket: aws.String(bucketName), // bucketName = "mybucket.test" CopySource: aws.String(url.PathEscape(source)), Key: aws.String(newPath)})if err != nil { return nil, err }错误信息{"err": "NoSuchKey: The specified key does not exist.""status code": 404}
2 回答
海绵宝宝撒
TA贡献1809条经验 获得超8个赞
使用url.QueryEscape而不是 url.PathEscapeasurl.QueryEscape可以编码特殊字符,例如无法编码+的字符url.PathEscape(这种技术对我有用)。
...
_, err := svc.CopyObject(
&s3.CopyObjectInput{
Bucket: aws.String("document.as.a.service.test"),
CopySource: aws.String(url.QueryEscape(source)),
Key: aws.String(newPath),
},
)
...
有时如果copySource没有正确编码错误可以显示为NoSuchKey: The specified key does not exist
为避免混淆,Go-AWS-SDKcopyObject函数copySource将是现有文件的路径,并且Key 是您希望复制文件的新路径或目标。
- 2 回答
- 0 关注
- 1381 浏览
添加回答
举报
0/150
提交
取消