Skip to content
AnswerWell geo Documentation

Research · 8 min read · Answerwell

S3-Compatible Object Storage: API Tokens, Presigned URLs, and Temporary Credentials

How Cloudflare R2's S3 API works: tokens, endpoints, presigned URLs, temporary credentials, Workers bindings, and common setup mistakes.

This page summarizes Cloudflare documentation on Cloudflare R2: how to choose an access path, authenticate for the S3-compatible API, and grant time-limited access with presigned URLs or temporary credentials—without exposing long-lived secrets to browsers or untrusted clients.

R2 API surfaces: when to use Workers, S3, or the REST API

Section titled “R2 API surfaces: when to use Workers, S3, or the REST API”

R2 exposes three ways to interact with buckets and objects. Picking the wrong one leads to rate limits, failed auth, or unnecessary credential handling.

Method Use when
Workers API Code on Cloudflare Workers (or Pages Functions) that reads, writes, or lists objects via an R2 binding (env.MY_BUCKET)
S3-compatible API Existing S3 SDKs, CLI tools (aws, rclone), or high-throughput object workloads
Cloudflare REST API (api.cloudflare.com) Dashboard/Wrangler-style bucket management; rate limits apply—prefer S3 or Workers for heavy object traffic

R2 provides three API surfaces for interacting with your data:

Workers API: An in-Worker API accessed by binding an R2 bucket to a Worker. Use the Workers API to read, write, and list objects from within a Worker. S3-compatible API: An S3-compatible HTTP API available at https://<ACCOUNT_ID>.r2.cloudflarestorage.com. Use existing S3 SDKs and tools to interact with R2. Cloudflare REST API: The api.cloudflare.com REST API used by the Cloudflare Dashboard and Wrangler CLI. Supports bucket management and object operations. Rate limits apply. Use the S3-compatible API or Workers API for high-throughput workloads.

— Cloudflare, API

For Workers and Pages on the same Cloudflare account, R2 bindings avoid managing access keys in the Worker: the runtime injects a bucket handle (for example MY_BUCKET) from your Wrangler configuration. For uploads from end-user browsers, the usual pattern is a trusted server that mints a presigned URL or temporary credentials rather than shipping parent API tokens to the client.

R2 API tokens: create Access Key ID and Secret Access Key for S3 clients

Section titled “R2 API tokens: create Access Key ID and Secret Access Key for S3 clients”

S3 clients do not use your Cloudflare dashboard login. They use an R2 API token, which maps to S3 Access Key ID and Secret Access Key values.

You must purchase R2 before generating tokens. In the dashboard, open R2 → Overview → Manage next to API Tokens, then create an Account or User API token with the permissions you need. For object permissions you can scope tokens to specific buckets.

You can generate an API token to serve as the Access Key for usage with existing S3-compatible SDKs or XML APIs.

— Cloudflare, Authentication

Configure every S3 client with:

  • Endpoint: https://<ACCOUNT_ID>.r2.cloudflarestorage.com (see Authentication)
  • Region: auto (required by many SDKs but not used by R2)

Jurisdictional buckets must use jurisdiction-specific endpoints—for example https://<ACCOUNT_ID>.eu.r2.cloudflarestorage.com for EU—and generally require one S3 client per jurisdiction, because most clients accept a single endpoint.

Object-level tokens vs the Cloudflare REST API

Section titled “Object-level tokens vs the Cloudflare REST API”

If you create a token with Object Read or Object Read and Write, use it only against the S3-compatible API (AWS SigV4). Calls to api.cloudflare.com for object operations fail with 401 or 403. See Troubleshooting.

Object-level tokens are only supported by the S3-compatible API, which authenticates with AWS Signature Version 4 (SigV4).

— Cloudflare, Troubleshooting

S3 client setup: endpoint, region auto, and SDK-specific fixes

Section titled “S3 client setup: endpoint, region auto, and SDK-specific fixes”

