@Override
public byte[] getBlobAsByteArray(String customerId, String docTypeId, String fileName, boolean isUser) {
String blobName = formBlobName(customerId, docTypeId, fileName);
BlobClient blobClient = configureBlobClient(blobName, isUser, false);
try (ByteArrayOutputStream outputStream = new ByteArrayOutputStream()) {
blobClient.downloadStream(outputStream);
return outputStream.toByteArray();
} catch (Exception e) {
throw new MSSNotFoundException("Failed to download blob for docTypeId: "+docTypeId, e.getMessage());
}
}
错误:
java.lang.Long.longValue()" because the return value of "com.azure.storage.blob.models.BlobDownloadHeaders.getContentLength()" is null
java.lang.NullPointerException: Cannot invoke "java.lang.Long.longValue()" because the return value of "com.azure.storage.blob.models.BlobDownloadHeaders.getContentLength()" is null
at com.azure.storage.blob.implementation.util.ModelHelper.getBlobLength(ModelHelper.java:467)
at com.azure.storage.blob.specialized.BlobAsyncClientBase.lambda$downloadStreamWithResponse$18(BlobAsyncClientBase.java:1289)
at reactor.core.publisher.FluxMap$MapSubscriber.onNext(FluxMap.java:106)
at reactor.core.publisher.FluxOnErrorResume$ResumeSubscriber.onNext(FluxOnErrorResume.java:79)
at reactor.core.publisher.MonoFlatMap$FlatMapMain.onNext(MonoFlatMap.java:158)
at reactor.core.publisher.MonoFlatMap$FlatMapMain.onNext(MonoFlatMap.java:158)
at reactor.core.publisher.FluxMapFuseable$MapFuseableSubscriber.onNext(FluxMapFuseable.java:129)
at reactor.core.publisher.FluxHide$SuppressFuseableSubscriber.onNext(FluxHide.java:137)
at reactor.core.publisher.FluxMapFuseable$MapFuseableSubscriber.onNext(FluxMapFuseable.java:129)
at reactor.core.publisher.FluxHide$SuppressFuseableSubscriber.onNext(FluxHide.java:137)
at reactor.core.publisher.FluxOnErrorResume$ResumeSubscriber.onNext(FluxOnErrorResume.java:79)
at reactor.core.publisher.MonoFlatMap$FlatMapMain.onNext(MonoFlatMap.java:158)
at reactor.core.publisher.FluxMapFuseable$MapFuseableSubscriber.onNext(FluxMapFuseable.java:129)
at reactor.core.publisher.MonoFlatMap$FlatMapMain.onNext(MonoFlatMap.java:158)
at reactor.core.publisher.SerializedSubscriber.onNext(SerializedSubscriber.java:99)
at reactor.core.publisher.FluxRetryWhen$RetryWhenMainSubscriber.onNext(FluxRetryWhen.java:178)
at reactor.core.publisher.FluxOnErrorResume$ResumeSubscriber.onNext(FluxOnErrorResume.java:79)
at reactor.core.publisher.Operators$MonoInnerProducerBase.complete(Operators.java:2864)
at reactor.core.publisher.MonoSingle$SingleSubscriber.onComplete(MonoSingle.java:180)
at reactor.core.publisher.MonoFlatMapMany$FlatMapManyInner.onComplete(MonoFlatMapMany.java:261)
at reactor.core.publisher.FluxContextWrite$ContextWriteSubscriber.onComplete(FluxContextWrite.java:126)
at reactor.core.publisher.Operators$ScalarSubscription.request(Operators.java:2573)
at reactor.core.publisher.FluxContextWrite$ContextWriteSubscriber.request(FluxContextWrite.java:136)
at reactor.core.publisher.MonoFlatMapMany$FlatMapManyMain.onSubscribeInner(MonoFlatMapMany.java:150)
at reactor.core.publisher.MonoFlatMapMany$FlatMapManyInner.onSubscribe(MonoFlatMapMany.java:246)
at reactor.core.publisher.FluxContextWrite$ContextWriteSubscriber.onSubscribe(FluxContextWrite.java:101)
at reactor.core.publisher.FluxJust.subscribe(FluxJust.java:68)
at reactor.core.publisher.Flux.subscribe(Flux.java:8891)
at reactor.core.publisher.MonoFlatMapMany$FlatMapManyMain.onNext(MonoFlatMapMany.java:196)
at reactor.core.publisher.SerializedSubscriber.onNext(SerializedSubscriber.java:99)
at reactor.core.publisher.FluxRetryWhen$RetryWhenMainSubscriber.onNext(FluxRetryWhen.java:178)
at reactor.core.publisher.MonoCreate$DefaultMonoSink.success(MonoCreate.java:176)
at reactor.netty.http.client.HttpClientConnect$HttpIOHandlerObserver.onStateChange(HttpClientConnect.java:435)
at reactor.netty.ReactorNetty$CompositeConnectionObserver.onStateChange(ReactorNe
从“BlobDownloadHeaders.getContentLength()”的 Blob 存储下载文件时出错
由于
getContentLength()
方法返回null而发生错误。此方法用于获取正在从 blob 存储下载的内容的长度。
您可以使用以下代码检查 blob 大小,并使用 Azure Java SDK 将内容下载为字节数组。
代码:
public static void main(String[] args) {
try {
String customerId = "customer123";
String docTypeId = "doc456";
String fileName = "example.txt";
byte[] blobData = getBlobAsByteArray(customerId, docTypeId, fileName);
System.out.println("Blob downloaded successfully, size: " + blobData.length + " bytes");
} catch (Exception e) {
System.err.println("Error downloading blob: " + e.getMessage());
e.printStackTrace();
}
}
public static byte[] getBlobAsByteArray(String customerId, String docTypeId, String fileName) {
String blobName = formBlobName(customerId, docTypeId, fileName);
System.out.println(blobName);
BlobClient blobClient = configureBlobClient(blobName);
try {
// Check if blob exists
if (!blobClient.exists()) {
throw new RuntimeException("Blob does not exist: " + blobName);
}
// Fetch blob properties and log metadata
BlobProperties properties = blobClient.getProperties();
System.out.println("Blob Size: " + properties.getBlobSize() + " bytes");
// Download blob content
try (ByteArrayOutputStream outputStream = new ByteArrayOutputStream()) {
blobClient.downloadStream(outputStream);
return outputStream.toByteArray();
}
} catch (BlobStorageException e) {
throw new RuntimeException("BlobStorageException occurred: " + e.getErrorCode(), e);
} catch (IOException e) {
throw new RuntimeException("IOException occurred while downloading blob: " + blobName, e);
}
}
private static String formBlobName(String customerId, String docTypeId, String fileName) {
return customerId + "/" + docTypeId + "/" + fileName;
}
private static BlobClient configureBlobClient(String blobName) {
return new BlobClientBuilder()
.connectionString(CONNECTION_STRING)
.containerName(CONTAINER_NAME)
.blobName(blobName)
.buildClient();
}
上面的代码将检查 blob 是否存在,获取其属性,并将其内容下载为字节数组。最后,它记录下载的 blob 的大小。
输出:
customer123/doc456/example.txt
Blob Size: 1739 bytes
Blob downloaded successfully, size: 1739 bytes
参考: