我需要列出Azure中每个blob的所有快照,如果可能,使用Java SDK,否则使用Azure REST API。对于这两个选项,我知道如何列出所有存储帐户,但我还没有找到一种方法来检索与单个存储帐户关联的快照列表。
答案 0 :(得分:1)
根据Azure Storage SDK for Java的javadocs,使用方法listBlobs(String prefix, boolean useFlatBlobListing, EnumSet<BlobListingDetails> listingDetails, BlobRequestOptions options, OperationContext opContext)
和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来实现这一需求,请执行以下步骤。
List Containers
存储帐户列出所有容器。List Blobs
和url参数include={snapshots}
作为reference的小节Blob and Snapshot List
表示列出包含快照blob的容器的所有blob,然后过滤所有快照blob。