SAAS-505: Extract Amazon_S3 class and have it inherit from a general 'cloud backend' class

This commit is contained in:
drigato 2014-12-01 21:05:46 -05:00
parent 432245b18e
commit 7c0a25be7f
7 changed files with 194 additions and 133 deletions

View File

@ -1,72 +0,0 @@
<?php
require_once 'Zend/Service/Amazon/S3.php';
/**
*
* This is a wrapper class for Zend_Service_Amazon_S3.
* Zend_Service_Amazon_S3 doesn't have getters and setters for the bucket,
* access key, or secret key. This functionality would greatly refine our
* use of the service so we decided to write our own here.
*
*/
class Amazon_S3
{
private $bucket;
private $accessKey;
private $secretKey;
private $zendServiceAmazonS3;
function Amazon_S3()
{
$this->initZendServiceAmazonS3();
}
private function initZendServiceAmazonS3()
{
$CC_CONFIG = Config::getConfig();
$this->setBucket($CC_CONFIG['storage_backend']['bucket']);
$this->setAccessKey($CC_CONFIG['storage_backend']['api_key']);
$this->setSecretKey($CC_CONFIG['storage_backend']['api_key_secret']);
$this->zendServiceAmazonS3 = new Zend_Service_Amazon_S3(
$this->getAccessKey(),
$this->getSecretKey());
}
public function getZendServiceAmazonS3()
{
return $this->zendServiceAmazonS3;
}
public function getBucket()
{
return $this->bucket;
}
private function setBucket($bucket)
{
$this->bucket = $bucket;
}
public function getAccessKey()
{
return $this->accessKey;
}
private function setAccessKey($accessKey)
{
$this->accessKey = $accessKey;
}
public function getSecretKey()
{
return $this->secretKey;
}
private function setSecretKey($secretKey)
{
$this->secretKey = $secretKey;
}
}

View File

@ -0,0 +1,74 @@
<?php
require_once 'StorageBackend.php';
class Amazon_S3 extends StorageBackend
{
private $zendServiceAmazonS3;
public function Amazon_S3()
{
$CC_CONFIG = Config::getConfig();
$this->setBucket($CC_CONFIG['storage_backend']['bucket']);
$this->setAccessKey($CC_CONFIG['storage_backend']['api_key']);
$this->setSecretKey($CC_CONFIG['storage_backend']['api_key_secret']);
$this->zendServiceAmazonS3 = new Zend_Service_Amazon_S3(
$this->getAccessKey(),
$this->getSecretKey());
}
public function getAbsoluteFilePath($resourceId)
{
$endpoint = $this->zendServiceAmazonS3->getEndpoint();
$scheme = $endpoint->getScheme();
$host = $endpoint->getHost();
$bucket = $this->getBucket();
return "$scheme://$bucket.$host/".utf8_encode($resourceId);
}
public function getSignedURL($resourceId)
{
//URL will be active for 30 minutes
$expires = time()+1800;
$bucket = $this->getBucket();
$secretKey = $this->getSecretKey();
$accessKey = $this->getAccessKey();
$string_to_sign = utf8_encode("GET\n\n\n$expires\n/$bucket/$resourceId");
// We need to urlencode the entire signature in case the hashed signature
// has spaces. (NOTE: utf8_encode() does not work here because it turns
// spaces into non-breaking spaces)
$signature = urlencode(base64_encode((hash_hmac("sha1", $string_to_sign, $secretKey, true))));
$resourceURL = $this->getAbsoluteFilePath($resourceId);
return $resourceURL."?AWSAccessKeyId=$accessKey&Expires=$expires&Signature=$signature";
}
public function getFileSize($resourceId)
{
$bucket = $this->getBucket();
$amz_resource = utf8_encode("$bucket/$resourceId");
$amz_resource_info = $this->zendServiceAmazonS3->getInfo($amz_resource);
return $amz_resource_info["size"];
}
public function deletePhysicalFile($resourceId)
{
$bucket = $this->getBucket();
$amz_resource = utf8_encode("$bucket/$resourceId");
if ($this->zendServiceAmazonS3->isObjectAvailable($amz_resource)) {
// removeObject() returns true even if the object was not deleted (bug?)
// so that is not a good way to do error handling. isObjectAvailable()
// does however return the correct value; We have to assume that if the
// object is available the removeObject() function will work.
$this->zendServiceAmazonS3->removeObject($amz_resource);
} else {
throw new Exception("ERROR: Could not locate object on Amazon S3");
}
}
}

