Last active
August 29, 2015 14:21
-
-
Save zacscott/13844f51925946af92c7 to your computer and use it in GitHub Desktop.
AWS S3 Signed URL
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** @file s3_signed_url.php - provides the s3_signed_url() function to produce time-limited URL's using AWS S3. | |
* Based off; http://dev.wattswork.com/make-signed-urls-for-amazon-s3-with-php/ | |
*/ | |
if ( !function_exists('s3_signed_url' ) ) { | |
/** | |
* Create signed URLs to your protected Amazon S3 files. | |
* | |
* @param string $awsAccessKey Your Amazon S3 access key | |
* @param string $secretKey Your Amazon S3 secret key | |
* @param string $bucket The bucket (mybucket.s3.amazonaws.com) | |
* @param string $objectPath The target file path | |
* @param int $expires In minutes | |
* @return string Temporary signed Amazon S3 URL | |
* @see http://awsdocs.s3.amazonaws.com/S3/20060301/s3-dg-20060301.pdf | |
*/ | |
function s3_signed_url( $awsAccessKey, $secretKey, $bucket, $objectPath, $expires = 5 ) { | |
// Calculate the expire time. | |
$expires = time() + intval( $expires * 60 ); | |
// Clean and url-encode the object path. | |
// $objectPath = str_replace( array( '%2F', '%2B' ), array('/', '+'), rawurlencode( ltrim($objectPath, '/') ) ); | |
$objectPath = urlencode( trim( $objectPath ) ); | |
// Create the object path for use in the signature. | |
$objectPathForSignature = '/'. $bucket .'/'. $objectPath; | |
// Create the S3 friendly string to sign. | |
$stringToSign = implode("\n", $pieces = array('GET', null, null, $expires, $objectPathForSignature)); | |
// Create the base URL to the object | |
$url = 'http://' . $bucket . '.s3.amazonaws.com/' . $objectPath; | |
// Hash the string-to-sign to create the signature. | |
$signature = base64_encode( hash_hmac( 'sha1', $stringToSign, $secretKey, true ) ); | |
// Append generated AWS parameters to the URL. | |
$queries = http_build_query( $pieces = array( | |
'AWSAccessKeyId' => $awsAccessKey, | |
'Expires' => $expires, | |
'Signature' => $signature, | |
) ); | |
$url .= '?'. $queries; | |
// Return the URL. | |
return $url; | |
} | |
} | |
/* Copyright (c) 2015, Zachary Scott <zscott.dev@gmail.com> | |
* | |
* Permission is hereby granted, free of charge, to any person obtaining a copy | |
* of this software and associated documentation files (the "Software"), to deal | |
* in the Software without restriction, including without limitation the rights | |
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
* copies of the Software, and to permit persons to whom the Software is | |
* furnished to do so, subject to the following conditions: | |
* | |
* The above copyright notice and this permission notice shall be included | |
* in all copies or substantial portions of the Software. | |
* | |
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL | |
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING | |
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER | |
* DEALINGS IN THE SOFTWARE. | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment