简体   繁体   中英

AWS S3 Download file directly via Browser

I am trying to download a file from S3 directly in browser. I am using AWS Javascript SDK alongwith NodeJS + Webpack.

I am able to make a GetObject request and can see the browser (Chrome) downloading the bytes in "Developer tools" view.

I have "Content Disposition " header in response set as:

Content-Disposition:attachment; filename="A1.jpeg"

However I do not see "Save As" dialog or any file being created in my filesystem (in Downloads folder). Where are the bytes going to?

How can I save the data in a file ?

Here's my code :

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

I faced this case before, then i used "s3.getSignedUrl" function instead of "s3.getObject" as following:

s3.getSignedUrl('putObject',s3Params).then(function(url){
  //the returned "url" used by the browser to download
},function(error){
  //Error handling
})

and the s3Params will contain (Buckey,key "object name" and so on)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM