简体   繁体   English

如何将 Flux java object 上传到 Azure Blob 存储?

[英]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.

相关问题 使用 Java 将文件上传到 Azure Blob 存储 - Upload a file to Azure Blob storage using Java 在Java和Azure Blob存储SDK中上载带有子目录的目录 - Upload directory with sub-directories in java and azure blob storage sdk 是否可以使用 resttemplate 在 azure blob 存储中上传文件 - Java - Is it possible to use resttemplate to upload file in azure blob storage - Java 如何在 Java 中将 AppendBlob/大于 4mb 限制的文件上传到 Azure 存储/Blob? - How to upload AppendBlob/a file larger than the 4mb limit into Azure Storage/Blob in Java? 如何使用 java 代码将文件作为 blob 从 Internet 上传到 Azure 存储? - How to upload a file from internet to azure storage as a blob using java code? 如何使用 java sdk 在 Azure Blob 存储中上传单个视频文件的多个块? - How to upload multiple chunks of a single video file in Azure Blob Storage using java sdk? 如何通过 Apache Beam 将文件上传到 Azure blob Storage? - How to upload a file to Azure blob Storage by Apache Beam? 如何使用com.microsoft.azure.storage.blob.CloudBlockBlob的upload() - How to use upload() of com.microsoft.azure.storage.blob.CloudBlockBlob 如何生成 SAS 令牌并使用它将文件上传到 Azure Blob 存储? - How to generate a SAS token and use it to upload a file to Azure Blob Storage? 从URL上传Azure存储Blob - Azure storage blob upload from url
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM