typescriptintermediate

S3 Presigned URL Generator

Generate secure presigned URLs for S3 upload and download operations with expiry and content type.

typescript
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.