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

Shubham Bhilare @shubham_bhilare_3611

About: A passionate developer, always learning!

Location:
Pune , Maharashtra
Joined:
May 14, 2025

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

Publish Date: Jun 5
0 0

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
Enter fullscreen mode Exit fullscreen mode

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;
Enter fullscreen mode Exit fullscreen mode

💡 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',
}));
Enter fullscreen mode Exit fullscreen mode

🗑️ 2. Delete a Bucket

import { DeleteBucketCommand } from '@aws-sdk/client-s3';

await s3Client.send(new DeleteBucketCommand({
  Bucket: 'my-awesome-bucket',
}));
Enter fullscreen mode Exit fullscreen mode

📤 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,
}));
Enter fullscreen mode Exit fullscreen mode

📥 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'));
Enter fullscreen mode Exit fullscreen mode

❌ 5. Delete an Object

import { DeleteObjectCommand } from '@aws-sdk/client-s3';

await s3Client.send(new DeleteObjectCommand({
  Bucket: 'my-awesome-bucket',
  Key: 'example.txt',
}));
Enter fullscreen mode Exit fullscreen mode

📃 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);
Enter fullscreen mode Exit fullscreen mode

🧠 Workflow Example: Image Upload API

Here’s a simple idea of how your workflow could look in a backend:

  1. User uploads an image
  2. Frontend sends file to backend
  3. Backend uploads file to S3
  4. S3 returns URL
  5. URL saved in DB
  6. 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}`;
};
Enter fullscreen mode Exit fullscreen mode

🧪 Extra Commands to Explore

  • CopyObjectCommand – Duplicate files
  • HeadObjectCommand – Check if file exists
  • PutObjectAclCommand – 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!

Comments 0 total

    Add comment