At SvayambhuTech, we specialize in crafting tailored solutions for your digital needs, including cutting-edge website designing, custom website development, and seamless cloud integrations like AWS S3. Today, we’ll walk you through connecting AWS S3 to a Node.js project in just a few simple steps.

Why Choose SvayambhuTech?

Our expertise in cloud-based integrations ensures your applications are fast, secure, and scalable. Whether you’re looking to build a robust custom website or integrate cloud services like AWS S3, we’ve got you covered.


Step-by-Step Guide to Connect AWS S3 with Node.js

Step 1: Create an AWS Account and Log In

If you haven’t already, sign up for an AWS account. Once logged in, navigate to the AWS S3 console to start.

Step 2: Create Your S3 Bucket

Create a new bucket to store your files. Ensure the name is globally unique, as AWS S3 enforces this. For example, we’ll name our bucket svayambhutech-testing.

Step 3: Organize Your Bucket

Within your bucket, create subfolders if needed. For instance, add subfolders like documents and images for better organization.

Step 4: Set Up AWS IAM Permissions

Go to the IAM console to either create a new IAM user or assign permissions to an existing one. Ensure the user or role has access to the S3 bucket. Generate an Access Key ID and Secret Access Key under the Security Credentials tab and store them securely.

Step 5: Set Up Node.js Project

Open your Node.js project in VS Code. Install the required AWS SDK packages using the following commands:

npm install @aws-sdk/client-s3
npm install @aws-sdk/s3-request-presigner

Step 6: Configure Environment Variables

Create a .env file in your project (or edit an existing one) and add the following keys for your AWS S3 connection:

AWS_BUCKET_NAME="svayambhutech-testing"
AWS_ACCESSKEYID="YOUR_ACCESS_KEY_ID"
AWS_SECRETACCESSKEY="YOUR_SECRET_ACCESS_KEY"
AWS_REGION="YOUR_REGION"

Step 7: Create an AWS S3 Utility File

In your Node.js project, create a file named awsS3connect.js. This file will handle all interactions with AWS S3.

const { S3Client, PutObjectCommand, GetObjectCommand, DeleteObjectCommand } = require('@aws-sdk/client-s3');
const { getSignedUrl } = require('@aws-sdk/s3-request-presigner');
const fs = require('fs');

// S3 Client Initialization
const s3Client = new S3Client({
    region: process.env.AWS_REGION,
    credentials: {
        accessKeyId: process.env.AWS_ACCESSKEYID,
        secretAccessKey: process.env.AWS_SECRETACCESSKEY,
    },
});

// Upload File to S3
exports.uploadFileToAws = async (fileName, filePath) => {
    const uploadParams = {
        Bucket: process.env.AWS_BUCKET_NAME,
        Key: fileName,
        Body: fs.createReadStream(filePath),
    };
    await s3Client.send(new PutObjectCommand(uploadParams));
};

// Get File URL
exports.getFileUrlFromAws = async (fileName, expireTime = 900) => {
    const command = new GetObjectCommand({
        Bucket: process.env.AWS_BUCKET_NAME,
        Key: fileName,
    });
    return await getSignedUrl(s3Client, command, { expiresIn: expireTime });
};

// Delete File from S3
exports.deleteFileFromAws = async (fileName) => {
    const deleteParams = {
        Bucket: process.env.AWS_BUCKET_NAME,
        Key: fileName,
    };
    await s3Client.send(new DeleteObjectCommand(deleteParams));
};

Step 8: Use AWS S3 Functions

Create a file named s3Use.js to utilize the functions:

const awsS3 = require('./awsS3connect');

// Upload File
exports.uploadToS3 = async () => {
    const filePath = './uploads/sample.txt';
    await awsS3.uploadFileToAws('documents/sample.txt', filePath);
};

// Get File URL
exports.getS3FileUrl = async () => {
    const fileUrl = await awsS3.getFileUrlFromAws('documents/sample.txt');
    console.log('File URL:', fileUrl);
};

// Delete File
exports.deleteS3File = async () => {
    await awsS3.deleteFileFromAws('documents/sample.txt');
};

Why Use SvayambhuTech for Your AWS Integrations?

At SvayambhuTech, we simplify complex tasks like AWS S3 integrations. Our expertise extends beyond cloud services to include:

  • Custom Website Development: Tailored websites designed to suit your business needs.
  • Website Designing: Eye-catching designs that captivate your audience.
  • SEO Optimization: Ensuring your website ranks at the top of search results.

Take Your Cloud Experience to the Next Level

Ready to elevate your projects? Contact SvayambhuTech today for expert AWS S3 integrations, custom website development, and more. Visit our website at svayambhutech.com to learn more.


Let’s build something amazing together! 🌟