UNPKG

cloudflare

Version:

The official TypeScript library for the Cloudflare API

168 lines • 7.26 kB
// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details. import { APIResource } from "../../../core/resource.mjs"; import { CursorPagination } from "../../../core/pagination.mjs"; import { buildHeaders } from "../../../internal/headers.mjs"; import { path } from "../../../internal/utils/path.mjs"; export var BaseObjects = /* @__PURE__ */ (() => { class BaseObjects extends APIResource { /** * Lists objects in an R2 bucket. Returns object metadata including key, size, * etag, last modified date, HTTP metadata, and custom metadata. * * For most workloads, we recommend using R2's * [S3-compatible API](https://developers.cloudflare.com/r2/api/s3/api/) or a * [Worker with an R2 binding](https://developers.cloudflare.com/r2/api/workers/workers-api-reference/) * instead. * * @example * ```ts * // Automatically fetches more pages as needed. * for await (const objectListResponse of client.r2.buckets.objects.list( * 'example-bucket', * { account_id: '023e105f4ecef8ad9ca31a8372d0c353' }, * )) { * // ... * } * ``` */ list(bucketName, params, options) { const { account_id, 'cf-r2-jurisdiction': cfR2Jurisdiction, ...query } = params; return this._client.getAPIList(path `/accounts/${account_id}/r2/buckets/${bucketName}/objects`, (CursorPagination), { query, ...options, headers: buildHeaders([ { ...(cfR2Jurisdiction?.toString() != null ? { 'cf-r2-jurisdiction': cfR2Jurisdiction?.toString() } : undefined), }, options?.headers, ]), }); } /** * Deletes an object from an R2 bucket. * * For most workloads, we recommend using R2's * [S3-compatible API](https://developers.cloudflare.com/r2/api/s3/api/) or a * [Worker with an R2 binding](https://developers.cloudflare.com/r2/api/workers/workers-api-reference/) * instead. * * @example * ```ts * const object = await client.r2.buckets.objects.delete( * 'path/to/my-object.txt', * { * account_id: '023e105f4ecef8ad9ca31a8372d0c353', * bucket_name: 'example-bucket', * }, * ); * ``` */ delete(objectKey, params, options) { const { account_id, bucket_name, 'cf-r2-jurisdiction': cfR2Jurisdiction } = params; return this._client.delete(path `/accounts/${account_id}/r2/buckets/${bucket_name}/objects/${objectKey}`, { ...options, headers: buildHeaders([ { ...(cfR2Jurisdiction?.toString() != null ? { 'cf-r2-jurisdiction': cfR2Jurisdiction?.toString() } : undefined), }, options?.headers, ]), })._thenUnwrap((obj) => obj.result); } /** * Retrieves an object from an R2 bucket. Returns the object body along with * metadata headers. * * For most workloads, we recommend using R2's * [S3-compatible API](https://developers.cloudflare.com/r2/api/s3/api/) or a * [Worker with an R2 binding](https://developers.cloudflare.com/r2/api/workers/workers-api-reference/) * instead. * * @example * ```ts * const object = await client.r2.buckets.objects.get( * 'path/to/my-object.txt', * { * account_id: '023e105f4ecef8ad9ca31a8372d0c353', * bucket_name: 'example-bucket', * }, * ); * * const content = await object.blob(); * console.log(content); * ``` */ get(objectKey, params, options) { const { account_id, bucket_name, 'cf-r2-jurisdiction': cfR2Jurisdiction, 'If-Modified-Since': ifModifiedSince, 'If-None-Match': ifNoneMatch, } = params; return this._client.get(path `/accounts/${account_id}/r2/buckets/${bucket_name}/objects/${objectKey}`, { ...options, headers: buildHeaders([ { Accept: 'application/octet-stream', ...(cfR2Jurisdiction?.toString() != null ? { 'cf-r2-jurisdiction': cfR2Jurisdiction?.toString() } : undefined), ...(ifModifiedSince != null ? { 'If-Modified-Since': ifModifiedSince } : undefined), ...(ifNoneMatch != null ? { 'If-None-Match': ifNoneMatch } : undefined), }, options?.headers, ]), __binaryResponse: true, }); } /** * Uploads an object to an R2 bucket. The object body is provided as the request * body. Returns metadata about the uploaded object. * * The maximum upload size for this endpoint is 300 MB. For most workloads, we * recommend using R2's * [S3-compatible API](https://developers.cloudflare.com/r2/api/s3/api/) or a * [Worker with an R2 binding](https://developers.cloudflare.com/r2/api/workers/workers-api-reference/) * instead. * * @example * ```ts * const response = await client.r2.buckets.objects.upload( * 'path/to/my-object.txt', * fs.createReadStream('path/to/file'), * { * account_id: '023e105f4ecef8ad9ca31a8372d0c353', * bucket_name: 'example-bucket', * }, * ); * ``` */ upload(objectKey, body, params, options) { const { account_id, bucket_name, 'cf-r2-jurisdiction': cfR2Jurisdiction, 'cf-r2-storage-class': cfR2StorageClass, } = params; return this._client.put(path `/accounts/${account_id}/r2/buckets/${bucket_name}/objects/${objectKey}`, { body: body, ...options, headers: buildHeaders([ { 'Content-Type': 'application/octet-stream', ...(cfR2Jurisdiction?.toString() != null ? { 'cf-r2-jurisdiction': cfR2Jurisdiction?.toString() } : undefined), ...(cfR2StorageClass?.toString() != null ? { 'cf-r2-storage-class': cfR2StorageClass?.toString() } : undefined), }, options?.headers, ]), })._thenUnwrap((obj) => obj.result); } } BaseObjects._key = Object.freeze([ 'r2', 'buckets', 'objects', ]); return BaseObjects; })(); export class Objects extends BaseObjects { } //# sourceMappingURL=objects.mjs.map