View File

@ -0,0 +1,44 @@
<?php
require_once 'StorageBackend.php';
require_once 'Amazon_S3.php';
/**
*
* Controls access to the storage backend class where a file is stored.
*
*/
class ProxyStorageBackend extends StorageBackend
{
private $storageBackend;
/**
* Receives the file's storage backend and instantiates the approriate
* object.
*/
public function ProxyStorageBackend($storageBackend)
{
$this->storageBackend = new $storageBackend();
}
public function getAbsoluteFilePath($resourceId)
{
return $this->storageBackend->getAbsoluteFilePath($resourceId);
}
public function getSignedURL($resourceId)
{
return $this->storageBackend->getSignedURL($resourceId);
}
public function getFileSize($resourceId)
{
return $this->storageBackend->getFileSize($resourceId);
}
public function deletePhysicalFile($resourceId)
{
$this->storageBackend->deletePhysicalFile($resourceId);
}
}

View File

@ -0,0 +1,55 @@
<?php
/**
*
* Provides access to file objects stored on a specific storage backend.
*/
abstract class StorageBackend
{
private $bucket;
private $accessKey;
private $secretKey;
/** Returns the file object's URL to the storage backend it is located on. */
abstract public function getAbsoluteFilePath($resourceId);
/** Returns the file object's signed URL. The URL must be signed since they
* privately stored on the storage backend. */
abstract public function getSignedURL($resourceId);
/** Returns the file's size in bytes. */
abstract public function getFileSize($resourceId);
/** Deletes the file from the storage backend. */
abstract public function deletePhysicalFile($resourceId);
protected function getBucket()
{
return $this->bucket;
}
protected function setBucket($bucket)
{
$this->bucket = $bucket;
}
protected function getAccessKey()
{
return $this->accessKey;
}
protected function setAccessKey($accessKey)
{
$this->accessKey = $accessKey;
}
protected function getSecretKey()
{
return $this->secretKey;
}
protected function setSecretKey($secretKey)
{
$this->secretKey = $secretKey;
}
}

View File

