Skip to content
GitHubXDiscordRSS

R2Object

Creates and manages individual objects within Cloudflare R2 Buckets.

Create a text object in an R2 bucket:

import { R2Bucket, R2Object } from "alchemy/cloudflare";
const bucket = await R2Bucket("my-bucket", {
name: "my-bucket",
});
const textObject = await R2Object("readme", {
bucket: bucket,
key: "README.txt",
content: "Hello, world!",
});

Store JSON configuration:

const configObject = await R2Object("config", {
bucket: bucket,
key: "config/app.json",
content: JSON.stringify({ version: "1.0.0" }),
});

Store binary content:

const imageObject = await R2Object("avatar", {
bucket: bucket,
key: "images/avatar.png",
content: imageBuffer, // ArrayBuffer
});
  • Type: R2Bucket
  • Description: The R2 bucket where the object will be stored
  • Type: string
  • Description: The object key/path within the bucket
  • Type: ReadableStream | ArrayBuffer | ArrayBufferView | string | null | Blob
  • Description: The content to store in the object