S3在上传时设置文件名

gib*_*son 3 amazon-s3 node.js

我正在使用aws-sdk module将文件上传到 S3 。我使用 uuid 来表示每个文件。

我的问题是 - 如何设置真实文件名(不是 uuid),这样当我稍后从 S3 下载密钥时 - 将下载的文件将被命名为真实文件名?

我已阅读有关 Content-Disposition 标头的内容,但我认为这仅在下载请求中,我想在上传请求中执行此操作

当前代码是:

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();
});
Run Code Online (Sandbox Code Playgroud)

谢谢!

Fré*_*nri 5

Content-Disposition将文件上传到 s3 时确实可用 ( http://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/S3.html#upload-property )。然后您可以在那里添加文件名

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();
});
Run Code Online (Sandbox Code Playgroud)