从S3复制对象时出现错误,错误信息为”NoSuchKey: The specified key does not exist”。

huangapple go评论397阅读模式
英文:

Error while copying object from S3 Golang "NoSuchKey: The specified key does not exist"

问题

我想将一个文件从一个文件夹复制到同一个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
}
英文:

I want to copy a file from one folder to another on the same S3 bucket. while doing this I am getting an error NoSuchKey: The specified key does not exist my code is given below.

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
    }

Error message

{
"err": "NoSuchKey: The specified key does not exist."
"status code": 404
}

答案1

得分: 0

可能的原因是url.PathEscape将路径中的斜杠替换为%2F

英文:

Possible reason is that url.PathEscape replaces slashes in path with %2F

答案2

得分: 0

请使用url.QueryEscape代替url.PathEscape,因为url.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-SDK中的copyObject函数中,copySource应该是现有文件的路径,而Key要将文件复制到的新路径或目标路径

英文:

Use url.QueryEscape instead of url.PathEscape as url.QueryEscape can encode special characters such as + which cannot be encoded by url.PathEscape (this technique worked for me).

...
_, err := svc.CopyObject(
			&s3.CopyObjectInput{
				Bucket:     aws.String("document.as.a.service.test"),
				CopySource: aws.String(url.QueryEscape(source)),
				Key: aws.String(newPath),
			},
		)
...

And sometimes if copySource is not properly encoded error can be displayed as NoSuchKey: The specified key does not exist

Just in case to avoid confusion Go-AWS-SDK copyObject functions copySource will be the path of an existing file, and Key is the newPath or destination that you want your file to be copied.

huangapple
  • 本文由 发表于 2022年1月14日 17:29:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/70708479.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定