gtx911 gtx911 - 1 year ago 70
Java Question

Mule & Java - Byte[] to File

I get a

with two nodes:

  1. The file name with the extension -

  2. The content - Encoded with Base64 -

With that data I want to put an object in my
Amazon S3
but it requires a
type parameter.

I use this to transform to

byte[] arrayDecoded = Base64.getDecoder().decode(node2JSON);

I want to transform the byte[ ] to File in a java class Java and then put it to Amazon S3.

How can I do that without using files from a directory?

Answer Source

You can use the AmazonS3#putObject(String bucketName, String key, InputStream input,ObjectMetadata metadata) method to upload the data without writing a file to the filesystem.
Specify the length of the file in the metadata and use a ByteArrayInputStream to convert your byte array to an input stream.

ObjectMetadata objectMetadata = new ObjectMetadata();
amazonS3.putObject(bucketName, filename, new ByteArrayInputStream(data), objectMetadata);