3

Having trouble finding some examples showing how I can use Java to allow users to upload an image to Amazon S3.

The flow is:

  1. User is on HTML form with file input form element.

  2. This form submits the selected image to a Servlet.

  3. This Servlet processes the image and stores it in S3.

Anyone know of any good links/tutorials that outline sample code to perform this?

Richard Heap
  • 48,344
  • 9
  • 130
  • 112
Stamford
  • 39
  • 1
  • 2

3 Answers3

8

For the 3rd point:

  • Grab jets3t
  • It's tutorial is simple. Here's a snippet I'm using:

    S3Object fileObject = new S3Object(path);
    fileObject.setDataInputStream(is);
    s3service.putObject(bucketName, fileObject);
    

For the previous two points - look at this question

Community
  • 1
  • 1
Bozho
  • 588,226
  • 146
  • 1,060
  • 1,140
  • Thanks! I've made some progress and am at the point where I am trying to put the object into S3. The documentation says "If your data isn't a File or String you can use any input stream as a data source, but you must manually set the Content-Length." I am grabbing the uploaded image as an InputStream. I am not sure how to extract the Content-Length to set it here: s3FileObject.setContentLength(contentLength); I have tried converting the InputStream to a String, to then grab the content-lenth, but the resulting uploaded image becomes corrupted. Any thoughts on how I can grab the content-lenth? – Stamford Sep 15 '11 at 21:28
  • to clarify, it actually does work without setting the content length, just gives me a warning. So figured better to figure out how to specify it: WARN [org.jets3t.service.impl.rest.httpclient.RestStorageService:1610] Content-Length of data stream not set, will automatically determine data length in memory – Stamford Sep 15 '11 at 22:07
  • `Integer.parseInt(request.getHeader("Content-Length")` should give you that – Bozho Sep 16 '11 at 05:40
1

Recommend you to use html amazon API to do this. Streaming is a bit complex and in most cases you do not need it.

Pavel
  • 11
  • 1
0

You can also use a simple form to uploda the file to the S3 Bucket. Look at this example http://aws.amazon.com/articles/1434

Example form:

<html> 
  <head>
    <title>S3 POST Form</title> 
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
  </head>

  <body> 
    <form action="https://s3-bucket.s3.amazonaws.com/" method="post" enctype="multipart/form-data">
      <input type="hidden" name="key" value="uploads/${filename}">
      <input type="hidden" name="AWSAccessKeyId" value="YOUR_AWS_ACCESS_KEY"> 
      <input type="hidden" name="acl" value="private"> 
      <input type="hidden" name="success_action_redirect" value="http://localhost/">
      <input type="hidden" name="policy" value="YOUR_POLICY_DOCUMENT_BASE64_ENCODED">
      <input type="hidden" name="signature" value="YOUR_CALCULATED_SIGNATURE">
      <input type="hidden" name="Content-Type" value="image/jpeg">
      <!-- Include any additional input fields here -->

      File to upload to S3: 
      <input name="file" type="file"> 
      <br> 
      <input type="submit" value="Upload File to S3"> 
    </form> 
  </body>
</html>
Koray Güclü
  • 2,857
  • 1
  • 34
  • 30