Categories
JavaScript Answers

How to write to S3 with Node AWS Lambda function?

Spread the love

To write to S3 with Node AWS Lambda function, we use the putObject method.

For instance, we write

const AWS = require("aws-sdk");

const putObjectToS3 = (bucket, key, data) => {
  const s3 = new AWS.S3();
  const params = {
    Bucket: bucket,
    Key: key,
    Body: data,
  };
  s3.putObject(params, (err, data) => {
    if (err) {
      console.log(err, err.stack);
    } else {
      console.log(data);
    }
  });
};

to call putObject to put the file at the Bucket with the Key.

Body has the file data.

And we get the file data from the data parameter in the callback.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *