/ Published in: PHP
This script generates an expiring link to an Amazon S3 file
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
<?php // ----------------------------------------------------------------------- // /** * Calculate the HMAC SHA1 hash of a string. * * @param string $key The key to hash against * @param string $data The data to hash * @param int $blocksize Optional blocksize * @return string HMAC SHA1 */ function el_crypto_hmacSHA1($key, $data, $blocksize = 64) { ($key ^ $ipad) . $data )) )); } } // ----------------------------------------------------------------------- // /** * Create temporary URLs to your protected Amazon S3 files. * * @param string $accessKey Your Amazon S3 access key * @param string $secretKey Your Amazon S3 secret key * @param string $bucket The bucket (bucket.s3.amazonaws.com) * @param string $path The target file path * @param int $expires In minutes * @return string Temporary Amazon S3 URL * @see http://awsdocs.s3.amazonaws.com/S3/20060301/s3-dg-20060301.pdf */ function el_s3_getTemporaryLink($accessKey, $secretKey, $bucket, $path, $expires = 5) { // Calculate expiry time // Fix the path; encode and sanitize // Path for signature starts with the bucket $signpath = '/'. $bucket .'/'. $path; // S3 friendly string to sign // Calculate the hash $signature = el_crypto_hmacSHA1($secretKey, $signsz); // Glue the URL ... // ... to the query string ... 'AWSAccessKeyId' => $accessKey, 'Expires' => $expires, 'Signature' => $signature, ));a // ... and return the URL! return $url.'?'.$qs; } }