英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论