PHP:
// Inspiration from http://birkoff.net/blog/post-files-directly-to-s3-with-php/
// require the AWS SDK for PHP library
require 'aws-autoloader.php';
use Aws\S3\S3Client;
// User configured fields
$access_key = 'Your_Access_Key';
$secret_key = 'Your_Secret_Key';
$bucket = 'Your-Bucket';
$acl = 'public-read'; // if you prefer you can use 'private'
$success_action_redirect = 'http://www.dreamhost.com';
// Establish connection with DreamObjects with an S3 client.
$client = S3Client::factory(array(
'base_url' => HOST,
'key' => $access_key ,
'secret' => $secret_key
));
// Create the timestamp we will call it $expire
$now = strtotime(date("Y-m-d\TG:i:s"));
$expire = gmdate('Y-m-d\TG:i:s\Z', strtotime('+ 10 minutes', $now)); // credentials valid 10 minutes from now
$url = 'https://'.$bucket.'.objects.dreamhost.com';
$policy_document='
{"expiration": "'.$expire.'",
"conditions": [
{"bucket": "'.$bucket.'"},
["starts-with", "$key", "uploads/"],
{"acl": "'.$acl.'"},
{"success_action_redirect": "'.$success_action_redirect.'"},
["starts-with", "$Content-Type", ""]
]
}';
// create policy
$policy = base64_encode($policy_document);
// create signature
$signature = base64_encode(hash_hmac("sha1", $policy, $secret_key, $raw_output = true));
HTML:
DreamObjects POST Form
Javascript:
// add open script
// Guess the mimetype and update the Content-Type
// Inspired from http://jsbin.com/akati3/2
file = document.getElementById('file');
file.addEventListener('change', function() {
document.forms.value = file.files[0].type;
}, false);
// add closing script
Original Source: https://discussion.dreamhost.com/thread-141720.html