Private Bulk Upload to Amazon S3 in Drupal

If you are using Amazon S3 services and need to upload large files or you want an ad-hoc "page" to upload large files that it can be downloaded by another party, S3 gives a trivially easy way to do this.
1. Create a bucket in S3
2. You'll need a policy and signature to upload. use the simple php script to generate it

$policy_doc = '{"expiration": "2199-01-01T00:00:00Z",' . "\n" .
'"conditions": [' . "\n" .
'{"bucket": "YOURBUCKETNAME"},' . "\n" .
'["starts-with", "$key", "UPLOADFOLDER/"],' . "\n" .
'{"acl": "private"},' . "\n" .
'{"success_action_redirect": "REDIRECTURLAFTERUPLOAD"},' . "\n" .
'{"content-type": "application/zip"},' . "\n" .
']' . "\n" .
$policy = base64_encode($policy_doc);
$signature= base64_encode(hash_hmac('sha1', $policy, 'YOURSECRETKEY', true));

echo 'Policy:' . $policy . "\n";
echo 'Signature:' . $signature . "\n";

echo "\n";

3. Run the PHP from command line with php myfile.php or call it with a URL and note the policy and signature
4. Create a simple HTML page

<form action="YOURS3BUCKETURL" method="post" enctype="multipart/form-data">
<input type="hidden" name="key" value="UPLOADFOLDER/${filename}">
<input type="hidden" name="AWSAccessKeyId" value="YOURAWSACCESSKEY">
<input type="hidden" name="acl" value="private">
<input type="hidden" name="success_action_redirect" value="REDIRECTURLAFTERUPLOAD">
<input type="hidden" name="content-type" value="application/zip">
<input type="hidden" name="policy" value="POLICYGENERATEDABOVE">
<input type="hidden" name="signature" value="SIGNATUREGENERATEDABOVE">
<input name="file" type="file">
<input type="submit" value="Upload">

5. You need to ensure that the REDIRECTURLAFTERUPLOAD and UPLOADFOLDER are exactly the same in both the places.
6. Now upload the HTML you have created and integrate it with Drupal or host is as a simple page in AWS S3 and send the link to those who need to upload the files.
The files uploaded to S3 will be accessible only to those who have the appropriate permission in the S3 bucket.

Post new comment