@ -29,6 +29,7 @@ class Config {
$cloudStorageConfig = isset($_SERVER['CLOUD_STORAGE_CONF']) ? $_SERVER['CLOUD_STORAGE_CONF'] : "/etc/airtime-saas/cloud_storage.conf";
$cloudStorageValues = parse_ini_file($cloudStorageConfig, true);
$currentStorageBackend = $cloudStorageValues['current_backend']['storage_backend'];
$CC_CONFIG['current_backend'] = $cloudStorageValues['current_backend']['storage_backend'];
$CC_CONFIG['storage_backend'] = $cloudStorageValues[$currentStorageBackend];
$values = parse_ini_file($filename, true);

View File

@ -1,13 +1,13 @@
<?php
require_once 'Amazon_S3.php';
require_once 'ProxyStorageBackend.php';
/**
* Skeleton subclass for representing a row from the 'cloud_file' table.
*
* Each cloud_file has a corresponding cc_file referenced as a foreign key.
* The file's metadata is stored in the cc_file table. This, cloud_file,
* table represents files that are stored on Amazon S3.
* table represents files that are stored in the cloud.
*
* You should add additional methods to this class to meet the
* application requirements. This class will only be generated as
@ -17,6 +17,7 @@ require_once 'Amazon_S3.php';
*/
class CloudFile extends BaseCloudFile
{
private $proxyStorageBackend;
/**
* Returns a signed URL to the file's object on Amazon S3. Since we are
@ -25,7 +26,10 @@ class CloudFile extends BaseCloudFile
*/
public function getURLForTrackPreviewOrDownload()
{
return $this->getAbsoluteFilePath()."?".$this->getAuthenticationParams();
if ($this->proxyStorageBackend == null) {
$this->proxyStorageBackend = new ProxyStorageBackend($this->getStorageBackend());
}
return $this->proxyStorageBackend->getSignedURL($this->getResourceId());
}
/**
@ -34,39 +38,10 @@ class CloudFile extends BaseCloudFile
*/
public function getAbsoluteFilePath()
{
$amazon_s3 = new Amazon_S3();
$zend_s3 = $amazon_s3->getZendServiceAmazonS3();
$resource_id = $this->getResourceId();
$endpoint = $zend_s3->getEndpoint();
$scheme = $endpoint->getScheme();
$host = $endpoint->getHost();
$s3_bucket = $amazon_s3->getBucket();
return "$scheme://$s3_bucket.$host/".utf8_encode($resource_id);
}
/**
*
* Returns a string of authentication paramaters to append to the cloud
* object's URL. We need this for track preview and download because the
* objects are privately stored on Amazon S3.
*/
public function getAuthenticationParams()
{
$expires = time()+120;
$resource_id = $this->getResourceId();
$amazon_s3 = new Amazon_S3();
$s3_bucket = $amazon_s3->getBucket();
$s3_secret_key = $amazon_s3->getSecretKey();
$s3_access_key = $amazon_s3->getAccessKey();
$string_to_sign = utf8_encode("GET\n\n\n$expires\n/$s3_bucket/$resource_id");
// We need to urlencode the entire signature in case the hashed signature
// has spaces. (NOTE: utf8_encode() does not work here because it turns
// spaces into non-breaking spaces)
$signature = urlencode(base64_encode((hash_hmac("sha1", $string_to_sign, $s3_secret_key, true))));
return "AWSAccessKeyId=$s3_access_key&Expires=$expires&Signature=$signature";
if ($this->proxyStorageBackend == null) {
$this->proxyStorageBackend = new ProxyStorageBackend($this->getStorageBackend());
}
return $this->proxyStorageBackend->getAbsoluteFilePath($this->getResourceId());
}
/**
@ -74,15 +49,10 @@ class CloudFile extends BaseCloudFile
*/
public function getFileSize()
{
$amazon_s3 = new Amazon_S3();
$zend_s3 = $amazon_s3->getZendServiceAmazonS3();
$bucket = $amazon_s3->getBucket();
$resource_id = $this->getResourceId();
$amz_resource = utf8_encode("$bucket/$resource_id");
$amz_resource_info = $zend_s3->getInfo($amz_resource);
return $amz_resource_info["size"];
if ($this->proxyStorageBackend == null) {
$this->proxyStorageBackend = new ProxyStorageBackend($this->getStorageBackend());
}
return $this->proxyStorageBackend->getFileSize($this->getResourceId());
}
public function getFilename()
@ -119,21 +89,10 @@ class CloudFile extends BaseCloudFile
*/
public function deletePhysicalFile()
{
$amazon_s3 = new Amazon_S3();
$zend_s3 = $amazon_s3->getZendServiceAmazonS3();
$bucket = $amazon_s3->getBucket();
$resource_id = $this->getResourceId();
$amz_resource = utf8_encode("$bucket/$resource_id");
if ($zend_s3->isObjectAvailable($amz_resource)) {
// removeObject() returns true even if the object was not deleted (bug?)
// so that is not a good way to do error handling. isObjectAvailable()
// does however return the correct value; We have to assume that if the
// object is available the removeObject() function will work.
$zend_s3->removeObject($amz_resource);
} else {
throw new Exception("ERROR: Could not locate object on Amazon S3");
if ($this->proxyStorageBackend == null) {
$this->proxyStorageBackend = new ProxyStorageBackend($this->getStorageBackend());
}
$this->proxyStorageBackend->deletePhysicalFile($this->getResourceId());
}
/**

View File

@ -57,8 +57,8 @@ if (file_exists('/usr/share/php/libzend-framework-php')) {
set_include_path('/usr/share/php/libzend-framework-php' . PATH_SEPARATOR . get_include_path());
}
//amazon directory
set_include_path(APPLICATION_PATH . '/amazon' . PATH_SEPARATOR . get_include_path());
//cloud storage directory
set_include_path(APPLICATION_PATH . '/cloud_storage' . PATH_SEPARATOR . get_include_path());
/** Zend_Application */
require_once 'Zend/Application.php';