1

I need to list all snapshots for each blob in Azure, using the Java SDK if possible or the Azure REST API otherwise. For both options, I know how to list all storage accounts, but I have not found a way to retrieve a list of snapshots associated with a single storage account.

dededecline
  • 103
  • 2
  • 11

1 Answers1

1

Acording to the javadocs of Azure Storage SDK for Java, using the method listBlobs(String prefix, boolean useFlatBlobListing, EnumSet<BlobListingDetails> listingDetails, BlobRequestOptions options, OperationContext opContext) with BlobListingDetails.SNAPSHOTS for a container to list all blobs which include snapshot blob to filter by the method isSnapshot().

Here is my sample code below.

String accountName = "<your-storage-account-name>";
String accountKey = "<your-storage-account-key>";
String storageConnectionString = "DefaultEndpointsProtocol=https;AccountName=%s;AccountKey=%s";
String connectionString = String.format(storageConnectionString, accountName, accountKey);
CloudStorageAccount account = CloudStorageAccount.parse(connectionString);
CloudBlobClient client = account.createCloudBlobClient();
// List all containers of a storage account
Iterable<CloudBlobContainer> containers = client.listContainers();
String prefix = null;
boolean useFlatBlobListing = true;
// Specify the blob list which include snapshot blob
EnumSet<BlobListingDetails> listingDetails = EnumSet.of(BlobListingDetails.SNAPSHOTS);
BlobRequestOptions options = null;
OperationContext opContext = null;
for (CloudBlobContainer container : containers) {
    Iterable<ListBlobItem> blobItems = container.listBlobs(prefix, useFlatBlobListing, listingDetails, options,
                    opContext);
    for (ListBlobItem blobItem : blobItems) {
        if (blobItem instanceof CloudBlob) {
            CloudBlob blob = (CloudBlob) blobItem;
            // Check a blob whether be a snapshot blob
            if (blob.isSnapshot()) {
                System.out.println(blobItem.getStorageUri());
            }
        }
    }
}   

If you want to use REST API for implementing this needs, the steps as below.

  1. Using List Containers for a storage account to list all containers.
  2. Using List Blobs with the url parameter include={snapshots} as the subsection Blob and Snapshot List of the reference said to list all blobs of a container which include snapshot blob, then to filter all snapshot blobs.
Peter Pan
  • 23,476
  • 4
  • 25
  • 43