Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 968
mikebertiean Avatar asked Mar 14 '23 04:03

mikebertiean


1 Answers

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();
meta.setContentLength(contents.length);
meta.setContentType("image/png");

s3client.putObject(new PutObjectRequest(
        bucketName, fileName, stream, meta)
        .withCannedAcl(CannedAccessControlList.Private));

inputStream.close();

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

like image 145
mikebertiean Avatar answered Mar 16 '23 04:03

mikebertiean