After generating credentials, point your SDK at the R2 endpoint. Environment-variable style setup is documented in Authenticate against R2 API using auth tokens:

export AWS_REGION=auto
export AWS_ENDPOINT_URL=https://<account_id>.r2.cloudflarestorage.com
export AWS_ACCESS_KEY_ID=your_access_key_id
export AWS_SECRET_ACCESS_KEY=your_secret_access_key

Language-specific walkthroughs live under Examples (AWS CLI, boto3, aws-sdk-js-v3, aws-sdk-go, aws4fetch, and others).

SDK pitfalls that cause HTTP 403 on upload

Section titled “SDK pitfalls that cause HTTP 403 on upload”

Some AWS SDK defaults assume full Amazon S3 behavior:

SDK Issue Fix
AWS SDK for Java v2 Chunked transfer encoding on putObject breaks signatures Set chunkedEncodingEnabled(false) and often pathStyleAccessEnabled(true)—see aws-sdk-java
AWS SDK for .NET Streaming SigV4 not supported by R2 Set DisablePayloadSigning = true and DisableDefaultChecksumValidation = true on PutObject—see aws-sdk-net
@aws-sdk/client-s3 in Workers DOMParser errors on XML operations when bundled Add nodejs_compat and alias @aws-sdk/xml-builder per aws-sdk-js-v3; for simple reads/writes, prefer the R2 binding or aws4fetch

Presigned URLs: single-operation temporary HTTP access

Section titled “Presigned URLs: single-operation temporary HTTP access”

Presigned URLs grant one S3 operation on one object until expiry, with credentials embedded in the query string—ideal for browser uploads, shareable download links, or any HTTP client without an S3 SDK.

Presigned URLs are an S3 concept for granting temporary access to objects without exposing your API credentials. A presigned URL includes signature parameters in the URL itself, authorizing anyone with the URL to perform a specific operation (like GetObject or PutObject) on a specific object until the URL expires.

— Cloudflare, Presigned URLs

You choose account ID, bucket, object key, operation (GET, PUT, HEAD, DELETE), and expiry (1 second to 7 days). Signing uses AWS Signature Version 4 locally; R2 is not contacted to generate the URL.

For providing secure access to bucket objects for anonymous users, we recommend using pre-signed URLs instead.

— Cloudflare, Authenticate against R2 API using auth tokens

The R2 Workers binding cannot mint presigned URLs; sign with aws4fetch or the AWS SDK from a Worker, typically with keys stored as Wrangler secrets. Example pattern for a PUT URL:

import { getSignedUrl } from "@aws-sdk/s3-request-presigner";
import { PutObjectCommand } from "@aws-sdk/client-s3";
// S3 client configured with R2 endpoint and token credentials
const putUrl = await getSignedUrl(S3, new PutObjectCommand({
Bucket: "my-bucket", Key: "image.png", ContentType: "image/png",
}), { expiresIn: 3600 });

Presigned URLs work only on *.r2.cloudflarestorage.com, not on custom domains; see Presigned URLs — Custom domains.

Treat presigned URLs as bearer tokens: anyone with the link can run the signed operation until expiration (Presigned URLs — Security considerations).

Pattern Grants Good for
Presigned URLs One operation on one object Direct browser or mobile HTTP to R2 without exposing keys
Temporary credentials Multiple scoped S3 operations on one bucket (optional path limits) Clients using a full S3 SDK for a short session

Temporary credentials: scoped S3 sessions without long-lived keys

Section titled “Temporary credentials: scoped S3 sessions without long-lived keys”

Temporary credentials are short-lived SigV4 credentials derived from a parent R2 API token. They include accessKeyId, secretAccessKey, and sessionToken (sent as X-Amz-Security-Token).

Temporary credentials are short-lived, scoped S3 credentials derived from an existing R2 API token. They authenticate with AWS Signature Version 4, the same as a long-lived token, but include a session token and expire automatically.

— Cloudflare, Temporary credentials

