Java Azure blob存储(sdk v10) - 在继续前进之前,如何确保上传完成? [英] Java Azure blob storage (sdk v10) - how do I ensure upload has finished before moving forward?
问题描述
我有一个web服务,它接收一个文件并将其上传到Azure blob存储帐户
我上传的代码在上传之前返回true已经完成,如何确保上传完成后再向前移动?
尝试 {
AsynchronousFileChannel fileChannel = AsynchronousFileChannel。 open (file.toPath());
TransferManager。 uploadFileToBlockBlob (fileChannel , blob , 8 * 1024 * 1024 ,null,null )
.subscribe(azureResponse - > {
logger .info("完成的上传请求。");
logger 。 info("状态代码:" + azureResponse.response()。statusCode());
if (azureResponse.response()。statusCode()!= 201 ) {
logger .error(" upload failed - " + azureResponse.response ()。body());
//处理错误
}
});
//不要通过这一点除非出现错误或上传已完成
} catch (例外e){
logger .error(" upload failed - " + e.getMessage());
//处理错误
} 最后 {
返回true;
}
检查同步版本的API规范,否则你需要以不同方式为异步操作编写
代码。看起来SDK正在使用RxJava,这非常好。
似乎有一个.blockingGet() 方法
,你可以调用而不是.subscribe(),它应该使它同步
尝试上述建议并告诉我状态。
请让我们知道上述内容是否有帮助,或者您需要在这个问题上需要进一步的帮助。
--------------------------------------- -------------------------------------------------- -
点击帮助您的帖子上的"标记为答案",这对其他
社区成员有益。
Hi, I have a webservice that takes a file and uploads it to an Azure blob storage account
The code I have at the moment returns true before the upload has finished, how do I ensure the upload has finished before moving forward?
try {
AsynchronousFileChannel fileChannel = AsynchronousFileChannel.open(file.toPath());
TransferManager.uploadFileToBlockBlob(fileChannel, blob, 8*1024*1024, null, null)
.subscribe( azureResponse -> {
logger.info("Completed upload request.");
logger.info("Status code: " + azureResponse.response().statusCode());
if( azureResponse.response().statusCode() != 201){
logger.error("upload failed - " + azureResponse.response().body());
// handle error
}
});
// do not pass this point unless there's an error or upload has finished
} catch (Exception e) {
logger.error("upload failed - " + e.getMessage());
// handle error
} finally{
return true;
}解决方案Check the API spec for a synchronous version or otherwise you need write the code differently for the async operation. Looks like the SDK is using RxJava which pretty good.
There seems to be a .blockingGet() method that you can call instead of .subscribe() which should make it synchronous
Try the above-mentioned suggestion and let me know the status.
Kindly let us know if the above helps or you need further assistance on this issue.
------------------------------------------------------------------------------------------
Do click on "Mark as Answer" on the post that helps you, this can be beneficial to other community members.
这篇关于Java Azure blob存储(sdk v10) - 在继续前进之前,如何确保上传完成?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文