使用Node将文件从一个AWS S3存储桶复制到另一个存储桶
我正在尝试使用Node将文件从AWS S3存储桶复制到另一个存储桶.问题是文件名没有空格,例如:"abc.csv",它工作正常. 但是,如果要复制到的文件的文件名中包含空格,例如:"abc xyz.csv".它抛出以下错误.
I am trying to copy a file from a AWS S3 bucket to another bucket using Node. The problem is if the file name doesn't has the white space for example: "abc.csv", It is working fine. But in case the file to which I want to copy has the white space in the file name for example: "abc xyz.csv". It is throwing the below error.
指定的键不存在." "NoSuchKey:指定的键不存在. 在Request.extractError(d:\ Projects \ Other \ testproject \ s3filetoarchieve \ node_modules \ aws-sdk \ lib \ services \ s3.js:577:35)
"The specified key does not exist." "NoSuchKey: The specified key does not exist. at Request.extractError (d:\Projects\Other\testproject\s3filetoarchieve\node_modules\aws-sdk\lib\services\s3.js:577:35)
下面是提供的代码.
return Promise.each( files, file => {
var params = {
Bucket: process.env.CR_S3_BUCKET_NAME,
CopySource: `/${ process.env.CR_S3_BUCKET_NAME }/${ prefix }${ file.name}`,
Key: `${ archieveFolder }${ file.name }`
};
console.log(params);
return new Promise(( resolve, reject) => {
s3bucket.copyObject(params, function(err, data) {
if (err){
console.log(err, err.stack);
debugger
} else {
console.log(data);
debugger
}
});
});
}).then( result => {
debugger
});
尽早提供帮助.谢谢.
我认为问题出在文件名中的空格.
I think the problem is exactly that space in the filename.
S3密钥必须以url编码,因为它们必须以URL形式可访问.
有些软件包可以帮助您设置url格式,例如 speakingUrl
或者,您也可以尝试自己编写一些内容,如果想保持友好性,可以简单地用破折号(_ or -
)替换空格(\s
).
S3 keys must be url encoded, as they need to be accesible in URL form.
There are some packages that helps you with url formatting like speakingUrl
or you can try writting some on your own, maybe just simply replacing spaces (\s
) with dashes (_ or -
) if you want to keep it friendly.
如果您不介意的话,只需encodeURIComponent(file.name)
If you don't mind about that, you can simply encodeURIComponent(file.name)
希望有帮助!