How to upload a binary file to S3 using AWS SDK for Node.js?

Sometimes, we want to upload a binary file to S3 using AWS SDK for Node.js.

In this article, we’ll look at how to upload a binary file to S3 using AWS SDK for Node.js.

How to upload a binary file to S3 using AWS SDK for Node.js?

To upload a binary file to S3 using AWS SDK for Node.js, we can call putObject with a read stream.

For instance, we write

const fileStream = fs.createReadStream("F:/directory/fileName.ext");
const putParams = {
  Bucket: s3bucket,
  Key: s3key,
  Body: fileStream
};

s3.putObject(putParams, (putErr, putData) => {
  if (putErr) {
    console.error(putErr);
  } else {
    console.log(putData);
  }
});

to call putObject with the putParams object.

We set putParams.Body to fileStream to upload its content to S3 at the location set by the Bucket and Key.

Conclusion

To upload a binary file to S3 using AWS SDK for Node.js, we can call putObject with a read stream.