我尝试在 Node 应用程序中使用 s3.copyObject() 来修改文件的内容处置元数据。我用它来存储用户浏览器应该命名的文件名。它给了我一个 NoSuchKey 错误。但是,当我检查存储桶的仪表板时,我可以看到它们的 key 确实存在。此外,我能够使用相同的 key 来上传、下载和删除文件。所以,我知道 key 是正确的。我猜测我缺少一个参数,导致它给我一条虚假的错误消息。
const aws = require('aws-sdk');
const config = require('../config.js');
...
static async updateFileInS3(strStoredFileName, strNewFileName){
const updateFileS3 = async (storedFileName, newFileName) => {
const bucketname = config.server.storageBucket;
const spacesEndpoint = new aws.Endpoint(config.server.storageEndpoint);
const s3 = new aws.S3({
endpoint: spacesEndpoint
});
const strKey = config.server.storageFolder + "/" + storedFileName;
const copyparams = {
Bucket : bucketname,
CopySource : bucketname + "/" + storedFileName,
Key : strKey,
ContentDisposition : 'attachment; filename=' + newFileName,
MetadataDirective : 'REPLACE'
};
await s3.copyObject(copyparams).promise();
}
try {
let awsUpdateResults = await updateFileS3(strStoredFileName, strNewFileName);
}
catch(err) {
console.error(`[BucketUtil]Error updating project file for ${strStoredFileName}: ${err}`);
}
}
预先感谢您的帮助。
最佳答案
如果您尝试通过将 s3 对象复制到自身来修改它,您需要:
const copyparams = {
Bucket : bucketname,
CopySource : bucketname + "/" + strKey, // The key should be the same.
Key : strKey,
ContentDisposition : 'attachment; filename=' + newFileName,
MetadataDirective : 'REPLACE'
};
另请注意,如果您通过签名网址下载,则可以包含 ResponseContentDisposition
参数来动态设置 ContentDisposition,而不是更新元数据。
关于javascript - AWS 上现有 key 出现 NoSuchKey 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57397948/