Mangesh Tak
Mangesh Tak

Reputation: 396

Download a file from AWS-S3 to local computer file system

I have written microservice using api-gateway, aws-lambda and s3 bucket. I am currently able to read all file content in lambda function. But I want to download the the file from s3 to local file system . I tried using pipe(localfilepath) but it throws error about the path(not found).

const AWS = require('aws-sdk'); 
var s3 = new AWS.S3();
exports.handler = function(event, context, callback) {
    var params = {
      "Bucket": "bucketname",
      "Key": "hello"
        };

    var readstream = s3.getObject(params).createReadStream();
    readstream.on('data',(data) => {
    console.log(data)
    })
    .on('end', () => {
      return callback(null, "file read done")
    })
    .on('error',(e) => {
      console.log("Error occured",e)
    })
};

How can I download the file to users file system?

Upvotes: 2

Views: 1992

Answers (2)

Victor
Victor

Reputation: 3517

If you want allow users downloading a file but want to do before some operation in lambda or generate content of S3 object then you can use getSignedUrl.

It creates temporary url that you can return to your clients and they could download it. In this case don't need run http server on S3. As a plus it supports ssl and resume downloading as well.

Upvotes: 1

Matus Dubrava
Matus Dubrava

Reputation: 14462

If by user you mean clients that are reaching your API Gateway then you can't. Imagine that I would create a Lambda function that would start loading tons of data to your computer once you visit my website. In this case, make the object in S3 downloadable and let users decide whether they want to download it or not by providing them with link to that object.

If by local file system you mean lambda's local file system then you have access to /tmp folder inside of the container in which the lambda is being executed but its size is limited to 500MB and it goes away once the container is destroyed so this is probably not what you are looking for either. This is useful in cases where some module that you are using requires access to file system during execution.

Upvotes: 0

Related Questions