typescriptintermediate
AWS SDK v3 — S3 Operations in TypeScript
Perform S3 operations with AWS SDK v3: upload, download, list, presigned URLs, and multipart upload.
typescriptPress ⌘/Ctrl + Shift + C to copy
import {
S3Client,
PutObjectCommand,
GetObjectCommand,
ListObjectsV2Command,
} from '@aws-sdk/client-s3';
import { getSignedUrl } from '@aws-sdk/s3-request-presigner';
import { readFileSync } from 'fs';
const s3 = new S3Client({ region: 'us-east-1' });
const BUCKET = 'my-app-uploads';
// Upload a file
async function upload(key: string, filePath: string): Promise<string> {
await s3.send(new PutObjectCommand({
Bucket: BUCKET,
Key: key,
Body: readFileSync(filePath),
ContentType: 'application/octet-stream',
}));
return `s3://${BUCKET}/${key}`;
}
// Generate presigned download URL (valid 1 hour)
async function getDownloadUrl(key: string): Promise<string> {
const command = new GetObjectCommand({ Bucket: BUCKET, Key: key });
return getSignedUrl(s3, command, { expiresIn: 3600 });
}
// List objects with prefix
async function listObjects(prefix: string) {
const result = await s3.send(new ListObjectsV2Command({
Bucket: BUCKET,
Prefix: prefix,
MaxKeys: 100,
}));
return result.Contents?.map(obj => ({
key: obj.Key!,
size: obj.Size!,
modified: obj.LastModified!,
})) ?? [];
}
// Generate presigned upload URL (client-side upload)
async function getUploadUrl(key: string): Promise<string> {
const command = new PutObjectCommand({ Bucket: BUCKET, Key: key });
return getSignedUrl(s3, command, { expiresIn: 300 });
}Sponsored
Cloudflare R2
Use Cases
- Server-side file uploads to S3
- Generating presigned URLs for client-side uploads
- Listing and managing S3 objects programmatically
Tags
Related Snippets
Similar patterns you can reuse in the same workflow.
bashadvanced
Terraform — AWS S3 + CloudFront Static Site
Provision an S3 static website with CloudFront CDN, SSL certificate, and origin access control.
Best for: Deploying static sites with global CDN distribution
#terraform#aws
typescriptintermediate
Vercel API — Trigger and List Deployments
Use the Vercel REST API to trigger deployments, list recent builds, and check deployment status.
Best for: Triggering deployments from CI or Slack bots
#vercel#api
bashadvanced
Aws Cdk
DevOps practice: aws-cdk
Best for: infrastructure management
#devops#infrastructure
typescriptintermediate
S3 Presigned URL Generator
Generate secure presigned URLs for S3 upload and download operations with expiry and content type.
Best for: Direct browser uploads
#aws#s3