简体   繁体   中英

Java image upload stored in Amazon S3

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?

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

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

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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM