Uploading files, especially images, to Amazon S3 is a common requirement in modern web applications. AWS S3 (Simple Storage Service) is highly scalable, durable, and secure — making it ideal for storing images, documents, and other assets.
This blog will show you how to upload an image to S3 using Node.js, step by step.
How to Upload an Image to AWS S3 Using Node.js?
Step 1: Set Up Your AWS Credentials
First, make sure you have:
- An AWS account
- Access Key ID & Secret Access Key
- An S3 bucket created (e.g., my-image-bucket)
Install AWS CLI (optional) and configure:
aws configureStep 2: Install Required Packages
You need the AWS SDK and optionally multer if you want to handle uploads via an API.
npm install aws-sdk multerStep 3: Configure AWS SDK in Node.js
Create a file named s3Upload.js:
const AWS = require('aws-sdk');
const fs = require('fs');
const path = require('path');
// Load credentials from env or AWS config
AWS.config.update({
accessKeyId: process.env.AWS_ACCESS_KEY_ID,
secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY,
region: 'us-east-1', // replace with your bucket region
});
const s3 = new AWS.S3();Step 4: Upload Image to S3 Bucket
You can upload an image either from disk or from a buffer. Here’s an example from the local filesystem:
const uploadImage = async () => {
const filePath = path.join(__dirname, 'sample.jpg');
const fileContent = fs.readFileSync(filePath);
const params = {
Bucket: 'my-image-bucket',
Key: 'uploads/sample.jpg', // folder + file name in bucket
Body: fileContent,
ContentType: 'image/jpeg',
ACL: 'public-read', // optional: makes file publicly accessible
};
try {
const data = await s3.upload(params).promise();
console.log('Upload Success:', data.Location);
} catch (err) {
console.error('Upload Error:', err.message);
}
};
uploadImage();Conclusion
Uploading images to AWS S3 using Node.js is pretty straightforward once your AWS SDK is set up and your credentials are ready. Whether you’re dealing with files stored locally or handling uploads coming through an API, this method works well and can handle big, real-world apps.
For teams building enterprise applications, it’s common to hire Node.js developers who know AWS services inside out. This helps keep uploads running smoothly and keeps your data safe.
In short, with the right setup and expertise, managing image uploads to S3 stays efficient and secure as your app grows.




