PHP‎ > ‎

Form Post to Amazon S3 using PHP

Example 1:   Single file form post.

<html>
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
  </head>
  <body>
 
<?php

  $form = array(
            'AWSAccessKeyId'            => 'your access key',
            'acl'                       => 'public-read',
            'success_action_redirect'   => 'http://yourdomain/uploadpicture.php',
        );
    

$form['policy'] = '{
            "expiration": "2016-12-01T12:00:00.000Z",
                "conditions": [
                    {
                        "acl": "'.$form['acl'].'"
                    },
                    {
                        "success_action_redirect": "'.$form['success_action_redirect'].'"
                    },

                    {
                        "bucket": "yourbucketname"
                    },
 ["starts-with", "$key", "uploads/"],
]
            }';

    $form['policy_encoded'] = base64_encode($form['policy']);
    $mysecretkey="your secret key";
    $mysignature = base64_encode(hash_hmac('sha1',$form['policy_encoded'] , $mysecretkey, true));
?>

<form action="https://yourbucket.s3.amazonaws.com/" method="post" enctype="multipart/form-data">
      <input type="hidden" name="key" value="uploads/${filename}">
      <input type="hidden" name="AWSAccessKeyId" value="<?php echo $form['AWSAccessKeyId'] ?>">
      <input type="hidden" name="acl" value="<?php echo $form['acl'] ?>">
      <input type="hidden" name="success_action_redirect" value="<?php echo $form['success_action_redirect'] ?>">
      <input type="hidden" name="policy" value="<?php echo $form['policy_encoded'] ?>">
      <input type="hidden" name="signature" value="<?php echo $mysignature; ?>">
      File to upload to S3:
      <input name="file" type="file">
      <br>
      <input type="submit" value="Upload File to S3">
</form>
</body>
</html>

Comments