R2Bucket
Creates and manages Cloudflare R2 Buckets for object storage with S3-compatible API.
Props
Name of the bucket. Names can only contain lowercase letters (a-z), numbers (0-9), and hyphens (-).
${app}-${stage}-${id}
Optional location hint for the bucket. Indicates the primary geographical location data will be accessed from.
storageClass
Standard | InfrequentAccess
Optional storage class for the bucket.
Optional jurisdiction for the bucket. Determines the regulatory jurisdiction the bucket data falls under.
Whether to allow public access through the r2.dev subdomain. Only for development purposes - use custom domains for production.
domains
string | R2BucketCustomDomainOptions | (string | R2BucketCustomDomainOptions)[]
The custom domain(s) to attach to the bucket.
Whether to delete the bucket when the resource is removed from Alchemy.
Whether to empty the bucket and delete all objects during resource deletion.
Whether to adopt an existing bucket with the same name if it exists.
CORS rules for the bucket.
HTTP methods allowed. Options: GET, PUT, POST, DELETE, HEAD
Allowed origins for CORS requests.
Allowed headers for CORS requests.
Headers that can be exposed back to the client.
Amount of time browsers are allowed to cache CORS preflight responses (in seconds).
Lifecycle rules for the bucket.
Unique identifier for this rule.
Whether or not this rule is in effect.
Transitions will only apply to objects/uploads that start with the given prefix.
Transition to delete objects.
Age or date-based condition for deletion.
Transitions to change the storage class of objects.
Enable data catalog for bucket.
Output
Time at which the bucket was created.
The r2.dev subdomain for the bucket, if devDomain is true.
The custom domains for the bucket, if applicable.
Type identifier for the binding.
Examples
Basic R2 Bucket
const bucket = await R2Bucket("storage", {
name: "my-app-data"
});
Bucket with Location Hint
const euBucket = await R2Bucket("eu-data", {
name: "eu-user-data",
locationHint: "eu",
jurisdiction: "eu"
});
Bucket with Public Access
const publicBucket = await R2Bucket("public-assets", {
name: "public-assets",
devDomain: true
});
Bucket with CORS
const bucket = await R2Bucket("api-storage", {
cors: [{
allowed: {
methods: ["GET", "PUT"],
origins: ["https://example.com"],
headers: ["Content-Type"]
},
exposeHeaders: ["ETag"],
maxAgeSeconds: 3600
}]
});
Bucket with Lifecycle Rules
const bucket = await R2Bucket("logs", {
lifecycle: [{
id: "delete-old-logs",
enabled: true,
conditions: { prefix: "logs/" },
deleteObjectsTransition: {
condition: { maxAge: 2592000, type: "Age" }
}
}]
});
Auto-Empty on Deletion
const tempBucket = await R2Bucket("temp-storage", {
name: "temp-storage",
empty: true
});