From 1024fe1e340652ba25afc1f0af35360357eefc33 Mon Sep 17 00:00:00 2001 From: Steve Farmer Date: Fri, 6 May 2016 11:35:13 -0400 Subject: [PATCH] New function to get an authenticated object URL for download. This change adds getAuthenticatedDownloadURL(), a variation of getAuthenticatedURL(), with a content disposition added to trigger downloading the object when the URL is opened. --- S3.php | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/S3.php b/S3.php index 660844c4..9a78594d 100644 --- a/S3.php +++ b/S3.php @@ -1188,6 +1188,27 @@ public static function getAuthenticatedURL($bucket, $uri, $lifetime, $hostBucket } + /** + * Get a query string authenticated URL to download an object + * + * @param string $bucket Bucket name + * @param string $uri Object URI + * @param integer $lifetime Lifetime in seconds + * @param boolean $hostBucket Use the bucket name as the hostname + * @param boolean $https Use HTTPS ($hostBucket should be false for SSL verification) + * @return string + */ + public static function getAuthenticatedDownloadURL($bucket, $uri, $lifetime, $hostBucket = false, $https = false) + { + $expires = self::__getTime() + $lifetime; + $uri = str_replace(array('%2F', '%2B'), array('/', '+'), rawurlencode($uri)); + return sprintf(($https ? 'https' : 'http').'://%s/%s?AWSAccessKeyId=%s&Expires=%u&response-content-disposition=attachment&Signature=%s', + // $hostBucket ? $bucket : $bucket.'.s3.amazonaws.com', $uri, self::$__accessKey, $expires, + $hostBucket ? $bucket : self::$endpoint.'/'.$bucket, $uri, self::$__accessKey, $expires, + urlencode(self::__getHash("GET\n\n\n{$expires}\n/{$bucket}/{$uri}?response-content-disposition=attachment"))); + } + + /** * Get a CloudFront signed policy URL *