gtx911 gtx911 - 5 months ago 24
Java Question

Mule & Java - Byte[] to File

I get a

JSON
with two nodes:


  1. The file name with the extension -
    String
    .

  2. The content - Encoded with Base64 -
    String
    .



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

I use this to transform to
byte[]
too:

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

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();
objectMetadata.setContentLength(data.length);
amazonS3.putObject(bucketName, filename, new ByteArrayInputStream(data), objectMetadata);
Comments