[英]How to upload a Flux java object into Azure Blob Storage?
I am trying to upload a Flux object into azure blob storage, but I'm not sure how to send a Flux pojo using BlobAsyncClient.我正在尝试将 Flux object 上传到 azure blob 存储中,但我不确定如何使用 BlobAsyncClient 发送 Flux pojo。 BlobAsyncClient has upload methods that take Flux or BinaryData but I have no luck trying to convert CombinedResponse to BYteBuffer or BinaryData.
BlobAsyncClient 具有采用 Flux 或 BinaryData 的上传方法,但我没有运气尝试将 CombinedResponse 转换为 BYteBuffer 或 BinaryData。 Does anyone have any suggestions or know how to upload a flux object to blob storage?
有没有人有任何建议或知道如何将通量 object 上传到 blob 存储?
You will need an asynch blob container client:您将需要一个异步 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);
}
And in your code you can use it to get a client, and save your Flux to it:在您的代码中,您可以使用它来获取客户端,并将您的 Flux 保存到它:
Flux<ByteBuffer> content = getContent();
blobServiceClient.getBlobAsyncClient(id)
.upload(content, new ParallelTransferOptions(), true);
I get that the getContent()
step is the part you are struggling with.我知道
getContent()
步骤是您正在努力解决的部分。 You can save either a BinaryData
object or a Flux<ByteBuffer>
stream.您可以保存
BinaryData
object 或Flux<ByteBuffer>
stream。
To turn your object into a BinaryData object, use the static helper method:要将 object 转换为 BinaryData object,请使用 static 辅助方法:
BinaryData foo = BinaryData.fromObject(myObject);
BinaryData is meant for exactly what the name says: binary data. BinaryData 顾名思义:二进制数据。 For example the content of an image file.
例如图像文件的内容。
If you want to turn it into a ByteBuffer, keep in mind that you're trying to turn an object into a stream of data here.如果你想把它变成一个 ByteBuffer,记住你试图在这里把一个 object 变成一个 stream 的数据。 You will probably want to use some standardized way of doing that, so it can be reliably reversed, so rather than a stream of bytes that may break if you ever load the data in a different client, or even just a different version of the same, we usually save a json or xml representation of the object.
您可能希望使用某种标准化的方式来执行此操作,以便可以可靠地反转它,而不是 stream 的字节,如果您将数据加载到不同的客户端,甚至只是相同的不同版本,则可能会中断,我们通常保存object的json或xml表示。
My go-to tool for this is Jackson:我的首选工具是 Jackson:
byte[] myBytes = new ObjectMapper().writeValueAsBytes(myObject);
var myByteBuffer = ByteBuffer.wrap(myBytes);
And return it as a Flux:并将其作为 Flux 返回:
Flux<ByteBuffer> myFlux = Flux.just(myByteBuffer);
By the way, Azure uses a JSON serializer under the hood in the BinaryData.fromObject()
method.顺便说一下,Azure 在
BinaryData.fromObject()
方法中使用了 JSON 序列化程序。 From the JavaDoc:来自 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.