Cloudflare Docs
R2
Visit R2 on GitHub
Set theme to dark (⇧+D)

Configure `aws-sdk-js` for R2

Example of how to configure `aws-sdk-js` to use R2.
You must generate an Access Key before getting started. All examples will utilize access_key_id and access_key_secret variables which represent the Access Key ID and Secret Access Key values you generated.

JavaScript or TypeScript users may continue to use the aws-sdk npm package as per normal. You must pass in the R2 configuration credentials when instantiating your S3 service client:

import S3 from 'aws-sdk/clients/s3.js';
const s3 = new S3({
endpoint: `https://${accountid}.r2.cloudflarestorage.com`,
accessKeyId: `${access_key_id}`,
secretAccessKey: `${access_key_secret}`,
signatureVersion: 'v4',
});
console.log(
await s3.listBuckets().promise()
);
//=> {
//=> Buckets: [
//=> { Name: 'user-uploads', CreationDate: 2022-04-13T21:23:47.102Z },
//=> { Name: 'my-bucket-name', CreationDate: 2022-05-07T02:46:49.218Z }
//=> ],
//=> Owner: {
//=> DisplayName: '...',
//=> ID: '...'
//=> }
//=> }
console.log(
await s3.listObjects({ Bucket: 'my-bucket-name' }).promise()
);
//=> {
//=> IsTruncated: false,
//=> Name: 'my-bucket-name',
//=> CommonPrefixes: [],
//=> MaxKeys: 1000,
//=> Contents: [
//=> {
//=> Key: 'cat.png',
//=> LastModified: 2022-05-07T02:50:45.616Z,
//=> ETag: '"c4da329b38467509049e615c11b0c48a"',
//=> ChecksumAlgorithm: [],
//=> Size: 751832,
//=> Owner: [Object]
//=> },
//=> {
//=> Key: 'todos.txt',
//=> LastModified: 2022-05-07T21:37:17.150Z,
//=> ETag: '"29d911f495d1ba7cb3a4d7d15e63236a"',
//=> ChecksumAlgorithm: [],
//=> Size: 279,
//=> Owner: [Object]
//=> }
//=> ]
//=> }

You can also generate presigned links that can be used to share public read or write access to a bucket temporarily.

// Use the expires property to determine how long the presigned link is valid.
console.log(
await s3.getSignedUrlPromise('getObject', { Bucket: 'my-bucket-name', Key: 'dog.png', Expires: 3600 })
)
// https://my-bucket-name.<accountid>.r2.cloudflarestorage.com/dog.png?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=<credential>&X-Amz-Date=<timestamp>&X-Amz-Expires=3600&X-Amz-Signature=<signature>&X-Amz-SignedHeaders=host
// You can also create links for operations such as putObject to allow temporary write access to a specific key.
console.log(
await s3.getSignedUrlPromise('putObject', { Bucket: 'my-bucket-name', Key: 'dog.png', Expires: 3600 })
)

You can use the link generated by the putObject example to upload to the specified bucket and key, until the presigned link expires.

$ curl -X PUT https://my-bucket-name.<accountid>.r2.cloudflarestorage.com/dog.png?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=<credential>&X-Amz-Date=<timestamp>&X-Amz-Expires=3600&X-Amz-Signature=<signature>&X-Amz-SignedHeaders=host -F "data=@dog.png"