Gilad Bison Gilad Bison - 16 days ago 7
Node.js Question

S3 set file name on upload

I'm uploading files to S3 using aws-sdk module. I use uuid to represent each file.

My question is - how can I set the real file name(which is not the uuid), so when I download the key from S3 later - the file that will be downloaded will be named as the real file name?

I've read about Content-Disposition header but I think this is only in download request and I want to do this on upload request

current code is:

var s3obj = new AWS.S3({
params: {
Bucket: CONFIG.S3_BUCKET,
Key: key,
ContentType: type
}
});

s3obj.upload({
Body: fileData
}).on('httpUploadProgress', function(evt) {
logger.debug('storing on S3: %s', evt);
}).send(function(err, data) {
logger.debug('storing on S3: err: %s data: %s', err, data);
return callback();
});


Thanks!

Answer

Content-Disposition is indeed available when uploading your file to s3 (http://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/S3.html#upload-property). You can then add the filename there

s3obj.upload({
    Key: <the uuid>,
    Body: fileData
    ContentDisposition => 'attachment; filename="' + <the filename> + '"',
}).on('httpUploadProgress', function(evt) {
    logger.debug('storing on S3: %s', evt);
}).send(function(err, data) {
    logger.debug('storing on S3: err: %s data: %s', err, data);
    return callback();
});