@tweedegolf/sab-adapter-azure-blob
Version:
Provides an abstraction layer for interacting with Microsoft Azure Blob Storage cloud service.
253 lines (252 loc) • 11.5 kB
TypeScript
import { ResultObject, ResultObjectBoolean, ResultObjectBuckets, ResultObjectFiles, ResultObjectNumber, ResultObjectObject, ResultObjectStream } from "./result";
import { FileBufferParams, FilePathParams, FileStreamParams } from "./add_file_params";
export declare enum Provider {
NONE = "none",// initial value for the abstract adapter, don't use this one
LOCAL = "local",// local testing adapter
GCS = "gcs",// Google Cloud Storage
GS = "gs",// Google Cloud Storage
S3 = "s3",// Amazon S3
AWS = "aws",// Amazon S3
AZURE = "azure",// Azure Storage Blob
B2 = "b2",// BackBlaze B2 using native API with AdapterBackblazeB2
BACKBLAZE = "b2",// BackBlaze B2 using native API with AdapterBackblazeB2
B2_S3 = "b2-s3",// Backblaze B2 using S3 API with AdapterAmazonS3
BACKBLAZE_S3 = "b2-s3",// Backblaze B2 using S3 API with AdapterAmazonS3
MINIO = "minio",// Minio using native API with AdapterMinio
MINIO_S3 = "minio-s3",// Minio using S3 API with AdapterAmazonS3
CUBBIT = "cubbit",// Cubbit uses S3 API with AdapterAmazonS3
R2 = "r2",// Cloudflare R2 uses S3 API with AdapterAmazonS3
CLOUDFLARE = "r2"
}
export interface AdapterConfig {
bucketName?: string;
[id: string]: any;
}
export interface StorageAdapterConfig extends AdapterConfig {
provider: string;
}
export interface Options {
[id: string]: any;
}
/**
* @param start the byte of the file where the stream starts(default: 0)
* @param end the byte in the file where the stream ends(default: last byte of file)
*/
export interface StreamOptions extends Options {
start?: number;
end?: number;
}
export interface IAdapter {
/**
* @returns The instance of the service client that connects to the cloud service under the hood. For instance the adapter for Amazon returns the S3Client of the AWS SDK v3.
*/
getServiceClient(): any;
/**
* `getServiceClient` implemented as getter
* The instance of the service client that connects to the cloud service under the hood. For instance the adapter for Amazon returns the S3Client of the AWS SDK v3.
*/
get serviceClient(): any;
/**
* @returns the storage provide, e.g. 'gcs', 'b2', 'local' etc.
*/
getProvider(): Provider;
/**
* `getType` implemented as getter
* The adapter type, e.g. 'gcs', 'b2', 'local' etc.
*/
get provider(): Provider;
/**
* Returns configuration settings that you've provided when instantiating as an object.
* Use this only for debugging and with great care as it may expose sensitive information.
*
* The object contains the key `bucketName` which is the initial value that you've set during
* initialization.
*
* The object also contains the key `options` which are only the options passed in during
* initialization; if you want all options, including the default options use `getOptions()`
*
* @returns adapter configuration as object
*/
getConfig(): AdapterConfig;
/**
* `getConfiguration` implemented as getter
* The adapter configuration as object
*/
get config(): AdapterConfig;
/**
* @returns the error message from the configuration parser or `null`.
*/
getConfigError(): null | string;
/**
* `getConfigError` implemented as getter
* The error message from the configuration parser or `null`.
*/
get configError(): null | string;
/**
* @returns The name of the selected bucket or `null` if no bucket is selected.
*/
getSelectedBucket(): null | string;
/**
* `getSelectedBucket` implemented as getter
* The name of the selected bucket or `null` if no bucket is selected.
*/
get selectedBucket(): null | string;
/**
* Stores the name of a bucket. The name of the bucket will be accessible using `getSelectedBucket` or the getter `selectedBucket`. By setting a selected bucket you can omit the `bucketName` parameter in all methods that otherwise require this parameter
* @param bucketName the name of the bucket, if you pass `null` the currently selected bucket will be deselected
*/
setSelectedBucket(bucketName: null | string): void;
/**
* Stores the name of a bucket. The name of the bucket will be accessible using `getSelectedBucket` or the getter `selectedBucket`. By setting a selected bucket you can omit the `bucketName` parameter in all methods that otherwise require this parameter
* @param bucketName the name of the bucket, if you pass `null` the currently selected bucket will be deselected
*/
set selectedBucket(bucketName: null | string);
/**
* short version of `getSelectedBucket`
* The name of the selected bucket or `null` if no bucket is selected.
*/
get bucketName(): null | string;
/**
* @description short version of `setSelectedBucket`
* @description Stores the name of a bucket. The name of the bucket will be accessible using `getSelectedBucket` or the getter `selectedBucket`. By setting a selected bucket you can omit the `bucketName` parameter in all methods that otherwise require this parameter
* @param name the name of the bucket, if you pass `null` the currently selected bucket will be deselected
*/
set bucketName(name: null | string);
/**
* @promise CreateBucketPromise
* @fulfill {ResultObject} `{value: "ok"}` or `{error: "the generated error message"}`
*/
/**
* @description creates new bucket
* @param bucketName name of the bucket to create, returns "ok" once the bucket has been created but yields an error if bucket already exists.
* @param options additional options for creating a bucket such as access rights
* @resolves `{value: "ok"}` or `{error: "the generated error message"}`
*/
createBucket(...args: [bucketName?: string, options?: Options] | [options?: Options]): Promise<ResultObject>;
/**
* @param bucketName: deletes all files in the bucket.
*/
clearBucket(bucketName?: string): Promise<ResultObject>;
/**
* Deletes the bucket with the provided name. If no bucket name is provided, the selected bucket will be deleted.
* @param {string} bucketName name of the bucket
* @returns {Promise<ResultObject>} a promise that always resolves in a ResultObject:
* ```typescript
* { error: null | string, value: null | string }
* ```
*/
deleteBucket(bucketName?: string): Promise<ResultObject>;
/**
* @returns an array of the names of the buckets in this storage
*/
listBuckets(): Promise<ResultObjectBuckets>;
/**
* @param {filePathParams | FileBufferParams | FileStreamParams} params related to the file to be added
* @returns the public url to the file
* Called internally by addFileFromPath, addFileFromBuffer and addFileFromReadable
*/
addFile(params: FilePathParams | FileBufferParams | FileStreamParams): Promise<ResultObject>;
/**
* @param {FilePathParams} params object that has the following keys:
* ```typescript
* {
* bucketName: string
* origPath: string //path to the file that you want to add, e.g. /home/user/Pictures/image1.jpg
* targetPath: string //path on the storage, you can add a path or only provide name of the file
* options?: object
* }
* ```
* @returns {ResultObject} a promise that always resolves in a ResultObject:
* ```typescript
* {
* value: null | string
* error: null | string
* }
* ```
*/
addFileFromPath(params: FilePathParams): Promise<ResultObject>;
/**
* @param {FileBufferParams} params
* @property {string} FilePath.bucketName
* @property {Buffer} FilePath.buffer - buffer
* @property {string} FilePath.targetPath - path on the storage, you can add a path or only provide name of the file
* @property {object} FilePath.options
*/
addFileFromBuffer(params: FileBufferParams): Promise<ResultObject>;
/**
* @param {FileStreamParams} params object that contains the following keys:
* ```typescript
* {
* bucketName: string
* readable: Readable // stream from the local file, e.g. fs.createReadStream(path)
* targetPath: string // path on the storage, you can add a path or only provide name of the file
* options?: object
* }
* ```
* @returns {ResultObject} a promise that always resolves in a ResultObject
* ```typescript
* {
* value: null | string // if success value is the public url to the file
* error: null | string // if fails error is the error message
* }
* ```
*/
addFileFromStream(params: FileStreamParams): Promise<ResultObject>;
/**
* @param bucketName name of the bucket where the file is stored
* @param fileName name of the file to be returned as a readable stream
*/
getFileAsStream(...args: [bucketName: string, fileName: string, options?: StreamOptions] | [fileName: string, options?: StreamOptions]): Promise<ResultObjectStream>;
/**
* @param bucketName name of the bucket where the file is stored
* @param fileName name of the file
* @param options
*/
getPublicURL(...args: [bucketName: string, fileName: string, options?: Options] | [fileName: string, options?: Options]): Promise<ResultObject>;
/**
* @param bucketName name of the bucket where the file is stored
* @param fileName name of the file
* @param options e.g. { expiresIn: 3600 }
*/
getSignedURL(...args: [bucketName: string, fileName: string, options?: Options] | [fileName: string, options?: Options]): Promise<ResultObject>;
/**
* @param bucketName name of the bucket where the file is stored
* @param fileName name of the file to be removed
*/
removeFile(...args: [bucketName: string, fileName: string] | [fileName: string]): Promise<ResultObject>;
/**
* @param bucketName
* @param numFiles
* @returns an array of tuples containing the file path and the file size of all files in the bucket.
*/
listFiles(...args: [bucketName: string, numFiles?: number] | [bucketName?: string] | [numFiles?: number]): Promise<ResultObjectFiles>;
/**
* @param bucketName name of the bucket where the file is stored
* @param fileName name of the file
* @returns the size of the file in bytes
*/
sizeOf(...args: [bucketName: string, fileName: string] | [fileName: string]): Promise<ResultObjectNumber>;
/**
* @param bucketName name of the bucket
* @returns boolean
*/
bucketExists(bucketName?: string): Promise<ResultObjectBoolean>;
/**
* @param bucketName name of the bucket
* @returns boolean
*/
bucketIsPublic(bucketName?: string): Promise<ResultObjectBoolean>;
/**
* @param bucketName name of the bucket where the file is stored
* @param fileName name of the file
* @returns boolean
*/
fileExists(...args: [bucketName: string, fileName: string] | [fileName: string]): Promise<ResultObjectBoolean>;
/**
* @param bucketName name of the bucket where the file is stored
* @param fileName name of the file
* @param options constraint validity of URL
* @returns string presigned upload URL
*/
getPresignedUploadURL(...args: [bucketName: string, fileName: string, options?: Options] | [fileName: string, options?: Options]): Promise<ResultObjectObject>;
}