简体   繁体   English

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

[英]How to compute Azure Storage Container Size in Java?

While the following link details the way you can compute the storage size using C#, I don't see similar methods in Java.虽然以下链接详细介绍了使用 C# 计算存储大小的方法,但我在 Java 中没有看到类似的方法。 Appreciate if someone can post a sample code for Java please.感谢有人可以发布 Java 的示例代码。 Azure Storage container size Azure 存储容器大小

Here is my sample code.这是我的示例代码。 For more details, please refer to the javadocs of Azure Storage SDK for Java .更多详情请参考Azure Storage SDK for Javajavadocs

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

If you need to count size for a container include snapshot, please using the code below to get the blob list.如果您需要计算包含快照的容器的大小,请使用下面的代码获取 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);

If just count size for snapshots in a container, please using the code below to check a blob whether is a snapshot.如果只计算容器中快照的大小,请使用下面的代码检查 blob 是否是快照。

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

In order to get the container size of Azure blob in Java, you would need to iterate through the list of blobs present in it.为了在 Java 中获取 Azure blob 的容器大小,您需要遍历其中存在的 blob 列表。

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

Make sure you definitely call downloadAttributes else the properties will be empty.确保您确实调用了 downloadAttributes,否则属性将为空。

You can refer to this documentation for more details https://docs.microsoft.com/en-us/java/api/com.microsoft.azure.storage.blob.cloudblockblob?view=azure-java-legacy您可以参考此文档以获取更多详细信息https://docs.microsoft.com/en-us/java/api/com.microsoft.azure.storage.blob.cloudblockblob?view=azure-java-legacy

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM