Options
All
  • Public
  • Public/Protected
  • All
Menu

Class BackblazeB2Provider

Client to interact with Backblaze B2 cloud storage.

Hierarchy

  • StorageProvider
    • BackblazeB2Provider

Index

Constructors

constructor

Properties

Private _bucketIdCache

_bucketIdCache: object

Type declaration

  • [s: string]: object
    • result: string
    • time: number

Protected _client

_client: any

Private _isAuthorized

_isAuthorized: boolean

Protected _provider

_provider: string

client

client: any

Returns an instance of the client object, to interact with the cloud provider directly

returns

Client object

provider

provider: string

Returns the name of the provider

returns

Provider name

Static bucketIdCacheDuration

bucketIdCacheDuration: number = 900000

Specifies for how long (in ms) to keep BucketId data in cache. Set to 0 to disable caching. Default is 15 minutes.

Methods

Private _ensureAuthorized

  • _ensureAuthorized(): Promise<void>

Private _getBucketId

  • _getBucketId(bucketName: string): Promise<string>
  • Returns the bucketId property for a given bucket name, as most B2 methods require a bucket's ID.

    The result is cached in memory for a certain amount of time configured with BackblazeB2Provider.bucketIdCacheDuration (default: 15 minutes), and up to 100 IDs.

    async

    Parameters

    • bucketName: string

      Name of the bucket

    Returns Promise<string>

    Promise that resolves with the bucketId

Private _getFileId

  • _getFileId(bucketId: string, fileName: string): Promise<string>
  • Returns the fileId property for a given file name, as some B2 methods require a file's ID

    async

    Parameters

    • bucketId: string

      ID of the bucket

    • fileName: string

      Name of the file

    Returns Promise<string>

    Promise that resolves with the fileId

createContainer

  • Create a container ("bucket") on the server.

    async

    Parameters

    • container: string

      Name of the container

    • Optional options: BackblazeB2CreateContainerOptions

      Dictionary with options for creating the container, including the access level

    Returns Promise<void>

    Promise that resolves once the container has been created. The promise doesn't contain any meaningful return value.

deleteContainer

  • deleteContainer(container: string): Promise<void>
  • Removes a container from the server

    async

    Parameters

    • container: string

      Name of the container

    Returns Promise<void>

    Promise that resolves once the container has been removed

deleteObject

  • deleteObject(container: string, path: string): Promise<void>
  • Removes an object from the server

    async

    Parameters

    • container: string

      Name of the container

    • path: string

      Path of the object, inside the container

    Returns Promise<void>

    Promise that resolves once the object has been removed

ensureContainer

  • Create a container ("bucket") on the server if it doesn't already exist.

    async

    Parameters

    • container: string

      Name of the container

    • Optional options: BackblazeB2CreateContainerOptions

      Dictionary with options for creating the container, including the access level

    Returns Promise<void>

    Promise that resolves once the container has been created

getObject

  • getObject(container: string, path: string): Promise<Stream>
  • Requests an object from the server. The method returns a Promise that resolves to a Readable Stream containing the data.

    async

    Parameters

    • container: string

      Name of the container

    • path: string

      Path of the object, inside the container

    Returns Promise<Stream>

    Readable Stream containing the object's data

getObjectAsBuffer

  • getObjectAsBuffer(container: string, path: string): Promise<Buffer>
  • Requests an object from the server. The method returns a Promise that resolves to a Buffer object containing the data from the server.

    async

    Parameters

    • container: string

      Name of the container

    • path: string

      Path of the object, inside the container

    Returns Promise<Buffer>

    Buffer containing the object's data

getObjectAsString

  • getObjectAsString(container: string, path: string, encoding?: string): Promise<string>
  • Requests an object from the server. The method returns a Promise that resolves to a string containing the data from the server.

    async

    Parameters

    • container: string

      Name of the container

    • path: string

      Path of the object, inside the container

    • Optional encoding: string

      Optional encoding for the string; defaults to utf8

    Returns Promise<string>

    String containing the object's data

