【问题标题】:Azure blob storage: Azure Source blob size is set to Zero when I download the blob using DownloadRange APIAzure blob 存储:当我使用 DownloadRange API 下载 blob 时,Azure 源 blob 大小设置为零
【发布时间】:2021-03-29 04:32:20
【问题描述】:

我正在尝试使用 azure 数据移动库下载 Azure blob。

当我尝试使用 API downloadRange 下载此源 blob 时,我遇到了 Azure 源 blob 大小设置为“零”的问题

目标文件已正确下载且大小正确。我错过了什么吗?

我正在使用 azure-storage java sdk 版本 8.6.5。这是示例代码

CloudStorageAccount storageAccount = CloudStorageAccount.Parse("myConnectionString");
CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
CloudBlobContainer container = blobClient.GetContainerReference("myContainer");
CloudBlockBlob cloudBlockBlob = container.GetBlockBlobReference("abc.txt");

这是我正在阅读的循环。目标文件大小正确。我只是想不通为什么源 blob 大小设置为零?当使用 API download 下载全部内容时,这是不可重现的

try (OutputStream out = new ByteBufferBackedOutputStream(buffer)) {
    cloudBlockBlob.downloadRange(position, (long) buffer.capacity(), out);
}...

提前致谢!!

【问题讨论】:

    标签: azure-storage azure-blob-storage


    【解决方案1】:

    如果你只是想通过cloudBlockBlob.downloadRange分块读取文件,试试下面的代码:

    import java.io.ByteArrayOutputStream;
    
    import com.microsoft.azure.storage.CloudStorageAccount;
    import com.microsoft.azure.storage.blob.CloudBlobClient;
    import com.microsoft.azure.storage.blob.CloudBlobContainer;
    import com.microsoft.azure.storage.blob.CloudBlockBlob;
    
    public class lagencyStorgeSdk {
    
        public static void main(String[] args) throws Exception {
            CloudStorageAccount storageAccount = CloudStorageAccount.parse(
                    "<storage account connection string>");
            CloudBlobClient blobClient = storageAccount.createCloudBlobClient();
            CloudBlobContainer container = blobClient.getContainerReference("<container name>");
            CloudBlockBlob cloudBlockBlob = container.getBlockBlobReference(".txt file name");
            cloudBlockBlob.downloadAttributes();
            long totalSize = cloudBlockBlob.getProperties().getLength();
    
            long readSize = 2;
            for (long i = 0; i < totalSize; i += readSize) {
                ByteArrayOutputStream downloadStream = new ByteArrayOutputStream();
                cloudBlockBlob.downloadRange(i, readSize, downloadStream);
                System.out.println("===>" + downloadStream);
            }
        }
    
    }
    

    我的 .txt 文件的内容:

    结果:

    如果您有任何问题,请告诉我。

    【讨论】:

      猜你喜欢
      • 2016-02-29
      • 2018-05-07
      • 2012-12-08
      • 1970-01-01
      • 2019-12-19
      • 2019-09-05
      • 2015-06-20
      • 2022-11-02
      • 2016-08-01
      相关资源
      最近更新 更多