Refactored file storage code slightly to allow multiple download URLs

This commit is contained in:
Albert Santoni 2015-03-30 11:31:07 -04:00
parent 271dc266fa
commit d31de0937f
11 changed files with 78 additions and 35 deletions

View file

@ -41,15 +41,19 @@ class Amazon_S3StorageBackend extends StorageBackend
return $this->s3Client->getObjectUrl($this->getBucket(), $resourceId);
}
public function getSignedURL($resourceId)
/** Returns a signed download URL from Amazon S3, expiring in 60 minutes */
public function getDownloadURLs($resourceId)
{
$url = $this->s3Client->getObjectUrl($this->getBucket(), $resourceId, '+60 minutes');
$urls = array();
$signedS3Url = $this->s3Client->getObjectUrl($this->getBucket(), $resourceId, '+60 minutes');
//If we're using the proxy cache, we need to modify the request URL after it has
//been generated by the above. (The request signature must be for the amazonaws.com,
//not our proxy, since the proxy translates the host back to amazonaws.com)
if ($this->proxyHost) {
$p = parse_url($url);
$p = parse_url($signedS3Url);
$p["host"] = $this->getBucket() . "." . $this->proxyHost;
$p["scheme"] = "http";
//If the path contains the bucket name (which is the case with HTTPS requests to Amazon),
@ -60,13 +64,19 @@ class Amazon_S3StorageBackend extends StorageBackend
if (strpos($p["path"], $this->getBucket()) == 1) {
$p["path"] = substr($p["path"], 1 + strlen($this->getBucket()));
}
$url = $p["scheme"] . "://" . $p["host"] . $p["path"] . "?" . $p["query"];
$proxyUrl = $p["scheme"] . "://" . $p["host"] . $p["path"] . "?" . $p["query"];
//Add this proxy cache URL to the list of download URLs.
array_push($urls, $proxyUrl);
}
//Add the direct S3 URL to the list (as a fallback)
array_push($urls, $signedS3Url);
//http_build_url() would be nice to use but it requires pecl_http :-(
Logging::info($url);
//Logging::info($url);
return $url;
return $urls;
}
public function deletePhysicalFile($resourceId)