UNPKG

cloudflare

Version:

The official TypeScript library for the Cloudflare API

173 lines • 7.55 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.Objects = exports.BaseObjects = void 0; // File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details. const resource_1 = require("../../../core/resource.js"); const pagination_1 = require("../../../core/pagination.js"); const headers_1 = require("../../../internal/headers.js"); const path_1 = require("../../../internal/utils/path.js"); var BaseObjects = /* @__PURE__ */ (() => { class BaseObjects extends resource_1.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((0, path_1.path) `/accounts/${account_id}/r2/buckets/${bucketName}/objects`, (pagination_1.CursorPagination), { query, ...options, headers: (0, headers_1.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((0, path_1.path) `/accounts/${account_id}/r2/buckets/${bucket_name}/objects/${objectKey}`, { ...options, headers: (0, headers_1.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((0, path_1.path) `/accounts/${account_id}/r2/buckets/${bucket_name}/objects/${objectKey}`, { ...options, headers: (0, headers_1.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((0, path_1.path) `/accounts/${account_id}/r2/buckets/${bucket_name}/objects/${objectKey}`, { body: body, ...options, headers: (0, headers_1.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; })(); exports.BaseObjects = BaseObjects; class Objects extends BaseObjects { } exports.Objects = Objects; //# sourceMappingURL=objects.js.map