Mastering AWS S3 with JavaScript: S3Client Config & Most Useful Commands 🚀


Whether you're building a full-stack app or managing static assets, Amazon S3 is a go-to service for scalable storage. In this post, we’ll break down:
- 📦 S3Client setup (with
@aws-sdk/client-s3
) - ⚙️ Most useful operations: creating/deleting buckets, uploading/downloading files
- 🔁 A simple workflow you can apply to your own app
🔧 Setting Up the AWS S3 Client
First, install the SDK:
npm install @aws-sdk/client-s3
Then configure your client:
// config/s3Client.js
import { S3Client } from '@aws-sdk/client-s3';
const s3Client = new S3Client({
region: 'your-region', // e.g., 'us-east-1'
credentials: {
accessKeyId: process.env.AWS_ACCESS_KEY_ID,
secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY
},
});
export default s3Client;
💡 Best Practice: Store your credentials securely using environment variables or IAM roles (if you're using Lambda/EC2).
📚 Most Useful S3 Commands
✅ 1. Create a Bucket
import { CreateBucketCommand } from '@aws-sdk/client-s3';
await s3Client.send(new CreateBucketCommand({
Bucket: 'my-awesome-bucket',
}));
🗑️ 2. Delete a Bucket
import { DeleteBucketCommand } from '@aws-sdk/client-s3';
await s3Client.send(new DeleteBucketCommand({
Bucket: 'my-awesome-bucket',
}));
📤 3. Upload an Object
import { PutObjectCommand } from '@aws-sdk/client-s3';
import fs from 'fs';
const fileStream = fs.createReadStream('./example.txt');
await s3Client.send(new PutObjectCommand({
Bucket: 'my-awesome-bucket',
Key: 'example.txt',
Body: fileStream,
}));
📥 4. Download an Object
import { GetObjectCommand } from '@aws-sdk/client-s3';
import fs from 'fs';
const response = await s3Client.send(new GetObjectCommand({
Bucket: 'my-awesome-bucket',
Key: 'example.txt',
}));
response.Body.pipe(fs.createWriteStream('./downloaded.txt'));
❌ 5. Delete an Object
import { DeleteObjectCommand } from '@aws-sdk/client-s3';
await s3Client.send(new DeleteObjectCommand({
Bucket: 'my-awesome-bucket',
Key: 'example.txt',
}));
📃 6. List Objects in a Bucket
import { ListObjectsV2Command } from '@aws-sdk/client-s3';
const response = await s3Client.send(new ListObjectsV2Command({
Bucket: 'my-awesome-bucket',
}));
console.log(response.Contents);
🧠 Workflow Example: Image Upload API
Here’s a simple idea of how your workflow could look in a backend:
- User uploads an image
- Frontend sends file to backend
- Backend uploads file to S3
- S3 returns URL
- URL saved in DB
- URL sent back to frontend
// Upload and return URL
const uploadToS3 = async (file, key) => {
await s3Client.send(new PutObjectCommand({
Bucket: 'my-awesome-bucket',
Key: key,
Body: file.buffer,
ContentType: file.mimetype,
}));
return `https://${yourBucket}.s3.${yourRegion}.amazonaws.com/${key}`;
};
🧪 Extra Commands to Explore
CopyObjectCommand
– Duplicate filesHeadObjectCommand
– Check if file existsPutObjectAclCommand
– Make file public- Multipart Upload – For large files (>5MB)
✅ Final Thoughts
The AWS SDK v3 makes working with S3 modular and easy. With the right config and a clean workflow, integrating S3 into your app is seamless. If you’re building a file uploader, static hosting solution, or media management API—this toolkit is all you need to start strong.
💬 Have questions or want to see this workflow in Express, Next.js, or NestJS? Drop a comment!
Subscribe to my newsletter
Read articles from Shubham Bhilare directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
