S3 Presigned URL Generator
Generate secure presigned URLs for S3 upload and download operations with expiry and content type.
import {
S3Client,
GetObjectCommand,
PutObjectCommand,
} from '@aws-sdk/client-s3';
import { getSignedUrl } from '@aws-sdk/s3-request-presigner';
const s3 = new S3Client({ region: process.env.AWS_REGION });
const BUCKET = process.env.S3_BUCKET!;
export async function getUploadUrl(key: string, contentType: string) {
const command = new PutObjectCommand({
Bucket: BUCKET,
Key: key,
ContentType: contentType,
});
return getSignedUrl(s3, command, { expiresIn: 3600 });
}
export async function getDownloadUrl(key: string) {
const command = new GetObjectCommand({
Bucket: BUCKET,
Key: key,
});
return getSignedUrl(s3, command, { expiresIn: 3600 });
}Sponsored
Try Cloudflare R2 — Zero Egress Object Storage
Use Cases
- Direct browser uploads
- Secure file downloads
- Temporary access links
Tags
Related Snippets
Similar patterns you can reuse in the same workflow.
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
AWS SDK v3 — S3 Operations in TypeScript
Perform S3 operations with AWS SDK v3: upload, download, list, presigned URLs, and multipart upload.
Best for: Server-side file uploads to S3
File Upload with Server Action
Handle file uploads using server actions with validation, size limits, and storage.
Best for: image uploads
Terraform AWS EC2 Instance Setup
Terraform configuration to provision an EC2 instance with VPC, security group, and SSH key pair.
Best for: Provisioning cloud infrastructure with code