Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Download image from S3 bucket to Lambda temp folder (Node.js)

Good day guys.

I have a simple question: How do I download an image from a S3 bucket to Lambda function temp folder for processing? Basically, I need to attach it to an email (this I can do when testing locally).

I have tried:

s3.download_file(bucket, key, '/tmp/image.png')

as well as (not sure which parameters will help me get the job done):

s3.getObject(params, (err, data) => {
    if (err) {
        console.log(err);
        const message = `Error getting object ${key} from bucket ${bucket}.`;
        console.log(message);
        callback(message);
    } else {

        console.log('CONTENT TYPE:', data.ContentType);
        callback(null, data.ContentType);
    }
});

Like I said, simple question, which for some reason I can't find a solution for.

Thanks!

like image 370
JBM Avatar asked Aug 16 '16 10:08

JBM


People also ask

How do I transfer files from S3 bucket to Lambda?

Create S3 Bucket in Source Account, to which the logs will be uploaded. Add below Bucket Access policy to the IAM Role created in Destination account. Lambda function will assume the Role of Destination IAM Role and copy the S3 object from Source bucket to Destination.

How do I download from S3 bucket to local?

You can download an object from an S3 bucket in any of the following ways: Select the object and choose Download or choose Download as from the Actions menu if you want to download the object to a specific folder. If you want to download a specific version of the object, select the Show versions button.

Can I download a folder from S3 bucket?

Use the cp command to download a folder inside a bucket from S3 to local. recursive option will download all files and folders if you have a recursive folder/file structure.


1 Answers

If you're writing it straight to the filesystem you can also do it with streams. It may be a little faster/more memory friendly, especially in a memory-constrained environment like Lambda.

var fs = require('fs');
var path = require('path');

var params = {
    Bucket: "mybucket",
    Key: "image.png"
};

var tempFileName = path.join('/tmp', 'downloadedimage.png');
var tempFile = fs.createWriteStream(tempFileName);

s3.getObject(params).createReadStream().pipe(tempFile);
like image 101
Seafish Avatar answered Sep 30 '22 09:09

Seafish