简体   繁体   中英

Saving an S3 buffer to file through AWS Javascript SDK

I'm using the AWS Javascript SDK to download a file from S3

var s3 = new AWS.S3();
var params = {
    Bucket: "MYBUCKET", 
    Key: file
   };
   s3.getObject(params, function(err, data) {
     if (err) console.log(err, err.stack); // an error occurred
     else {
        //code to save file from data's byte array here
     } 
   });

This feels like it should be easier than I'm making it out to be. Basically I want to trigger the native file download for the browser. Every resource I've found on the internet is for node's file system. I can't just use the file's URL to download as it is stored encrypted via KMS, so that is why I am going about it this way.

Thanks for the help!

I ended up changing how I was storing files. Instead of encrypting them with KMS, I moved them to a private bucket and then based the retrieval off of the logged in cognito user's ID. Then, I switched to using getSignedURL to appropriately pass in the cognito user ID.

var s3 = new AWS.S3();
var params = {
    Bucket: "MYBUCKET", 
    Key: cognitoUser.username + "/" + file
   };
var url = s3.getSignedUrl('getObject', params);
window.open(url);

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