Reputation: 81
I'm attempting to use s3.copyObject() in my Node application to modify my file's content disposition metadata. I’m using it to store the filename my user’s browser should name the file. It's giving me a NoSuchKey error. But, when I check my bucket's dashboard, I can see that they key does exist. Furthermore, I was able to use the same key to upload, download and delete the file. So, I know the key is correct. I'm guessing that I am missing a parameter that is causing it to give me a bogus error message.
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}`);
}
}
Thanks in advance for your help.
Upvotes: 0
Views: 670
Reputation: 4606
If you are trying to modify the s3 object in place by copying it onto itself you need:
const copyparams = {
Bucket : bucketname,
CopySource : bucketname + "/" + strKey, // The key should be the same.
Key : strKey,
ContentDisposition : 'attachment; filename=' + newFileName,
MetadataDirective : 'REPLACE'
};
Also note that if you are downloading via signed urls you can include the ResponseContentDisposition
parameter to set the ContentDisposition dynamically instead of updating the metadata.
Upvotes: 1