2016-11-21 74 views
0

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

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

我读过有关内容处置头,但我认为这仅仅是下载请求,我想这样做对上传请求

当前的代码是:

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(); 
}); 

谢谢!

回答

2

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(); 
}); 
+0

很愚蠢的我,只是没有找到它。谢谢! –

1

Ad Frederic建议,Content-Disposition header将完成这项工作。然而,我强烈建议使用库,用于构建该头(如处理支持不同的标准,最大的问题不同的平台时,它会饶你很多麻烦 - !编码

有很大的库来实现它 - 叫... content-disposition :)。简单的用法可能如下:

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 
    }); 
} 
+0

谢谢,我会试试这个 –