NodeJS: Upload File to S3

7/11/2021 8:03:34 PM

This is a very simple example to upload a file to AWS S3 using NodeJS. This can easily be used in a Lambda function.

let AWS = require("aws-sdk");
var s3 = new AWS.S3();


async function upload_file()
{
	let aws_bucket = "my-bucket";
	let aws_key = "my-folder/filename.txt";
	let file_contents = "lets write this to the file";

	var params = {
        Body: file_contents,
        Bucket: aws_bucket,
        Key: aws_key

    };
    //console.log(params);


    try
    {
		//the aws sdk providers the "promise()" function that allows you to return a promise
		//since this is an async function, we can await the promise
        let s3_result = await s3.putObject(params).promise();
        console.log(s3_result);
    }
    catch (err)
    {
        console.log(err);
    }
}