Home > Back-end >  nodejs: how to download a zip file from AWS S3 bucket, save as local file in EC2 instance and unzip
nodejs: how to download a zip file from AWS S3 bucket, save as local file in EC2 instance and unzip

Time:02-11

My EC2 instance is running a nodejs service. I want to download / save a zip file in S3 bucket to a local location in EC2 instance.

There are a lot of answers about downloading files from s3 bucket, but they are all about how to implement the nodejs api endpoint such that you can download the file from s3 bucket to your local machine:

s3.getObject(bucketParams).createReadStream().pipe(res);

This will create a stream for the file and you just pipe the stream to res, and it will start downloading to your machine.

This is not what I need to do here.

I need to save the file in local in EC2, do some operations such as unzipping it, and then the http response does not return the file. Everything is on the EC2's side.

What should I do? This seems to be so basic and yet I couldn't find much documentations / resources.

CodePudding user response:

Create a read stream for the object and a write stream for the local file and pipe the read stream to the write stream, something like this:

const AWS = require('aws-sdk');
const path = require('path');
const fs = require('fs');

const s3 = new AWS.S3();

const params = {
  Bucket: 'mybucket', 
  Key: 'cats/siberian.png'
};

const rs = s3.getObject(params).createReadStream();
const ws = fs.createWriteStream(path.join('myfolder', 'siberian.png'));

rs.pipe(ws);
  • Related