S3
Simple Storage Service (S3) is an object storage service, which was one of the earliest services offered by AWS, it is now a cornerstone of cloud storage due to its simplicity, scalability, and durability.
It provides a simple interface for storing and retrieving any amount of data.
Creating a Bucket
To get started, we need to create(make) a bucket, the bucket namespace is shared by all users, so you need a very unique name:
$ aws s3 mb s3://your-unique-bucket-name
Uploading Files
$ aws s3 cp file.txt s3://your-unique-bucket-name/
Listing Files
$ aws s3 ls s3://your-unique-bucket-name
Output:
2024-10-30 10:22:11 115 file.txt
Downloading Files
$ aws s3 cp s3://your-unique-bucket-name/file.txt download.txt
Using the SDK
I will use bun for simplicity:
Install the SDK:
$ bun add @aws-sdk/client-s3
Add s3.ts with the following content:
import { S3Client, GetObjectCommand, PutObjectCommand } from '@aws-sdk/client-s3'
import fs from 'node:fs'
const s3 = new S3Client()
const Bucket = 'wdgfs'
await s3.send(
new PutObjectCommand({
Bucket,
Key: "s3.ts",
Body: fs.readFileSync("./s3.ts"),
}),
);
const { Body } = await s3.send(
new GetObjectCommand({
Bucket,
Key: "s3.ts",
}),
)
console.log(await Body.transformToString())
Credentials
You might noticed that not credentials are provided in the code, that's
because the SDK will retrieves them from ~/.aws/credentials.
Run your script with:
$ bun s3.ts
This covers the very basics of S3.