UNPKG

node-cloudflare-r2

Version:
161 lines (160 loc) 7.24 kB
import { type S3Client as R2, type S3ClientConfig } from '@aws-sdk/client-s3'; import { type Progress } from '@aws-sdk/lib-storage'; import { type ReadStream, type PathLike } from 'fs'; import type { Readable } from 'stream'; import type { CORSPolicy, HeadObjectResponse, ObjectListResponse, UploadFileResponse } from './types'; export declare class Bucket { private readonly r2; private readonly endpoint; private bucketPublicUrls; /** * Name of the bucket. * @readonly */ readonly name: string; /** * URI of the bucket. * @readonly */ readonly uri: string; /** * Instantiate `Bucket`. * @param r2 R2 instance. * @param bucketName Name of the bucket. * @param endpoint Cloudflare R2 base endpoint. */ constructor(r2: R2, bucketName: string, endpoint: S3ClientConfig['endpoint']); /** * Returns the name of the current bucket. */ getBucketName(): string; /** * Returns the URI for the current bucket. */ getUri(): string; provideBucketPublicUrl(bucketPublicUrl: string): this; provideBucketPublicUrl(bucketPublicUrls: string[]): this; /** * **DEPRECATED. This method will be removed in the next major version. Use `getPublicUrls()` instead.** * * Returns the bucket public URL if it's set with `provideBucketPublicUrl` method. * @deprecated */ getPublicUrl(): string | undefined; /** * Returns all public URLs of the bucket if it's set with `provideBucketPublicUrl()` method. */ getPublicUrls(): string[]; /** * Returns the signed URL of an object. This method does not check whether the object exists or not. * @param objectKey * @param expiresIn Expiration time in seconds. */ getObjectSignedUrl(objectKey: string, expiresIn: number): Promise<string>; /** * Generates object public URL if the bucket public URL is set with `provideBucketPublicUrl` method. * @param objectKey */ protected generateObjectPublicUrl(objectKey: string): string | null; /** * Generates object public URLs if the bucket public URL is set with `provideBucketPublicUrl` method. * @param objectKey */ protected generateObjectPublicUrls(objectKey: string): Array<string>; /** * Returns all public URL of an object in the bucket. * @param objectKey */ getObjectPublicUrls(objectKey: string): string[]; /** * Checks if the bucket exists and you have permission to access it. * @param bucketName */ exists(): Promise<boolean>; /** * **DEPRECATED. This method will be remove in the next major version. Use `getCorsPolicies()` instead.** * * Returns Cross-Origin Resource Sharing (CORS) policies of the bucket. * @deprecated */ getCors(): Promise<CORSPolicy[]>; /** * Returns Cross-Origin Resource Sharing (CORS) policies of the bucket. */ getCorsPolicies(): Promise<CORSPolicy[]>; /** * Returns the region the bucket resides in. * @param bucketName */ getRegion(): Promise<import("@aws-sdk/client-s3").BucketLocationConstraint | "auto">; /** * Returns the encryption configuration of the bucket. */ getEncryption(): Promise<{ applyServerSideEncryptionByDefault: { sseAlgorithm: import("@aws-sdk/client-s3").ServerSideEncryption | undefined; kmsMasterKeyId: string | undefined; }; bucketKeyEnabled: boolean | undefined; }[]>; /** * Upload a local file to the bucket. If the file already exists in the bucket, it will be overwritten. * @param file File location. * @param destination Name of the file to put in the bucket. If `destination` contains slash character(s), this will put the file inside directories. * @param customMetadata Custom metadata to set to the uploaded file. * @param mimeType Optional mime type. (Default: `application/octet-stream`) */ uploadFile(file: PathLike, destination?: string, customMetadata?: Record<string, string>, mimeType?: string): Promise<UploadFileResponse>; /** * Upload an object to the bucket. * @param contents The object contents to upload. * @param destination The name of the file to put in the bucket. If `destination` contains slash character(s), this will put the file inside directories. If the file already exists in the bucket, it will be overwritten. * @param customMetadata Custom metadata to set to the uploaded file. * @param mimeType Optional mime type. (Default: `application/octet-stream`) */ upload(contents: string | Uint8Array | Buffer | Readable | ReadStream, destination: string, customMetadata?: Record<string, string>, mimeType?: string): Promise<UploadFileResponse>; /** * Upload an object or stream to the bucket. This is a new method to put object to the bucket using multipart upload. * @param contents The object contents to upload. * @param destination The name of the file to put in the bucket. If `destination` contains slash character(s), this will put the file inside directories. If the file already exists in the bucket, it will be overwritten. * @param customMetadata Custom metadata to set to the uploaded file. * @param mimeType Optional mime type. (Default: `application/octet-stream`) * @param onProgress A callback to handle progress data. */ uploadStream(contents: string | Uint8Array | Buffer | Readable | ReadStream, destination: string, customMetadata?: Record<string, string>, mimeType?: string, onProgress?: (progress: Progress) => void): Promise<UploadFileResponse>; /** * **DEPRECATED. This method will be removed in the next major version. Use `deleteObject()` instead.** * * Deletes a file in the bucket. * @param file * @deprecated */ deleteFile(file: string): Promise<boolean | 0 | undefined>; /** * Deletes an object in the bucket. * @param objectKey */ deleteObject(objectKey: string): Promise<boolean | 0 | undefined>; /** * Retrieves metadata from an object without returning the object itself. * @param objectKey */ headObject(objectKey: string): Promise<HeadObjectResponse>; /** * Returns some or all (up to 1,000) of the objects in the bucket with each request. * @param maxResults The maximum number of objects to return per request. (Default: 1000) * @param marker A token that specifies where to start the listing. */ listObjects(maxResults?: number, marker?: string): Promise<ObjectListResponse>; /** * Copies an object from the current storage bucket to a new destination object in the same bucket. * @param sourceObjectKey The key of the source object to be copied. * @param destinationObjectKey The key of the destination object where the source object will be copied to. */ copyObject(sourceObjectKey: string, destinationObjectKey: string): Promise<import("@aws-sdk/client-s3").CopyObjectCommandOutput>; /** * Checks if an object exists in the bucket. * @param objectkey */ objectExists(objectkey: string): Promise<boolean>; }