Mint them either:

  1. Temporary Credentials API on a trusted server (POST to .../accounts/<ACCOUNT_ID>/r2/temp-access-credentials with a Bearer parent token), or
  2. Local JWT signing with the parent secret—for high volume or when the API is unreachable; never run local signing in an untrusted client (Authenticate against R2 with temporary credentials).

A temporary credential cannot exceed its parent token’s permissions; revoking the parent invalidates derived creds immediately.

Never ship parent credentials to a client.

— Cloudflare, Authenticate against R2 with temporary credentials

CORS and Content-Type: browser uploads with presigned PUT URLs

Section titled “CORS and Content-Type: browser uploads with presigned PUT URLs”

Browsers enforce CORS even when the presigned URL signature is valid. Without a matching bucket CORS policy, uploads from a web app fail at the preflight step. Configure rules in the dashboard or via S3-compatible tooling; see Configure CORS and Use CORS with a presigned URL.

When a browser makes a request to a presigned URL on a different origin, the browser enforces CORS. Without a CORS policy, browser-based uploads and downloads using presigned URLs will fail, even though the presigned URL itself is valid.

— Cloudflare, Configure CORS

Restrict uploads by signing Content-Type in the PutObject request (SDK ContentType or aws4fetch with allHeaders: true). The client must send the same Content-Type header or R2 returns signature errors—described in Presigned URLs — Best practices and aws4fetch.

Expired presigned URLs return 403; R2 may omit CORS headers on those responses, so client-side code cannot read the error body—refresh URLs before expiry or proxy through your backend (Configure CORS).

Workers R2 binding: in-Worker object API without S3 keys

Section titled “Workers R2 binding: in-Worker object API without S3 keys”

Binding a bucket in Wrangler gives a typed R2Bucket with get, put, list, delete, and multipart helpers—see Workers API reference and Use R2 from Workers.

{
"r2_buckets": [
{ "binding": "MY_BUCKET", "bucket_name": "my-bucket" }
]
}

By default, wrangler dev uses local R2 simulation (data under .wrangler/state); production traffic uses the real bucket unless you enable remote bindings. A binding alone does not secure the bucket—implement auth in Worker code (Use R2 from Workers — Bucket access and privacy).

For CLI and local testing without a Worker, Wrangler R2 commands and wrangler r2 object put with --local support local data (Adding local data).

  • Using object-scoped tokens against the REST API instead of the S3 endpoint (Troubleshooting).
  • Expecting presigned URLs on a custom domain—they require the S3 API hostname (Presigned URLs).
  • Browser PUT without bucket CORS or with wrong Content-Type vs the signed headers (Configure CORS).
  • Putting parent Access Key / Secret in frontend or mobile apps—use presigned URLs or temporary credentials minted server-side.
  • Assuming the Workers binding can generate presigned URLs—it cannot; use signing libraries with stored secrets (Presigned URLs — Prerequisites).
  • Wrong jurisdiction endpoint for EU/FedRAMP/US jurisdictional buckets (Authentication).
  • AWS CLI presign only supports GET; use an SDK for PUT presigns (Presigned URLs).
  1. In the Cloudflare dashboard, confirm an active R2 subscription and that your bucket exists under R2 Overview.
  2. Create or verify an R2 API token via Authentication, then run aws s3api list-buckets --endpoint-url https://<ACCOUNT_ID>.r2.cloudflarestorage.com as in AWS CLI (expect HTTP 200 and your bucket names).
  3. Optional: in Postman, set account-id, r2-access-key-id, and r2-secret-access-key, then send GET ListBuckets from the public R2 collection.
  4. If browsers upload via presigned PUT, confirm bucket CORS in the dashboard per Configure CORS and retry from the app origin.
  5. You can check automatically in Answerwell (our tool): https://answerwell.org/workspace

If something on this page is wrong or unclear, email yimasynthetic@gmail.com with the page URL and the exact statement you are asking about.