[英]How to upload a Flux java object into Azure Blob Storage?
我正在尝试将 Flux object 上传到 azure blob 存储中,但我不确定如何使用 BlobAsyncClient 发送 Flux pojo。 BlobAsyncClient 具有采用 Flux 或 BinaryData 的上传方法,但我没有运气尝试将 CombinedResponse 转换为 BYteBuffer 或 BinaryData。 有没有人有任何建议或知道如何将通量 object 上传到 blob 存储?
您将需要一个异步 blob 容器客户端:
@Bean("blobServiceClient")
BlobContainerAsyncClient blobServiceClient(ClientSecretCredential azureClientCredentials, String storageAccount, String containerName) {
BlobServiceClientBuilder blobServiceClientBuilder = new BlobServiceClientBuilder();
return blobServiceClientBuilder
.endpoint(format("https://%s.blob.core.windows.net/", storageAccount))
.credential(azureClientCredentials)
.buildAsyncClient()
.getBlobContainerAsyncClient(containerName);
}
在您的代码中,您可以使用它来获取客户端,并将您的 Flux 保存到它:
Flux<ByteBuffer> content = getContent();
blobServiceClient.getBlobAsyncClient(id)
.upload(content, new ParallelTransferOptions(), true);
我知道getContent()
步骤是您正在努力解决的部分。 您可以保存BinaryData
object 或Flux<ByteBuffer>
stream。
要将 object 转换为 BinaryData object,请使用 static 辅助方法:
BinaryData foo = BinaryData.fromObject(myObject);
BinaryData 顾名思义:二进制数据。 例如图像文件的内容。
如果你想把它变成一个 ByteBuffer,记住你试图在这里把一个 object 变成一个 stream 的数据。 您可能希望使用某种标准化的方式来执行此操作,以便可以可靠地反转它,而不是 stream 的字节,如果您将数据加载到不同的客户端,甚至只是相同的不同版本,则可能会中断,我们通常保存object的json或xml表示。
我的首选工具是 Jackson:
byte[] myBytes = new ObjectMapper().writeValueAsBytes(myObject);
var myByteBuffer = ByteBuffer.wrap(myBytes);
并将其作为 Flux 返回:
Flux<ByteBuffer> myFlux = Flux.just(myByteBuffer);
顺便说一下,Azure 在BinaryData.fromObject()
方法中使用了 JSON 序列化程序。 来自 JavaDoc:
Creates an instance of BinaryData by serializing the Object using the default JsonSerializer.
Note: This method first looks for a JsonSerializerProvider
implementation on the classpath. If no implementation is found, a
default Jackson-based implementation will be used to serialize the object
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.