S3
R2 provides support for a S3-compatible API, which means you can use any S3 SDK, library, or tool to interact with your buckets. If you have existing code that works with S3, you can use it with R2 by changing the endpoint URL.
A bucket stores your objects in R2. To create a new R2 bucket:
-
Log in to your Cloudflare account:
Terminal window npx wrangler login -
Create a bucket named
my-bucket:Terminal window npx wrangler r2 bucket create my-bucketIf prompted, select the account you want to create the bucket in.
-
Verify the bucket was created:
Terminal window npx wrangler r2 bucket list
-
In the Cloudflare Dashboard, go to R2 object storage.
Go to Overview -
Select Create bucket.
-
Enter a name for your bucket.
-
Select a location for your bucket and a default storage class.
-
Select Create bucket.
To use the S3 API, you need to generate credentials and get an Access Key ID and Secret Access Key:
- Go to the Cloudflare Dashboard ↗.
- Select Storage & databases > R2 > Overview.
- Select Manage in API Tokens.
- Select Create Account API token or Create User API token
- Choose Object Read & Write permission and Apply to specific buckets only to select the buckets you want to access.
- Select Create API Token.
- Copy the Access Key ID and Secret Access Key. Store these securely as you cannot view the secret again.
You also need your S3 API endpoint URL which you can find at the bottom of the Create API Token confirmation page once you have created your token, or on the R2 Overview page:
https://<ACCOUNT_ID>.r2.cloudflarestorage.comThe following examples show how to use Python and JavaScript SDKs. For other languages, refer to S3-compatible SDK examples for Go, Java, PHP, Ruby, and Rust.
-
Install boto3 ↗:
Terminal window pip install boto3 -
Create a test file to upload:
Terminal window echo 'Hello, R2!' > myfile.txt -
Use your credentials to create an S3 client and interact with your bucket:
Python import boto3s3 = boto3.client(service_name='s3',# Provide your R2 endpoint: https://<ACCOUNT_ID>.r2.cloudflarestorage.comendpoint_url='https://<ACCOUNT_ID>.r2.cloudflarestorage.com',# Provide your R2 Access Key ID and Secret Access Keyaws_access_key_id='<ACCESS_KEY_ID>',aws_secret_access_key='<SECRET_ACCESS_KEY>',region_name='auto', # Required by boto3, not used by R2)# Upload a files3.upload_file('myfile.txt', 'my-bucket', 'myfile.txt')print('Uploaded myfile.txt')# Download a files3.download_file('my-bucket', 'myfile.txt', 'downloaded.txt')print('Downloaded to downloaded.txt')# List objectsresponse = s3.list_objects_v2(Bucket='my-bucket')for obj in response.get('Contents', []):print(f"Object: {obj['Key']}") -
Save this as
example.pyand run it:Terminal window python example.pyUploaded myfile.txtDownloaded to downloaded.txtObject: myfile.txt
Refer to boto3 examples for more operations.
-
Install the @aws-sdk/client-s3 ↗ package:
Terminal window npm install @aws-sdk/client-s3 -
Use your credentials to create an S3 client and interact with your bucket:
JavaScript import {S3Client,PutObjectCommand,GetObjectCommand,ListObjectsV2Command,} from "@aws-sdk/client-s3";const s3 = new S3Client({region: "auto", // Required by AWS SDK, not used by R2// Provide your R2 endpoint: https://<ACCOUNT_ID>.r2.cloudflarestorage.comendpoint: "https://<ACCOUNT_ID>.r2.cloudflarestorage.com",credentials: {// Provide your R2 Access Key ID and Secret Access KeyaccessKeyId: "<ACCESS_KEY_ID>",secretAccessKey: "<SECRET_ACCESS_KEY>",},});// Upload a fileawait s3.send(new PutObjectCommand({Bucket: "my-bucket",Key: "myfile.txt",Body: "Hello, R2!",}),);console.log("Uploaded myfile.txt");// Download a fileconst response = await s3.send(new GetObjectCommand({Bucket: "my-bucket",Key: "myfile.txt",}),);const content = await response.Body.transformToString();console.log("Downloaded:", content);// List objectsconst list = await s3.send(new ListObjectsV2Command({Bucket: "my-bucket",}),);console.log("Objects:",list.Contents.map((obj) => obj.Key),); -
Save this as
example.mjsand run it:Terminal window node example.mjsUploaded myfile.txtDownloaded: Hello, R2!Objects: [ 'myfile.txt' ]
Refer to AWS SDK for JavaScript examples for more operations.
Was this helpful?
- Resources
- API
- New to Cloudflare?
- Directory
- Sponsorships
- Open Source
- Support
- Help Center
- System Status
- Compliance
- GDPR
- Company
- cloudflare.com
- Our team
- Careers
- © 2026 Cloudflare, Inc.
- Privacy Policy
- Terms of Use
- Report Security Issues
- Trademark
-