isContainer

  • isContainer(container: string): Promise<boolean>
  • Check if a container exists.

    async

    Parameters

    • container: string

      Name of the container

    Returns Promise<boolean>

    Promises that resolves with a boolean indicating if the container exists.

listContainers

  • listContainers(): Promise<string[]>
  • Lists all containers belonging to the user

    async

    Returns Promise<string[]>

    Promise that resolves with an array of all the containers

listObjects

  • listObjects(container: string, prefix?: string): Promise<ListResults>
  • Returns a list of objects with a given prefix (folder). The list is not recursive, so prefixes (folders) are returned as such.

    async

    Parameters

    • container: string

      Name of the container

    • Optional prefix: string

      Prefix (folder) inside which to list objects

    Returns Promise<ListResults>

    List of elements returned by the server

presignedGetUrl

  • presignedGetUrl(container: string, path: string, ttl?: number): Promise<string>
  • Returns a URL that clients (e.g. browsers) can use to request an object from the server with a GET request, even if the object is private.

    Backblaze B2 currently does not support this API, and calling this method will always throw an error. Sorry!

    async

    Parameters

    • container: string

      Name of the container

    • path: string

      Path of the object, inside the container

    • Optional ttl: number

      Expiry time of the URL, in seconds (default: 1 day)

    Returns Promise<string>

    Promise that resolves with the pre-signed URL for GET requests

presignedPutUrl

  • presignedPutUrl(container: string, path: string, options?: PutObjectOptions, ttl?: number): Promise<string>
  • Returns a URL that clients (e.g. browsers) can use for PUT operations on an object in the server, even if the object is private.

    Backblaze B2 currently does not support this API, and calling this method will always throw an error. Sorry!

    async

    Parameters

    • container: string

      Name of the container

    • path: string

      Path where to store the object, inside the container

    • Optional options: PutObjectOptions

      Key-value pair of options used by providers, including the metadata dictionary

    • Optional ttl: number

      Expiry time of the URL, in seconds (default: 1 day)

    Returns Promise<string>

    Promise that resolves with the pre-signed URL for GET requests

putObject

  • Uploads a stream to the object storage server.

    The Backblaze B2 APIs have relatively poor support for streams, as it requires the size of the data to be sent at the beginning of the request. As a consequence, this method will upload the file using a different API based on the input data:

    1. If the length of the data can be known before the upload starts, makes a single upload call. This applies to all situations when data is a Buffer or a string, and when data is a Stream and either the options.length argument is specified, or data.byteLength is defined (all data is loaded in memory before being sent to the server in this case).
    2. In the situation when data is a Stream and the length can't be known beforehand, if the data is longer than B2Upload.chunkSize (default: 9MB; minimum: 5MB) the method will use B2's large files APIs. With those, it's possible to chunk the file into many chunks and upload them separately, thus it's not necessary to load the entire Stream in memory. However, this way of uploading files requires many more network calls, and could be significantly slower. B2 supports up to 1,000 chunks per object, so using 9MB chunks (the default value for B2Upload.chunkSize), maximum file size is 90GB.

    Notes on the metadata:

    • The Content-Type header is always supported and used as-is
    • When using the large file APIs, no other custom header can be added
    • When using the "normal APIs", you can add up to 10 custom headers, all starting with the X-Bz-Info- prefix (if your headers don't start with this prefix, it will be added automatically)
    async

    Parameters

    • container: string

      Name of the container

    • path: string

      Path where to store the object, inside the container

    • data: Stream | string | Buffer

      Object data or stream. Can be a Stream (Readable Stream), Buffer or string.

    • Optional options: BackblazeB2PutObjectOptions

      Key-value pair of options used by providers, including the metadata dictionary. For the Backblaze B2 provider, this object contains the length property too, which is useful when passing a stream as data object, as it might allow for faster uploads.

    Returns Promise<void>

    Promise that resolves once the object has been uploaded

Generated using TypeDoc