Uploading Files to Amazon S3 using Node.js: A Comprehensive Guide with Examples

--

s3

Prerequisites

Before we start, make sure you have the following installed on your machine:

  • Node.js and NPM (Node Package Manager) installed on your machine
  • AWS SDK for JavaScript in Node.js

Step 1: Create an S3 Bucket

First, you need to create an S3 bucket to store the files you want to upload. Go to your AWS console and create a new S3 bucket. Make sure you take note of the bucket name, region, and access keys that you will use later in your Node.js code.

Step 2: Install the AWS SDK for JavaScript

Open your terminal and navigate to your Node.js project directory. Then, run the following command to install the AWS SDK for JavaScript:

npm install aws-sdk

Step 3: Set up AWS Credentials

Next, you need to set up your AWS credentials to access your S3 bucket. You can either set up environment variables or create a new configuration file in your project directory. For this tutorial, we’ll use environment variables.

Open your terminal and run the following command to set your AWS credentials as environment variables:

export AWS_ACCESS_KEY_ID=<your-access-key>
export AWS_SECRET_ACCESS_KEY=<your-secret-key>

Make sure to replace <your-access-key> and <your-secret-key> with your own AWS access keys.

Step 4: Upload a Single File

Now that you’ve set up your S3 bucket and AWS credentials, let’s write some Node.js code to upload a single file to S3.

Create a new file uploadFile.js in your project directory and add the following code:

const AWS = require('aws-sdk');
const fs = require('fs');

// Set the region and access keys
AWS.config.update({
region: '<your-region>',
accessKeyId: process.env.AWS_ACCESS_KEY_ID,
secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY
});

// Create a new instance of the S3 class
const s3 = new AWS.S3();

// Set the parameters for the file you want to upload
const params = {
Bucket: '<your-bucket-name>',
Key: 'myFile.txt',
Body: fs.createReadStream('path/to/myFile.txt')
};

// Upload the file to S3
s3.upload(params, (err, data) => {
if (err) {
console.log('Error uploading file:', err);
} else {
console.log('File uploaded successfully. File location:', data.Location);
}
});

Make sure to replace <your-region> and <your-bucket-name> with your own S3 bucket region and name.

To upload a single file, run the following command in your terminal:

node uploadFile.js

This will upload the file myFile.txt to your S3 bucket.

Step 5: Upload a Whole Folder

If you want to upload a whole folder to S3, you can use the fs module to loop through all the files in the folder and upload them one by one. Here's an example:

const AWS = require('aws-sdk');
const fs = require('fs');
const path = require('path');

// Set the region and access keys
AWS.config.update({
region: '<your-region>',
accessKeyId: process.env.AWS_ACCESS_KEY_ID,
secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY
});

// Create a new instance of the S3 class
const s3 = new AWS.S3();

// Set the parameters for the folder you want to upload
const folderPath = 'path

Thanks for reading!

--

--

The Modern Developer Academy - by Alex Madrazo

Guatemalan software enthusiast in Madrid, leading high-performing engineering teams. Passionate about tech, entrepreneurship, and economics.