UNPKG

@webda/gcp

Version:

Webda GCP Services implementation

160 lines (159 loc) 4.78 kB
import { Bucket } from "@google-cloud/storage"; import { BinaryFile, BinaryMap, BinaryParameters, CloudBinary, CoreModel, DeepPartial, OperationContext, WebContext } from "@webda/core"; import { Readable } from "stream"; export type StorageObject = { key: string; /** * bucket is an option, * if not provided: it will get the DEFAULT_BUCKET set from environment variable */ bucket?: string; }; export type DownloadParams = StorageObject & { localPath: string; }; export type PutObjectParams = StorageObject & { localPath?: string; content?: string | Buffer; }; export type SignedUrlParams = StorageObject & { expires?: number; action: "read" | "write" | "delete"; contentMd5?: string; contentType?: string; extensionHeaders?: { [key: string]: string; }; responseDisposition?: string; queryParams?: { [key: string]: string; }; }; export type StorageObjectMeta = { size: number; contentType: string; }; export declare class StorageParameters extends BinaryParameters { prefix?: string; bucket: string; constructor(params: any, service: Storage); } /** * Storage handles the storage of binary on a Google Cloud Storage bucket * * See Binary the general interface * @WebdaModda GoogleCloudStorage */ export default class Storage<T extends StorageParameters = StorageParameters> extends CloudBinary<T> { private _storage?; private get storage(); /** * Load the parameters * * @param params */ loadParameters(params: DeepPartial<T>): StorageParameters; /** * @override */ _get(info: BinaryMap): Promise<Readable>; /** * @override */ getSignedUrlFromMap(map: BinaryMap, expires: number, _context: OperationContext): Promise<string>; /** * Get object content in Buffer * @param {StorageObject} params */ getContent({ bucket, key }: StorageObject): Promise<Readable>; /** * Upload a local file to GCS bucket destination * * @param key to add to * @param body content of the object * @param metadatas to put along the object * @param bucket to use */ putObject(key: string, body: Readable | Buffer | string, metadata?: {}, bucket?: string): Promise<void>; /** * Return the Google Bucket directly * * @param bucket name of the bucket default to the one predefined * @returns */ getStorageBucket(bucket?: string): Bucket; /** * @inheritdoc */ _cleanUsage(hash: string, uuid: string, property?: string): Promise<void>; /** * @inheritdoc */ getUsageCount(hash: string): Promise<number>; /** * @inheritdoc */ store(object: CoreModel, property: string, file: BinaryFile): Promise<void>; /** * Delete a file inside an existing bucket * @param {DeleteObjectParams} params */ deleteObject({ bucket, key }: StorageObject): Promise<void>; /** * Move a file from one bucket to an other * @param {StorageObject} source * @param {StorageObject} destination */ moveObject(source: StorageObject, destination: StorageObject): Promise<void>; /** * @inheritdoc */ putRedirectUrl(ctx: WebContext): Promise<{ url: string; method: string; headers: { [key: string]: string; }; }>; /** * Retrieve one signed URL to download the file in parameter * @param {SignedUrlParams} params * @returns {string} URL in order to download the file */ getSignedUrl({ bucket, key, expires, action, ...params }: SignedUrlParams): Promise<string>; /** * Retrieve mandatory headers if needed by the cloud provider (ie. Azure) * Returns empty object if no headers are mandatory * @returns {[key:string]: string} mandatory headers */ getSignedUrlHeaders(): { [key: string]: string; }; /** * Retrieve public URL of this object * @param {StorageModel} store * @returns {string} Public URL in order to download the file */ getPublicUrl({ bucket, key }: StorageObject): Promise<string>; /** * Fetch an object's metadata * @param {StorageModel} store * @returns {any} Metadata */ getMeta({ bucket, key }: StorageObject): Promise<StorageObjectMeta>; /** * Get a bucket size with a prefix * @param bucket * @param prefix on the bucket * @param regex to filter files */ getBucketSize(bucket?: string, prefix?: string, regex?: RegExp): Promise<{ size: number; count: number; }>; /** * @inheritdoc */ putMarker(hash: string, uuid: string, storeName: string): Promise<void>; } export { Storage };