AWS S3通过浏览器直接下载文件
我正试图从S3直接在浏览器中下载一个文件。 我正在使用AWS Javascript SDK和NodeJS + Webpack。
我可以做一个GetObject请求,并可以在“开发人员工具”视图中看到浏览器(Chrome)下载字节。
我有“内容处置”标题在响应设置为:
Content-Disposition:attachment; filename="A1.jpeg"
但是,我没有看到“另存为”对话框或正在我的文件系统(在下载文件夹)中创build的任何文件。 字节在哪里?
如何将数据保存在文件中?
这是我的代码:
var getObject = function getObject(bucketName, fileName) { var params = { Bucket: bucketName, Key: fileName, ResponseContentDisposition: 'attachment; filename="' + fileName + '"' }; return s3.getObject(params).promise(); } getObject(BUCKET_NAME, item) .then( function (getFileResponse) { console.log(" file downloaded.. " ); } , function(error) { // Common error handling console.log(" error = " + error); })
之前我遇到过这种情况,于是我用“s3.getSignedUrl”函数代替“s3getObject”,如下所示:
s3.getSignedUrl('putObject',s3Params).then(function(url){ //the returned "url" used by the browser to download },function(error){ //Error handling })
而s3Params将包含(Buckey,键“对象名称”等)