Asynchronous POST Uploads to Amazon S3

Sample Code & Libraries>Asynchronous POST Uploads to Amazon S3
Community Contributed Software

  • Amazon Web Services provides links to these packages as a convenience for our customers, but software not authored by an "@AWS" account has not been reviewed or screened by AWS.
  • Please review this software to ensure it meets your needs before using it.

These samples demonstrate how to use the Amazon S3 browser-based uploads using POST and the Yahoo! User Interface Library to create an asynchronous client-side file upload.

Details

Submitted By: Michael@AWS
AWS Products Used: Amazon S3
Language(s): JavaScript, PHP
License: Apache License 2.0
Created On: July 10, 2008 6:55 AM GMT
Last Updated: April 4, 2009 2:37 PM GMT
Download

About This Sample

  • Upload a file to Amazon S3 directly from a web-browser using an off-screen IFRAME generated by the Yahoo! User Interface Library so that the upload is handled in the background of the browser.
  • Demonstrates the creation and signing of a POST policy statement in PHP.
  • Demonstrates the use of the success_action_redirect form field to notify your server of a successful upload and allow your server to send a response to the client's browser.

Updates

[04.04.2009] Added an Amazon S3 POST policy class.

Prerequisites

  • You must be signed up to use Amazon S3. You can sign up for Amazon S3 at http://aws.amazon.com/s3
  • You need your AWS Access Key ID and Secret Access Key
  • PHP 5 or greater

Running the Sample

  1. Open config.php
  2. Set the $AWSAccessKeyId variable to your AWS Access Key ID.
  3. Set the $AWSSecretKey variable to your AWS Secret Key.
  4. Set the $bucket variable to a bucket you own. If the bucket does not already exists, you will need to create it.
  5. Run sample.php or sample-attachments.php from a web-server.

Upload a file to Amazon S3 using a HTML Form POST:

  1. User clicks to submit a file to upload, and YUI creates an off-screen IFRAME to upload the file to Amazon S3.
  2. The file is sent via POST to Amazon S3 inside of the IFRAME.
  3. From within the IFRAME, Amazon S3 generates a redirect URL to the success_action_redirect url located on your server.
  4. The IFRAME makes a request to the redirect URL on your Webserver.
  5. Your Webserver returns data in JSON.
  6. The IFRAME sends JSON to the page.

Comments

Nice example - minor bug
This worked well once I fixed a minor bug. Line 34 of sample.php should be: $s3policy = new Aws_S3_PostPolicy($AWSAccessKeyId, $AWSSecretKey, $bucket, 86400); For my app, the uploaded files should not be publicly browseable, so I changed line 35 to: $s3policy->addCondition('', 'acl', 'private') The callback.php URL on your server must be publicly accessible. For maintaining and debugging S3 file contents I recommend S3Tools (http://s3tools.org/s3cmd)
Paul Hamill on January 5, 2010 4:57 PM GMT
We are temporarily not accepting new comments.
©2015, Amazon Web Services, Inc. or its affiliates. All rights reserved.