mikebertiean mikebertiean - 1 year ago 170
Java Question

Uploading Image to S3 Bucket - Java SDK

I am working on an iOS application which connects to my Tomcat/Jersey server.

The way I am currently uploading images to S3 is using the following workflow:

- upload image to a temp folder on my server using a POST request Multipart Form data

- take the file from the temp folder and using the Amazon Java SDK I upload it to S3

- delete the image file from the temp folder on the server once upload is completed to S3

I do not want my iOS app to upload directly to S3 as I want to go through my server to perform some operations but in my opinion this seems redundant and will make the process slower than it may need to be. Is there a way to stream the file directly through the Amazon SDK instead of having to temp save it to my server?

Answer Source

Thanks for the suggestions. I used your answers to solve it by uploading the Input Stream directly through the Amazon SDK like so:

byte[] contents = IOUtils.toByteArray(inputStream);
InputStream stream = new ByteArrayInputStream(contents);

ObjectMetadata meta = new ObjectMetadata();

s3client.putObject(new PutObjectRequest(
        bucketName, fileName, stream, meta)


Where inputStream is the input stream received from the iOS application to my server and s3client is the AmazonS3Client initialization with the BasicAWSCredentials.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download