Home > Enterprise >  Get list of version ids for a blob using node.js
Get list of version ids for a blob using node.js

Time:10-06

I am trying to get list of version ids for a blob using node.js.

    async function getBlobNameAndVersions() {
    
      for await (const blob of containerClient.listBlobsFlat(includeVersions?: true)) {
     
        const tempBlockBlobClient = containerClient.getBlockBlobClient(blob.name);
   
        console.log(`\n\tname: ${blob.versionId}\n\tURL: ${tempBlockBlobClient.name}\n`);
    
      }
    
    }

getBlobNameAndVersions()

Received error saying "ReferenceError: inlcudeVersions is not defined"

When looked at the reference listBlobsFlat, below were options to include versions.

listBlobsFlat(options?: ContainerListBlobsOptions): PagedAsyncIterableIterator<BlobItem, ContainerListBlobFlatSegmentResponse>;
/**
 * Returns an AsyncIterableIterator for ContainerListBlobHierarchySegmentResponse
 *
 * @param delimiter - The character or string used to define the virtual hierarchy
 * @param marker - A string value that identifies the portion of
 *                          the list of blobs to be returned with the next listing operation. The
 *                          operation returns the ContinuationToken value within the response body if the
 *                          listing operation did not return all blobs remaining to be listed
 *                          with the current page. The ContinuationToken value can be used as the value for
 *                          the marker parameter in a subsequent call to request the next page of list
 *                          items. The marker value is opaque to the client.
 * @param options - Options to list blobs operation. 

Tried using options as below.

export declare interface ContainerListBlobsOptions extends CommonOptions {
     * Specifies whether versions should be included in the enumeration. Versions are listed from oldest to newest in the response.
     */
    includeVersions?: boolean;
}

CodePudding user response:

You would need to pass the options as an object i.e. instead of passing includeVersions?: true, you would need to use {includeVersions: true}.

So your code would be something like:

async function getBlobNameAndVersions() {

  for await (const blob of containerClient.listBlobsFlat({includeVersions: true})) {
  
    const tempBlockBlobClient = containerClient.getBlockBlobClient(blob.name);

    console.log(`\n\tname: ${blob.name}\n\tURL: ${tempBlockBlobClient.url}\n`);

  }

}

CodePudding user response:

Updated Code is working to get version ids of blobs and its URL's

async function getBlobNameAndVersions() {
      for await (const blob of containerClient.listBlobsFlat({includeVersions: true})){

        const tempBlockBlobClient = containerClient.getBlockBlobClient(blob.name);
    
        console.log(`\n\tversion: ${blob.versionId}\n\tURL: ${tempBlockBlobClient.url}\n`);
      }
}
  • Related