Options
All
  • Public
  • Public/Protected
  • All
Menu

Class BlobClient

Package version

A BlobClient represents a URL to an Azure Storage blob; the blob may be a block blob, append blob, or page blob.

export
class

BlobClient

Hierarchy

Index

Constructors

constructor

  • Creates an instance of BlobClient from connection string.

    memberof

    BlobClient

    Parameters

    • connectionString: string

      Account connection string or a SAS connection string of an Azure storage account. [ Note - Account connection string can only be used in NODE.JS runtime. ] Account connection string example - DefaultEndpointsProtocol=https;AccountName=myaccount;AccountKey=accountKey;EndpointSuffix=core.windows.net SAS connection string example - BlobEndpoint=https://myaccount.blob.core.windows.net/;QueueEndpoint=https://myaccount.queue.core.windows.net/;FileEndpoint=https://myaccount.file.core.windows.net/;TableEndpoint=https://myaccount.table.core.windows.net/;SharedAccessSignature=sasString

    • containerName: string

      Container name.

    • blobName: string

      Blob name.

    • Optional options: StoragePipelineOptions

    Returns BlobClient

  • Creates an instance of BlobClient. This method accepts an encoded URL or non-encoded URL pointing to a blob. Encoded URL string will NOT be escaped twice, only special characters in URL path will be escaped. If a blob name includes ? or %, blob name must be encoded in the URL.

    memberof

    BlobClient

    Parameters

    Returns BlobClient

  • Creates an instance of BlobClient. This method accepts an encoded URL or non-encoded URL pointing to a blob. Encoded URL string will NOT be escaped twice, only special characters in URL path will be escaped. If a blob name includes ? or %, blob name must be encoded in the URL.

    memberof

    BlobClient

    Parameters

    Returns BlobClient

Properties

accountName

accountName: string

credential

Such as AnonymousCredential, StorageSharedKeyCredential or any credential from the @azure/identity package to authenticate requests to the service. You can also provide an object that implements the TokenCredential interface. If not specified, AnonymousCredential is used.

type

{StorageSharedKeyCredential | AnonymousCredential | TokenCredential}

memberof

StorageClient

Protected isHttps

isHttps: boolean
type

{boolean}

memberof

StorageClient

Protected storageClientContext

storageClientContext: StorageClientContext

StorageClient is a reference to protocol layer operations entry, which is generated by AutoRest generator.

type

{StorageClientContext}

memberof

StorageClient

url

url: string

Encoded URL string value.

type

{string}

memberof

StorageClient

Accessors

containerName

  • get containerName(): string

name

  • get name(): string

Methods

abortCopyFromURL

beginCopyFromURL

  • Asynchronously copies a blob to a destination within the storage account. This method returns a long running operation poller that allows you to wait indefinitely until the copy is completed. You can also cancel a copy before it is completed by calling cancelOperation on the poller. Note that attempting to cancel a completed copy will result in an error being thrown.

    In version 2012-02-12 and later, the source for a Copy Blob operation can be a committed blob in any Azure storage account. Beginning with version 2015-02-21, the source for a Copy Blob operation can be an Azure file in any Azure storage account. Only storage accounts created on or after June 7th, 2012 allow the Copy Blob operation to copy from another storage account.

    see

    https://docs.microsoft.com/en-us/rest/api/storageservices/copy-blob

    Example usage of automatic polling:

    const copyPoller = await blobClient.beginCopyFromURL('url');
    const result = await copyPoller.pollUntilDone();

    Example usage of manual polling:

    const copyPoller = await blobClient.beginCopyFromURL('url');
    while (!poller.isDone()) {
       await poller.poll();
    }
    const result = copyPoller.getResult();

    Example usage of progress updates:

    const copyPoller = await blobClient.beginCopyFromURL('url', {
      onProgress(state) {
        console.log(`Progress: ${state.copyProgress}`);
      }
    });
    const result = await copyPoller.pollUntilDone();

    Example usage of changing polling interval (default 15 seconds):

    const copyPoller = await blobClient.beginCopyFromURL('url', {
      intervalInMs: 1000 // poll blob every 1 second for copy progress
    });
    const result = await copyPoller.pollUntilDone();

    Example usage of copy cancellation:

    const copyPoller = await blobClient.beginCopyFromURL('url');
    // cancel operation after starting it.
    try {
      await copyPoller.cancelOperation();
      // calls to get the result now throw PollerCancelledError
      await copyPoller.getResult();
    } catch (err) {
      if (err.name === 'PollerCancelledError') {
        console.log('The copy was cancelled.');
      }
    }

    Parameters

    Returns Promise<PollerLike<PollOperationState<BlobBeginCopyFromURLResponse>, BlobBeginCopyFromURLResponse>>

