TypeScript API / laikacms/assets/r2 / R2AssetsDataSource
Class: R2AssetsDataSource
R2AssetsDataSource provides low-level operations for binary assets in Cloudflare R2. Unlike the storage datasource which handles text serialization, this handles raw binary data.
Constructors
Constructor
new R2AssetsDataSource(bucket): R2AssetsDataSource;Parameters
| Parameter | Type |
|---|---|
bucket | R2Bucket |
Returns
R2AssetsDataSource
Methods
abortMultipartUpload()
abortMultipartUpload(key, uploadId): Promise<LaikaResult<void>>;Abort a multipart upload
Parameters
| Parameter | Type |
|---|---|
key | string |
uploadId | string |
Returns
Promise<LaikaResult<void>>
completeMultipartUpload()
completeMultipartUpload(
key,
uploadId,
parts
): Promise<LaikaResult<R2AssetMeta>>;Complete a multipart upload
Parameters
| Parameter | Type |
|---|---|
key | string |
uploadId | string |
parts | object[] |
Returns
Promise<LaikaResult<R2AssetMeta>>
createFolder()
createFolder(key): Promise<LaikaResult<void>>;Create an empty folder (using .keep file)
Parameters
| Parameter | Type |
|---|---|
key | string |
Returns
Promise<LaikaResult<void>>
deleteFolder()
deleteFolder(key, recursive?): Promise<LaikaResult<void>>;Delete a folder and optionally all its contents
Parameters
| Parameter | Type | Default value |
|---|---|---|
key | string | undefined |
recursive | boolean | false |
Returns
Promise<LaikaResult<void>>
deleteObject()
deleteObject(key): Promise<LaikaResult<void>>;Delete an object
Parameters
| Parameter | Type |
|---|---|
key | string |
Returns
Promise<LaikaResult<void>>
deleteObjects()
deleteObjects(keys): AsyncGenerator<LaikaResult<string>>;Delete multiple objects
Parameters
| Parameter | Type |
|---|---|
keys | readonly string[] |
Returns
AsyncGenerator<LaikaResult<string>>
exists()
exists(key): Promise<boolean>;Check if an object exists
Parameters
| Parameter | Type |
|---|---|
key | string |
Returns
Promise<boolean>
getFolderMeta()
getFolderMeta(key): Promise<LaikaResult<{
createdAt: Date;
updatedAt: Date;
}>>;Get folder metadata (for R2, folders don't have real metadata)
Parameters
| Parameter | Type |
|---|---|
key | string |
Returns
Promise<LaikaResult<{ createdAt: Date; updatedAt: Date; }>>
getObjectBody()
getObjectBody(key): Promise<LaikaResult<{
body: ArrayBuffer;
meta: R2AssetMeta;
}>>;Get object body as ArrayBuffer
Parameters
| Parameter | Type |
|---|---|
key | string |
Returns
Promise<LaikaResult<{ body: ArrayBuffer; meta: R2AssetMeta; }>>
getObjectMeta()
getObjectMeta(key): Promise<LaikaResult<R2AssetMeta>>;Get object metadata without fetching the body
Parameters
| Parameter | Type |
|---|---|
key | string |
Returns
Promise<LaikaResult<R2AssetMeta>>
getObjectStream()
getObjectStream(key): Promise<LaikaResult<{
meta: R2AssetMeta;
stream: ReadableStream;
}>>;Get object body as ReadableStream
Parameters
| Parameter | Type |
|---|---|
key | string |
Returns
Promise<LaikaResult<{ meta: R2AssetMeta; stream: ReadableStream; }>>
isDirectory()
isDirectory(key): Promise<boolean>;Check if a key represents a directory (has objects with that prefix)
Parameters
| Parameter | Type |
|---|---|
key | string |
Returns
Promise<boolean>
listDirectory()
listDirectory(prefix, options?): Promise<LaikaResult<R2AssetEntry[]>>;List objects in a "directory" (prefix)
Parameters
| Parameter | Type |
|---|---|
prefix | string |
options? | { includeMetadata?: boolean; } |
options.includeMetadata? | boolean |
Returns
Promise<LaikaResult<R2AssetEntry[]>>
listPage()
listPage(prefix, options?): Promise<LaikaResult<{
cursor?: string;
entries: R2AssetEntry[];
}>>;List one native R2 page of objects under a prefix, recursively (no delimiter), surfacing R2's own continuation cursor instead of draining. Returns only files (.keep folder markers are skipped, so a page may carry slightly fewer entries than limit); a returned cursor means the listing is truncated and can be resumed from that exact position.
Parameters
| Parameter | Type |
|---|---|
prefix | string |
options? | { cursor?: string; includeMetadata?: boolean; limit?: number; } |
options.cursor? | string |
options.includeMetadata? | boolean |
options.limit? | number |
Returns
Promise<LaikaResult<{ cursor?: string; entries: R2AssetEntry[]; }>>
putObject()
putObject(
key,
body,
options?
): Promise<LaikaResult<R2AssetMeta>>;Put an object (create or update)
Parameters
| Parameter | Type |
|---|---|
key | string |
body | | string | ReadableStream<any> | ArrayBuffer | Uint8Array<ArrayBufferLike> | ArrayBufferView<ArrayBufferLike> |
options? | { cacheControl?: string; contentType?: string; customMetadata?: Record<string, string>; } |
options.cacheControl? | string |
options.contentType? | string |
options.customMetadata? | Record<string, string> |
Returns
Promise<LaikaResult<R2AssetMeta>>
startMultipartUpload()
startMultipartUpload(key, options?): Promise<LaikaResult<{
uploadId: string;
}>>;Start a multipart upload
Parameters
| Parameter | Type |
|---|---|
key | string |
options? | { contentType?: string; customMetadata?: Record<string, string>; } |
options.contentType? | string |
options.customMetadata? | Record<string, string> |
Returns
Promise<LaikaResult<{ uploadId: string; }>>
uploadPart()
uploadPart(
key,
uploadId,
partNumber,
body
): Promise<LaikaResult<{
etag: string;
partNumber: number;
}>>;Upload a part in a multipart upload
Parameters
| Parameter | Type |
|---|---|
key | string |
uploadId | string |
partNumber | number |
body | | string | ReadableStream<any> | ArrayBuffer | Uint8Array<ArrayBufferLike> | ArrayBufferView<ArrayBufferLike> |
Returns
Promise<LaikaResult<{ etag: string; partNumber: number; }>>