import { S3Client, PutObjectCommand, GetObjectCommand } from '@aws-sdk/client-s3'; import { randomUUID } from 'crypto'; import { expect, test } from 'vitest'; import 'dotenv/config'; const s3 = new S3Client({ region: 'us-east-1', endpoint: process.env.S3_ENDPOINT, credentials: { accessKeyId: process.env.S3_ACCESS_KEY!, secretAccessKey: process.env.S3_SECRET_KEY!, }, forcePathStyle: true, }); const BUCKET = process.env.S3_BUCKET!; test('can upload and download a file from S3', async () => { const key = `test-file-${randomUUID()}.txt`; const content = 'hello s3 world!'; // Upload await s3.send(new PutObjectCommand({ Bucket: BUCKET, Key: key, Body: content, })); // Download const getObj = await s3.send(new GetObjectCommand({ Bucket: BUCKET, Key: key, })); const downloaded = await streamToString(getObj.Body); expect(downloaded).toBe(content); }); function streamToString(stream: any): Promise { return new Promise((resolve, reject) => { const chunks: any[] = []; stream.on('data', (chunk: any) => chunks.push(chunk)); stream.on('error', reject); stream.on('end', () => resolve(Buffer.concat(chunks).toString('utf-8'))); }); }