node.jsazuresnapshotstorage-file-share

List azure file shares snapshots with node.js


Is there any way to list in node.js the list of snapshots that a file share has?

Example code:

const { ShareServiceClient, StorageSharedKeyCredential } = require("@azure/storage-file-share");
const credential = new StorageSharedKeyCredential(AZURE_STORAGE_ACCOUNT,AZURE_STORAGE_ACCESS_KEY);
const shareServiceClient = new ShareServiceClient(AZURE_STORAGE_CONNECTION_STRING,credential);
var shareName = "xxxxx";
var shareClient = shareServiceClient.getShareClient(shareName);

// Create a snapshot:
await shareClient.createSnapshot();

How to list the snapshots that this shareName has?


Solution

  • As such there's no special method to list snapshots for a file share. You will need to call listShares method of ShareServiceClient (@azure/storage-file-share version 12.5.0) with includeSnapshots parameter as true and prefix as the share name.

    Here's the sample code to do so (untested code):

    const shareName = 'share-name';
    const listingOptions = {
        prefix: shareName,
        includeSnapshots: true
    };
    shareServiceClient.listShares(listingOptions).byPage().next()
    .then((result) => {
        const shareItems = result.value.shareItems;
        //Filter results where name of the share is same as share name and is a snapshot
        const shareSnapshots = shareItems.filter(s => s.name === shareName && s.snapshot && s.snapshot !== '');
        console.log(shareSnapshots);
    })
    .catch((error) => {
        console.log(error);
    })