Azure blob 存储:当我使用 DownloadRange API 下载 blob 时,Azure 源 blob 大小设置为零
Azure blob storage: Azure Source blob size is set to Zero when I download the blob using DownloadRange API
我正在尝试使用 azure 数据移动库下载 Azure blob。
当我尝试使用 APIs 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);
}...
提前致谢!!
如果您只想按 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 文件的内容:
结果:
如果您有任何问题,请告诉我。
我正在尝试使用 azure 数据移动库下载 Azure blob。
当我尝试使用 APIs 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);
}...
提前致谢!!
如果您只想按 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 文件的内容:
结果:
如果您有任何问题,请告诉我。