Subscribe
How to Use Amazon S3 with Node.js
5 mins read

By: vishwesh

How to Use Amazon S3 with Node.js

Introduction

Amazon S3 (Simple Storage Service) is a highly scalable and reliable cloud storage service provided by Amazon Web Services (AWS). It allows you to store and retrieve data from anywhere on the web. In this tutorial, we will explore how to use Amazon S3 with Node.js, a popular server-side JavaScript runtime environment.

Prerequisites

Before getting started, ensure that you have the following prerequisites in place:

  1. An AWS account: Sign up for an AWS account if you don't have one already.
  2. Node.js and npm: Make sure you have Node.js and npm (Node Package Manager) installed on your machine. You can download them from the official Node.js website.

Step 1: Setting Up AWS SDK

To interact with Amazon S3 using Node.js, we need to install the AWS SDK (Software Development Kit). Open your terminal and run the following command:

npm install aws-sdk

This command will download and install the AWS SDK package from the npm registry.

Step 2: Configuring AWS Credentials

To access your AWS resources, you need to provide your AWS access key ID and secret access key. Follow these steps to configure your AWS credentials:

  1. Log in to the AWS Management Console.
  2. Open the IAM (Identity and Access Management) service.
  3. Navigate to "Users" and click on your username.
  4. Select the "Security credentials" tab.
  5. Click on the "Create access key" button.
  6. Take note of the generated access key ID and secret access key.

Step 3: Creating an S3 Bucket

Before uploading files to Amazon S3, we need to create an S3 bucket. An S3 bucket is like a folder that holds your files. Follow these steps to create an S3 bucket:

  1. Log in to the AWS Management Console.
  2. Open the S3 service.
  3. Click on the "Create bucket" button.
  4. Provide a unique bucket name and select the region where you want to create the bucket.
  5. Click on the "Create" button to create the bucket.

Step 4: Uploading Files to S3

Now that we have set up the AWS SDK, configured our credentials, and created an S3 bucket, we can proceed to upload files to S3. Create a new JavaScript file (e.g., upload.js) and add the following code:

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

// Set up AWS credentials
AWS.config.update({
  accessKeyId: 'YOUR_ACCESS_KEY_ID',
  secretAccessKey: 'YOUR_SECRET_ACCESS_KEY'
});

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

// Read the file from your local machine
const fileContent = fs.readFileSync('path/to/file');

// Set up the parameters for the upload
const params = {
  Bucket: 'YOUR_BUCKET_NAME',
  Key: 'path/to/destination',
  Body: fileContent
};

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

Make sure to replace 'YOUR_ACCESS_KEY_ID', 'YOUR_SECRET_ACCESS_KEY', 'YOUR_BUCKET_NAME', 'path/to/file', and 'path/to/destination' with your own values.

Save the file and run it using the following command:

bashCopy code

node upload.js

This code will upload the specified file to your S3 bucket.

Step 5: Downloading Files from S3

Apart from uploading files, you may also need to download files from your S3 bucket. Let's see how to accomplish that. Create a new JavaScript file (e.g., download.js) and add the following code:

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

// Set up AWS credentials
AWS.config.update({
  accessKeyId: 'YOUR_ACCESS_KEY_ID',
  secretAccessKey: 'YOUR_SECRET_ACCESS_KEY'
});

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

// Set up the parameters for the download
const params = {
  Bucket: 'YOUR_BUCKET_NAME',
  Key: 'path/to/file'
};

// Download the file from S3
s3.getObject(params, function(err, data) {
  if (err) {
    console.log('Error', err);
  } else {
    fs.writeFileSync('path/to/local/file', data.Body);
    console.log('File downloaded successfully');
  }
});

Just like in the previous step, ensure that you replace 'YOUR_ACCESS_KEY_ID', 'YOUR_SECRET_ACCESS_KEY', 'YOUR_BUCKET_NAME', and 'path/to/file' with your own values.

Save the file and run it using the following command:

node download.js

This code will download the specified file from your S3 bucket and save it to your local machine.

Step 6: Deleting Files from S3

If you need to remove files from your S3 bucket, you can do so by following these steps. Create a new JavaScript file (e.g., delete.js) and add the following code:

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

// Set up AWS credentials
AWS.config.update({
  accessKeyId: 'YOUR_ACCESS_KEY_ID',
  secretAccessKey: 'YOUR_SECRET_ACCESS_KEY'
});

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

// Set up the parameters for the deletion
const params = {
  Bucket: 'YOUR_BUCKET_NAME',
  Key: 'path/to/file'
};

// Delete the file from S3
s3.deleteObject(params, function(err, data) {
  if (err) {
    console.log('Error', err);
  } else {
    console.log('File deleted successfully');
  }
});

Again, replace 'YOUR_ACCESS_KEY_ID', 'YOUR_SECRET_ACCESS_KEY', 'YOUR_BUCKET_NAME', and 'path/to/file' with your own values.

Save the file and run it using the following command:

node delete.js

This code will delete the specified file from your S3 bucket.

Conclusion

In this tutorial, we have learned how to use Amazon S3 with Node.js. We covered setting up the AWS SDK, configuring AWS credentials, creating an S3 bucket, and performing file upload, download, and deletion operations. With this knowledge, you can now leverage the power of Amazon S3 to store and retrieve files in your Node.js applications. Experiment further with the AWS SDK documentation to explore additional features and capabilities of Amazon S3 with Node.js. Happy coding!

Recent posts

Don't miss the latest trends

    Popular Posts

    Popular Categories