Home > Blockchain >  How to upload a Flux java object into Azure Blob Storage?
How to upload a Flux java object into Azure Blob Storage?

Time:12-06

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. BlobAsyncClient has upload methods that take Flux or BinaryData but I have no luck trying to convert CombinedResponse to BYteBuffer or BinaryData. Does anyone have any suggestions or know how to upload a flux object to blob storage?

CodePudding user response:

You will need an asynch blob container client:

@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<ByteBuffer> content = getContent();
blobServiceClient.getBlobAsyncClient(id)
            .upload(content, new ParallelTransferOptions(), true);

I get that the getContent() step is the part you are struggling with. You can save either a BinaryData object or a Flux<ByteBuffer> stream.

To turn your object into a BinaryData object, use the static helper method:

BinaryData foo = BinaryData.fromObject(myObject);

BinaryData is meant for exactly what the name says: binary data. 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. 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.

My go-to tool for this is Jackson:

byte[] myBytes = new ObjectMapper().writeValueAsBytes(myObject);

var myByteBuffer = ByteBuffer.wrap(myBytes);

And return it as a Flux:

Flux<ByteBuffer> myFlux = Flux.just(myByteBuffer);

By the way, Azure uses a JSON serializer under the hood in the BinaryData.fromObject() method. From the 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
  • Related