createSnapshot

delete

download

  • download(offset?: number, count?: undefined | number, options?: BlobDownloadOptions): Promise<BlobDownloadResponseModel>
  • Reads or downloads a blob from the system, including its metadata and properties. You can also call Get Blob to read a snapshot.

    • In Node.js, data returns in a Readable stream readableStreamBody
    • In browsers, data returns in a promise blobBody
    see

    https://docs.microsoft.com/en-us/rest/api/storageservices/get-blob

    memberof

    BlobClient

    example
    // Download and convert a blob to a string (Node.js only)
    const downloadBlockBlobResponse = await blobClient.download();
    const downloaded = await streamToString(downloadBlockBlobResponse.readableStreamBody);
    console.log("Downloaded blob content:", downloaded);
    
    async function streamToString(readableStream) {
      return new Promise((resolve, reject) => {
        const chunks = [];
        readableStream.on("data", (data) => {
          chunks.push(data.toString());
        });
        readableStream.on("end", () => {
          resolve(chunks.join(""));
        });
        readableStream.on("error", reject);
      });
    }
    example
    // Download and convert a blob to a string (Browser only)
    const downloadBlockBlobResponse = await blobClient.download();
    const downloaded = await blobToString(await downloadBlockBlobResponse.blobBody);
    console.log(
      "Downloaded blob content",
      downloaded
    );
    
    async function blobToString(blob: Blob): Promise<string> {
      const fileReader = new FileReader();
      return new Promise<string>((resolve, reject) => {
        fileReader.onloadend = (ev: any) => {
          resolve(ev.target!.result);
        };
        fileReader.onerror = reject;
        fileReader.readAsText(blob);
      });
    }

    Parameters

    • Default value offset: number = 0
    • Optional count: undefined | number
    • Default value options: BlobDownloadOptions = {}

    Returns Promise<BlobDownloadResponseModel>

downloadToBuffer

  • downloadToBuffer(offset?: undefined | number, count?: undefined | number, options?: BlobDownloadToBufferOptions): Promise<Buffer>
  • downloadToBuffer(buffer: Buffer, offset?: undefined | number, count?: undefined | number, options?: BlobDownloadToBufferOptions): Promise<Buffer>
  • ONLY AVAILABLE IN NODE.JS RUNTIME.

    Downloads an Azure Blob in parallel to a buffer. Offset and count are optional, downloads the entire blob if they are not provided.

    export

    Parameters

    • Optional offset: undefined | number

      From which position of the block blob to download(in bytes)

    • Optional count: undefined | number
    • Optional options: BlobDownloadToBufferOptions

    Returns Promise<Buffer>

  • ONLY AVAILABLE IN NODE.JS RUNTIME.

    Downloads an Azure Blob in parallel to a buffer. Offset and count are optional, downloads the entire blob if they are not provided.

    export

    Parameters

    • buffer: Buffer

      Buffer to be fill, must have length larger than count

    • Optional offset: undefined | number

      From which position of the block blob to download(in bytes)

    • Optional count: undefined | number
    • Optional options: BlobDownloadToBufferOptions

    Returns Promise<Buffer>

downloadToFile

  • downloadToFile(filePath: string, offset?: number, count?: undefined | number, options?: BlobDownloadOptions): Promise<BlobDownloadResponseModel>
  • ONLY AVAILABLE IN NODE.JS RUNTIME.

    Downloads an Azure Blob to a local file. Fails if the the given file path already exits. Offset and count are optional, pass 0 and undefined respectively to download the entire blob.

    memberof

    BlobClient

    Parameters

    • filePath: string
    • Default value offset: number = 0
    • Optional count: undefined | number
    • Default value options: BlobDownloadOptions = {}

    Returns Promise<BlobDownloadResponseModel>

    The response data for blob download operation, but with readableStreamBody set to undefined since its content is already read and written into a local file at the specified path.

exists

  • Returns true if the Azrue blob resource represented by this client exists; false otherwise.

    NOTE: use this function with care since an existing blob might be deleted by other clients or applications. Vice versa new blobs might be added by other clients or applications after this function completes.

    memberof

    BlobClient

    Parameters

    Returns Promise<boolean>

getAppendBlobClient

getBlobLeaseClient

getBlockBlobClient

getPageBlobClient

getProperties

setAccessTier

setHTTPHeaders

setMetadata

syncCopyFromURL

undelete

withSnapshot

  • Creates a new BlobClient object identical to the source but with the specified snapshot timestamp. Provide "" will remove the snapshot and return a Client to the base blob.

    memberof

    BlobClient

    Parameters

    • snapshot: string

      The snapshot timestamp.

    Returns BlobClient

    A new BlobClient object identical to the source but with the specified snapshot timestamp

Generated using TypeDoc