Working with S3 and Cloud Storage in TypeScript: Libraries, Setup, and Code Examples
Cloud object storage compatible with the Amazon S3 API has become the de facto standard for storing and delivering files in modern web applications. In the TypeScript and Node.js ecosystem, there are several powerful tools for working with S3. In this article, we will cover the main libraries, their setup, and provide practical code examples for typical tasks: uploading, downloading, deleting files, and managing buckets.
Main Libraries for Working with S3 in TypeScript
The most popular and official solution is the AWS SDK for JavaScript v3. This library is completely rewritten in TypeScript, modular, and supports tree-shaking. An alternative is the MinIO JavaScript Client — a lightweight library fully compatible with the S3 API, which is great for working with local or self-hosted S3-compatible storage (e.g., MinIO, Ceph, DigitalOcean Spaces).
For simple scenarios, you can also use @aws-sdk/client-s3 — a separate package from the AWS SDK v3 that contains only the necessary functionality for S3.
Installation and Initialization
Install the main package for working with S3 via AWS SDK v3:
npm install @aws-sdk/client-s3 @aws-sdk/credential-providersFor working with MinIO:
npm install minioExample of initializing an S3 client using AWS SDK v3:
import { S3Client } from "@aws-sdk/client-s3";import { fromEnv } from "@aws-sdk/credential-providers";
const s3Client = new S3Client({ region: "us-east-1", credentials: fromEnv(), // uses AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY from environment variables});The MinIO client is configured as follows:
import * as Minio from "minio";
const minioClient = new Minio.Client({ endPoint: "play.min.io", port: 9000, useSSL: true, accessKey: "YOUR_ACCESS_KEY", secretKey: "YOUR_SECRET_KEY",});Typing and Strict Types
One of the main advantages of TypeScript is strict typing. Both libraries provide complete types for all requests and responses. For example, when uploading a file to S3, you get autocompletion for all parameters:
import { PutObjectCommand } from "@aws-sdk/client-s3";
const uploadParams: PutObjectCommand["input"] = { Bucket: "my-bucket", Key: "uploads/photo.jpg", Body: fileBuffer, ContentType: "image/jpeg", ACL: "public-read",};
const command = new PutObjectCommand(uploadParams);const result = await s3Client.send(command);Main Operations: Upload, Download, Delete
1. Upload File
import { PutObjectCommand } from "@aws-sdk/client-s3";
async function uploadFile(bucket: string, key: string, body: Buffer | Uint8Array | string) { const command = new PutObjectCommand({ Bucket: bucket, Key: key, Body: body, }); return await s3Client.send(command);}
// Usage exampleconst buffer = Buffer.from("Hello, S3!");await uploadFile("my-bucket", "hello.txt", buffer);2. Download File
import { GetObjectCommand } from "@aws-sdk/client-s3";
async function downloadFile(bucket: string, key: string): Promise<Buffer> { const command = new GetObjectCommand({ Bucket: bucket, Key: key }); const response = await s3Client.send(command); return Buffer.from(await response.Body!.transformToByteArray());}
// Exampleconst fileContent = await downloadFile("my-bucket", "hello.txt");console.log(fileContent.toString());3. Delete File
import { DeleteObjectCommand } from "@aws-sdk/client-s3";
async function deleteFile( Also in library
PDF Generation in JavaScript (Node.js): Libraries and Tools for Creating PDF
PM2: Monitoring and Managing JavaScript Applications
Monitoring TypeScript Applications: Tools and Practices
PDF Generation in Kotlin: Libraries and Tools (iText, Apache PDFBox)