如何用Java计算Azure存储容器大小?

时间:2017-03-15 04:40:14

标签: java azure containers azure-storage-blobs

虽然以下链接详细介绍了使用C#计算存储大小的方法,但我没有在Java中看到类似的方法。感谢有人可以发布Java示例代码。 Azure Storage container size

2 个答案:

答案 0 :(得分:2)

这是我的示例代码。有关详细信息,请参阅javadocsAzure Storage SDK for Java

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();
String containerName = "mycontainer";
CloudBlobContainer container = client.getContainerReference(containerName);
long size = 0L;
Iterable<ListBlobItem> blobItems = container.listBlobs();
for (ListBlobItem blobItem : blobItems) {
    if (blobItem instanceof CloudBlob) {
        CloudBlob blob = (CloudBlob) blobItem;
        size += blob.getProperties().getLength();
    }
}

如果您需要计算容器包含快照的大小,请使用以下代码获取blob列表。

// If count blob size for a container include snapshots
String prefix = null;
boolean useFlatBlobListing = true;
EnumSet<BlobListingDetails> listingDetails = EnumSet.of(BlobListingDetails.SNAPSHOTS);
BlobRequestOptions options = null;
OperationContext opContext = null;
Iterable<ListBlobItem> blobItems = container.listBlobs(prefix, useFlatBlobListing, listingDetails, options, opContext);

如果只计算容器中快照的大小,请使用下面的代码检查blob是否为快照。

if (blob.isSnapshot()) {
    size += blob.getProperties().getLength();
}

答案 1 :(得分:0)

为了获取Java中Azure斑点的容器大小,您需要遍历其中存在的斑点列表。

val container: CloudBlobContainer = blobClient.getContainerReference(containerName)
val blob: CloudBlockBlob = container.getBlockBlobReference(blobPath)
blob.downloadAttributes()
blob.getProperties.getLength

请确保您确实调用downloadAttributes,否则属性将为空。

有关更多详细信息,请参阅此文档https://docs.microsoft.com/en-us/java/api/com.microsoft.azure.storage.blob.cloudblockblob?view=azure-java-legacy