使用 Azure Java SDK 或 REST API 列出存储帐户的所有快照

List all snapshots for a storage account using Azure Java SDK or REST API

我需要列出 Azure 中每个 blob 的所有快照,如果可能的话使用 Java SDK,否则使用 Azure REST API。对于这两个选项,我知道如何列出所有存储帐户,但我还没有找到检索与单个存储帐户关联的快照列表的方法。

根据容器的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列出所有包含snapshot blob的blob,通过isSnapshot().

方法过滤

下面是我的示例代码。

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());
            }
        }
    }
}   

如果你想使用 REST API 来实现这个需求,步骤如下。

  1. 使用存储帐户 List Containers 列出所有容器。
  2. 使用List Blobs with the url parameter include={snapshots} as the subsection Blob and Snapshot List of the reference表示列出包含快照blob的容器的所有blob,然后过滤所有快照blob。