AWS S3 and Nodejs File Managment

mesutbesagil
4 min readSep 9, 2023

--

nodejs with aws-s3

Step 1: AWS Account and AWS S3 First Steps

Create an AWS Account and Login to AWS Console

Before you can get started with AWS S3, you must create an AWS account. Follow the steps below:

  1. Go to the AWS website.
  2. Click on “Create Account” and provide the required information.
  3. Log in to the AWS Console.
  4. General Information about AWS S3 Service
  5. AWS S3 (Simple Storage Service) is a cloud storage service where users can store all kinds of files over the internet. It is used to securely store and manage your data.

Creating an AWS S3 Bucket

You must create an AWS S3 bucket to store your files:

  1. Go to the “S3” service in the AWS Console.
  2. Click on “Create Bucket”.
  3. Give your bucket a unique name and configure other settings.
  4. Bucket Permissions and Security Settings

You must configure permissions to secure your bucket:

  1. Go to your bucket and click on the “Permissions” tab.
  2. Add permissions and limit unnecessary access.

Spet 2: AWS SDK Setup with Node.js

Node.js is a platform for running server-side JavaScript. We will use Node.js to communicate with AWS S3. Follow these steps to add the AWS SDK to our project:

  1. Open the terminal in the root directory of your Node.js project. 2 . Install the AWS SDK using the following command:
npm install aws-sdk

AWS Authentication Information (Access Key and Secret Key) To communicate with AWS you need to get your authentication credentials. These consist of the Access Key and Secret Key and are obtained from the AWS Console.

const AWS = require(‘aws-sdk’);
// Define credentials
AWS.config.update({
accessKeyId: ‘YOUR_ACCESS_KEY’,
secretAccessKey: ‘YOUR_SECRET_KEY’,
});

By following these steps, you can create your AWS account, create an S3 bucket and add Node.js and AWS SDK to your project. After that, you can use these basic settings to perform file uploads and downloads.

Step 3: Uploading Files to AWS S3 with Node.js

Create a new Node.js project. Open the terminal in the root directory of your project and start a Node.js project using the following commands:

mkdir aws-s3-file-upload
cd aws-s3-file-upload
npm init -y

File Upload Using AWS SDK You can upload files to AWS S3 using the AWS SDK. Here is a sample code fragment:

const AWS = require(‘aws-sdk’);
const fs = require(‘fs’);
// Define AWS credentials
AWS.config.update({
accessKeyId: ‘YOUR_ACCESS_KEY’,
secretAccessKey: ‘YOUR_SECRET_KEY’,
});
// Create the S3 client
const s3 = new AWS.S3();
// Define the information of the file to upload
const params = {
Bucket: ‘your-bucket-name’,
Key: ‘example.jpg’, // The name of the file you want to upload
Body: fs.createReadStream(‘example.jpg’), // Path to the uploaded file
};
// Upload the file
s3.upload(params, (err, data) => {
if (err) {
console.error(‘Error loading file:’, err);
} else {
console.log(‘File uploaded successfully. Access URL:’, data.Location);
}
});

The example code fragment above demonstrates the process of uploading files to AWS S3. It explains how to use the AWS SDK and how to define the required parameters. By following these steps, you have learned how to upload files to AWS S3 using Node.js. Now, you can move on to Step 4 to review the settings and code examples for downloading files.

Step 4: Downloading Files from AWS S3 with Node.js

Settings Required for File Download You must make the necessary settings for downloading files from AWS S3. Here are the details of this step:

  1. Go to the S3 service in the AWS Console.
  2. Click the name of the bucket containing the file you want to download.
  3. Click on the file to open its details.
  4. Click on the “Actions” button and select “Download”.
  5. File Download Using AWS SDK Now, let’s use Node.js to download files using the AWS SDK. Here is a sample code fragment:
Copy code
const AWS = require(‘aws-sdk’);
const fs = require(‘fs’);
// Define AWS credentials
AWS.config.update({
accessKeyId: ‘YOUR_ACCESS_KEY’,
secretAccessKey: ‘YOUR_SECRET_KEY’,
});
// Create the S3 client
const s3 = new AWS.S3();
// Define the information of the file to download
const params = {
Bucket: ‘your-bucket-name’,
Key: ‘example.jpg’, // The name of the file you want to download
};
// Download the file
s3.getObject(params, (err, data) => {
if (err) {
console.error(‘Error downloading file:’, err);
} else {
fs.writeFileSync(‘downloaded-example.jpg’, data.Body);
console.log(‘File downloaded successfully.’);
}
});

The example code snippet above demonstrates the process of downloading a file from AWS S3. By defining the name of the file and the path, it downloads the file and saves it to a local file.

Sources: AWS S3 Official Documentation. Nodejs aws-sdk.

--

--