DEV Community

Cover image for How to download s3 object and save to file
Volkan Şentürk
Volkan Şentürk

Posted on

How to download s3 object and save to file

import * as fs from "fs";
import * as AWS from "aws-sdk";

const BUCKET_NAME = "<<bucket-name>>";
const IAM_USER_KEY = "<<user-key>>";
const IAM_USER_SECRET = "<<user-secret>>";

const s3bucket = new AWS.S3({
  accessKeyId: IAM_USER_KEY,
  secretAccessKey: IAM_USER_SECRET
});

export function downloadToS3(fileName: string): Promise<any> {

  const params = {
    Bucket: BUCKET_NAME,
    Key: "myapp" + "/" + filename,
  };


  s3bucket.getObject(params, function (err, data) {
    if (err) {
        console.log("error!");
    }
    else {
        fs.createWriteStream(filename).write(data.Body);
        console.log(data.Body);
    }
  });  

  return new Promise((resolve, reject) => {

  });
}


downloadToS3("<<filename>>")
Enter fullscreen mode Exit fullscreen mode

Top comments (0)