S3 set file name on upload

2020-07-30 03:42发布

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!

3条回答
Bombasti
2楼-- · 2020-07-30 04:10

As Frederic has suggested Content-Disposition header will do the job. However, I'd strongly suggest to use a library for building this header (as it will spare you a lot of trouble when handling different platforms supporting different standards and the biggest problem - ENCODING!

There's a great library to achieve it and it's called content-disposition. Simple usage could be as follows:

const contentDisposition = require('content-disposition');
return this.s3.upload({
    ACL: 'private', // Or whatever do you need
    Bucket: someBucket,
    ContentType: mimeType, // It's good practice to set it to a proper mime or to application/octet-stream
    ContentDisposition: contentDisposition(fileName, {
      type: 'inline'
    }),
    Key: someKey,
    Body: someBody
  });
}
查看更多
来,给爷笑一个
3楼-- · 2020-07-30 04:16

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();
});
查看更多
可以哭但决不认输i
4楼-- · 2020-07-30 04:34

I have the similar requirement that I need to upload file with lowercase file name no matter what file name the user uploads. I tried ContentDisposition but somehow it didn't work so I tried the below configuration and it worked for me.

In short, make sure your Key reflect the name you would like the file to be.

const params = {
  Bucket: this.config.bucketName,
  Key: this.config.getFolder() + file.name.toLowerCase(),
  Body: file,
  ContentType: contentType,
  Metadata: {
    "websiteid": "999",
    "userKey": "xyz
  }
};
查看更多
登录 后发表回答