Upload to AWS S3 Using a Node.js Script or AWS Lambda
By: Michael Whittle
Tags: Storage, AWS, Nodejs, Lambda, S3

Okay, S3 and and our programmatic IAM user are done.

So now for a basic test using a Node.js script. I’m making the assumption you are using Linux or a Mac and have at least Node v12.* installed. Here are the steps to create a basic project.

~ % node -v

v12.14.1 ~ % npm -v

6.13.4 ~ % mkdir ~/mediumtutorial

~ % cd ~/mediumtutorial mediumtutorial % npm install aws-sdk

npm WARN saveError ENOENT: no such file or directory, open '/Users/REMOVED/mediumtutorial/package.json'

npm notice created a lockfile as package-lock.json. You should commit this file.

npm WARN enoent ENOENT: no such file or directory, open '/Users/REMOVED/mediumtutorial/package.json'

npm WARN mediumtutorial No description

npm WARN mediumtutorial No repository field.

npm WARN mediumtutorial No README data

npm WARN mediumtutorial No license field. + aws-sdk@2.756.0

added 14 packages from 66 contributors and audited 14 packages in 1.353s

found 0 vulnerabilities

Now create a file called helloworld.js using your favourite text editor.

const AWS = require('aws-sdk'); const BUCKET_NAME = "YOUR_S3_BUCKET";

const IAM_USER_KEY = "YOUR_IAM_USER_KEY";

const IAM_USER_SECRET = "YOUR_IAM_USER_SECRET"; const s3bucket = new AWS.S3({

accessKeyId: IAM_USER_KEY,

secretAccessKey: IAM_USER_SECRET

}); function uploadObjectToS3Bucket(objectName, objectData) {

const params = {

Bucket: BUCKET_NAME,

Key: objectName,

Body: objectData

}; s3bucket.upload(params, function(err, data) {

if (err) throw err;

console.log(`File uploaded successfully at ${data.Location}`)

});

} uploadObjectToS3Bucket('helloworld.json', 'Hello World!');

If you have been following the steps with me in this tutorial if you run the script it will create an object in your S3 bucket called “helloworld.json” with the content ‘{ “message” : “Hello World!” }’.

mediumtutorial % node ./helloworld.js

File uploaded successfully at https://mediumtutorial.s3.amazonaws.com/helloworld.json

A tip here that had me a little flummoxed was when I ran this script I didn’t see the object in S3 automatically when I opened my bucket. I had to click the refresh icon in the S3 bucket for it to show.

So hopefully this worked for you.

So now to show you how to do this with Lambda. I could not get it working exactly using the same code so I had to tweak it a little.

The first step is to log into the Amazon AWS Console and search for Lambda under Find Services.

Click on the orange “Create function” button.

For the purpose of this tutorial you will want to select the default “Author from scratch”, give your function a name (in this case “mediumtutorial”, select the Runtime (in this case “Node.js 12.x”), and for the Execution role make sure you select “Create a new rule with basic Lambda permissions”. If this isn’t your first Lambda function you may want to “Use an existing role”.

It will take some time to create the function but once completed you will be on the Configuration screen. There are many useful options there but for the purpose of the tutorial I want to keep this as simple as possible. Please scroll down to the “Function code” section to find the default “Hello from Lambda!” code.

Please replace the default code with this…