Amazon S3
Core S3 operations for object storage: create buckets, put and get objects, list with pagination, presigned URLs, multipart uploads, and encryption.
Busque em todas as páginas da documentação
Core S3 operations for object storage: create buckets, put and get objects, list with pagination, presigned URLs, multipart uploads, and encryption.
Create a new bucket in the AWS region your client is configured for.
# --- Python (boto3) ---
import boto3
s3 = boto3.client("s3")
s3.create_bucket(Bucket="my-new-bucket")// --- TypeScript (AWS SDK v3) ---
import { S3Client, CreateBucketCommand } from "@aws-sdk/client-s3";
const s3 = new S3Client({});
await s3.send(new CreateBucketCommand({ Bucket: "my-new-bucket" }));Upload bytes or a string to a key in a bucket.
# --- Python (boto3) ---
import boto3
s3 = boto3.client("s3")
s3.put_object(Bucket="my-bucket", Key="hello.txt", Body=b"hello")// --- TypeScript (AWS SDK v3) ---
import { S3Client, PutObjectCommand } from "@aws-sdk/client-s3";
const s3 = new S3Client({});
await s3.send(new PutObjectCommand({ Bucket: "my-bucket", Key: "hello.txt", Body: "hello" }));Retrieve an object's content as bytes or a stream.
# --- Python (boto3) ---
import boto3
s3 = boto3.client("s3")
response = s3.get_object(Bucket="my-bucket", Key="hello.txt")
body = response["Body"].read() # bytes// --- TypeScript (AWS SDK v3) ---
import { S3Client, GetObjectCommand } from "@aws-sdk/client-s3";
const s3 = new S3Client({});
const response = await s3.send(new GetObjectCommand({ Bucket: "my-bucket", Key: "hello.txt" }));
const body = await response.Body.transformToByteArray();Upload a local file to S3 in one call.
# --- Python (boto3) ---
import boto3
s3 = boto3.client("s3")
s3.upload_file("/path/to/file.txt", "my-bucket", "remote-file.txt")// --- TypeScript (AWS SDK v3) ---
import { Upload } from "@aws-sdk/lib-storage";
import { S3Client } from "@aws-sdk/client-s3";
import { readFileSync } from "fs";
const s3 = new S3Client({});
await new Upload({ client: s3, params: { Bucket: "my-bucket", Key: "remote-file.txt", Body: readFileSync("/path/to/file.txt") } }).done();Write an S3 object to a local file.
# --- Python (boto3) ---
import boto3
s3 = boto3.client("s3")
s3.download_file("my-bucket", "remote-file.txt", "/path/to/file.txt")// --- TypeScript (AWS SDK v3) ---
import { S3Client, GetObjectCommand } from "@aws-sdk/client-s3";
import { writeFileSync } from "fs";
const s3 = new S3Client({});
const response = await s3.send(new GetObjectCommand({ Bucket: "my-bucket", Key: "remote-file.txt" }));
writeFileSync("/path/to/file.txt", await response.Body.transformToByteArray());Retrieve a paginated list of objects in a bucket.
# --- Python (boto3) ---
import boto3
s3 = boto3.client("s3")
paginator = s3.get_paginator("list_objects_v2")
for page in paginator.paginate(Bucket="my-bucket", Prefix="logs/"):
for obj in page.get("Contents", []):
print(obj["Key"])// --- TypeScript (AWS SDK v3) ---
import { S3Client, paginateListObjectsV2 } from "@aws-sdk/client-s3";
const s3 = new S3Client({});
const pages = paginateListObjectsV2({ client: s3, input: { Bucket: "my-bucket", Prefix: "logs/" } });
for await (const page of pages) {
page.Contents?.forEach(obj => console.log(obj.Key));
}Remove a single object from a bucket.
# --- Python (boto3) ---
import boto3
s3 = boto3.client("s3")
s3.delete_object(Bucket="my-bucket", Key="old-file.txt")// --- TypeScript (AWS SDK v3) ---
import { S3Client, DeleteObjectCommand } from "@aws-sdk/client-s3";
const s3 = new S3Client({});
await s3.send(new DeleteObjectCommand({ Bucket: "my-bucket", Key: "old-file.txt" }));Copy an object from one location to another within or across buckets.
# --- Python (boto3) ---
import boto3
s3 = boto3.client("s3")
s3.copy_object(CopySource={"Bucket": "source-bucket", "Key": "file.txt"}, Bucket="dest-bucket", Key: "file.txt")// --- TypeScript (AWS SDK v3) ---
import { S3Client, CopyObjectCommand } from "@aws-sdk/client-s3";
const s3 = new S3Client({});
await s3.send(new CopyObjectCommand({ CopySource: "source-bucket/file.txt", Bucket: "dest-bucket", Key: "file.txt" }));Check if an object exists or retrieve its metadata without downloading the body.
# --- Python (boto3) ---
import boto3
s3 = boto3.client("s3")
response = s3.head_object(Bucket="my-bucket", Key="file.txt")
print(response["ContentLength"]) # bytes// --- TypeScript (AWS SDK v3) ---
import { S3Client, HeadObjectCommand } from "@aws-sdk/client-s3";
const s3 = new S3Client({});
const response = await s3.send(new HeadObjectCommand({ Bucket: "my-bucket", Key: "file.txt" }));
console.log(response.ContentLength); // bytesGenerate a temporary URL for downloading an object without AWS credentials.
# --- Python (boto3) ---
import boto3
s3 = boto3.client("s3")
url = s3.generate_presigned_url("get_object", Params={"Bucket": "my-bucket", "Key": "file.txt"}, ExpiresIn=3600)// --- TypeScript (AWS SDK v3) ---
import { S3Client, GetObjectCommand } from "@aws-sdk/client-s3";
import { getSignedUrl } from "@aws-sdk/s3-request-presigner";
const s3 = new S3Client({});
const url = await getSignedUrl(s3, new GetObjectCommand({ Bucket: "my-bucket", Key: "file.txt" }), { expiresIn: 3600 });Create a temporary URL that allows uploading (putting) an object without credentials.
# --- Python (boto3) ---
import boto3
s3 = boto3.client("s3")
url = s3.generate_presigned_url("put_object", Params={"Bucket": "my-bucket", "Key": "file.txt"}, ExpiresIn=3600)// --- TypeScript (AWS SDK v3) ---
import { S3Client, PutObjectCommand } from "@aws-sdk/client-s3";
import { getSignedUrl } from "@aws-sdk/s3-request-presigner";
const s3 = new S3Client({});
const url = await getSignedUrl(s3, new PutObjectCommand({ Bucket: "my-bucket", Key: "file.txt" }), { expiresIn: 3600 });Initiate, upload parts, and complete a multipart upload for large objects.
# --- Python (boto3) ---
import boto3
s3 = boto3.client("s3")
mpu = s3.create_multipart_upload(Bucket="my-bucket", Key="large.bin")
part1 = s3.upload_part(Bucket="my-bucket", Key="large.bin", PartNumber=1, UploadId=mpu["UploadId"], Body=b"chunk1")
s3.complete_multipart_upload(Bucket="my-bucket", Key="large.bin", UploadId=mpu["UploadId"], MultipartUpload={"Parts": [{"ETag": part1["ETag"], "PartNumber": 1}]})// --- TypeScript (AWS SDK v3) ---
import { S3Client, CreateMultipartUploadCommand, UploadPartCommand, CompleteMultipartUploadCommand } from "@aws-sdk/client-s3";
const s3 = new S3Client({});
const mpu = await s3.send(new CreateMultipartUploadCommand({ Bucket: "my-bucket", Key: "large.bin" }));
const part1 = await s3.send(new UploadPartCommand({ Bucket: "my-bucket", Key: "large.bin", PartNumber: 1, UploadId: mpu.UploadId, Body: "chunk1" }));
await s3.send(new CompleteMultipartUploadCommand({ Bucket: "my-bucket", Key: "large.bin", UploadId: mpu.UploadId, MultipartUpload: { Parts: [{ ETag: part1.ETag, PartNumber: 1 }] } }));Assign custom metadata and content type when uploading an object.
# --- Python (boto3) ---
import boto3
s3 = boto3.client("s3")
s3.put_object(Bucket="my-bucket", Key="image.jpg", Body=b"...", ContentType="image/jpeg", Metadata={"custom-key": "value"})// --- TypeScript (AWS SDK v3) ---
import { S3Client, PutObjectCommand } from "@aws-sdk/client-s3";
const s3 = new S3Client({});
await s3.send(new PutObjectCommand({ Bucket: "my-bucket", Key: "image.jpg", Body: "...", ContentType: "image/jpeg", Metadata: { "custom-key": "value" } }));Encrypt objects at rest using AWS KMS.
# --- Python (boto3) ---
import boto3
s3 = boto3.client("s3")
s3.put_object(Bucket="my-bucket", Key="secret.txt", Body=b"secret", ServerSideEncryption="aws:kms", SSEKMSKeyId="arn:aws:kms:us-east-1:123456789012:key/...")// --- TypeScript (AWS SDK v3) ---
import { S3Client, PutObjectCommand } from "@aws-sdk/client-s3";
const s3 = new S3Client({});
await s3.send(new PutObjectCommand({ Bucket: "my-bucket", Key: "secret.txt", Body: "secret", ServerSideEncryption: "aws:kms", SSEKMSKeyId: "arn:aws:kms:us-east-1:123456789012:key/..." }));Poll until an S3 object becomes available.
# --- Python (boto3) ---
import boto3
s3 = boto3.client("s3")
waiter = s3.get_waiter("object_exists")
waiter.wait(Bucket="my-bucket", Key="file.txt")// --- TypeScript (AWS SDK v3) ---
import { S3Client, waitUntilObjectExists } from "@aws-sdk/client-s3";
const s3 = new S3Client({});
await waitUntilObjectExists({ client: s3, maxWaitTime: 60 }, { Bucket: "my-bucket", Key: "file.txt" });Stack versions: This page was written for boto3 1.43.x (Python 3.10+) and the AWS SDK for JavaScript v3 (Node.js 18+).
Revisado por Chris St. John·Última atualização: 23 de jul. de 2026