Perforce Chronicle 2012.2/486814
API Documentation
|
Provide an Amazon S3 backend for Asset Handling. More...
Public Member Functions | |
__construct (array $options=null) | |
Constructor allows passing options for accessKey, secretKey or bucket. | |
exists ($id) | |
Check if the given ID exists in storage. | |
getAccessKey () | |
Returns the s3 access key. | |
getBucket () | |
Returns the bucket to store objects under. | |
getSecretKey () | |
Returns the s3 access key. | |
isOffsite () | |
Used to determine if the asset handler will be storing assets offsite or not. | |
put ($id, $data) | |
Store the passed data using indicated ID. | |
setAccessKey ($key) | |
Used to set the s3 access key. | |
setBucket ($bucket) | |
Used to set the bucket for storage. | |
setSecretKey ($key) | |
Used to set the s3 secret key. | |
uri ($id) | |
Provide a URI for the indicated asset ID. | |
Protected Member Functions | |
_getHeaders ($id) | |
This method will provide the appropriate headers for the passed id. | |
_getS3Service () | |
Returns the s3 service instance in use. | |
_toObjectId ($id) | |
Translates a Asset ID to S3 Object ID. | |
Protected Attributes | |
$_accessKey | |
$_bucket | |
$_s3Service | |
$_secretKey |
Provide an Amazon S3 backend for Asset Handling.
Storing generated assets in S3 moves them to a fast cookie free host which should give a speed boost. Further, if you are horizontally scaling your web servers this will provide a shared data store.
P4Cms_AssetHandler_S3::__construct | ( | array $ | options = null | ) |
Constructor allows passing options for accessKey, secretKey or bucket.
array | $options | Options to use on this instance |
{ foreach ($options ?: array() as $option => $value) { $method = 'set' . ucfirst($option); if (method_exists($this, $method)) { $this->$method($value); } } }
P4Cms_AssetHandler_S3::_getHeaders | ( | $ | id | ) | [protected] |
This method will provide the appropriate headers for the passed id.
It ensures the asset is publicly available and properly detects the cssgz and jsgz file extensions to set content encoding/type as needed.
string | $id | The id to generate headers for |
{ $headers = array( Zend_Service_Amazon_S3::S3_ACL_HEADER => Zend_Service_Amazon_S3::S3_ACL_PUBLIC_READ ); // the S3 class won't detect our compressed assets // correctly so we manually setup the headers here. $extension = substr(strrchr($id, '.'), 1); if ($extension == 'cssgz') { $headers['Content-Type'] = 'text/css'; $headers['Content-Encoding'] = 'gzip'; } else if ($extension == 'jsgz') { $headers['Content-Type'] = 'text/javascript'; $headers['Content-Encoding'] = 'gzip'; } return $headers; }
P4Cms_AssetHandler_S3::_getS3Service | ( | ) | [protected] |
Returns the s3 service instance in use.
{ if (!$this->_s3Service) { $this->_s3Service = new Zend_Service_Amazon_S3( $this->getAccessKey(), $this->getSecretKey() ); } return $this->_s3Service; }
P4Cms_AssetHandler_S3::_toObjectId | ( | $ | id | ) | [protected] |
Translates a Asset ID to S3 Object ID.
This is a simple task of tacking the bucket name on the front.
string | $id | The id to translate to an object ID |
{ return $this->getBucket() . '/' . $id; }
P4Cms_AssetHandler_S3::exists | ( | $ | id | ) |
Check if the given ID exists in storage.
string | $id | The ID to test |
Implements P4Cms_AssetHandlerInterface.
{ $cacheId = 's3_asset_' . md5($id); if (P4Cms_Cache::load($cacheId)) { return true; } // if we can retreive meta-data cache result and return true $info = $this->_getS3Service()->getInfo($this->_toObjectId($id)); if ($info) { P4Cms_Cache::save(true, $cacheId); return true; } // we don't cache failures as we will, most likely, // upload a copy shortly; simply return false return false; }
P4Cms_AssetHandler_S3::getAccessKey | ( | ) |
Returns the s3 access key.
{
return $this->_accessKey;
}
P4Cms_AssetHandler_S3::getBucket | ( | ) |
Returns the bucket to store objects under.
The DEFAULT_BUCKET constant will be used if no value has been provided.
{
return $this->_bucket;
}
P4Cms_AssetHandler_S3::getSecretKey | ( | ) |
Returns the s3 access key.
{
return $this->_secretKey;
}
P4Cms_AssetHandler_S3::isOffsite | ( | ) |
Used to determine if the asset handler will be storing assets offsite or not.
Assets such as CSS need to know this so they can decide if they need to include the site's url when referencing images.
Implements P4Cms_AssetHandlerInterface.
{ return true; }
P4Cms_AssetHandler_S3::put | ( | $ | id, |
$ | data | ||
) |
Store the passed data using indicated ID.
Will clobber any existing entry with the same ID.
string | $id | The ID to store under |
string | $data | The data to store |
Implements P4Cms_AssetHandlerInterface.
{ return $this->_getS3Service()->putObject( $this->_toObjectId($id), $data, $this->_getHeaders($id) ); }
P4Cms_AssetHandler_S3::setAccessKey | ( | $ | key | ) |
Used to set the s3 access key.
string | null | $key | The access key to use |
{ if (!is_string($key) && !is_null($key)) { throw new InvalidArgumentException("Access Key must be a string or null"); } $this->_s3Service = null; $this->_accessKey = $key; return $this; }
P4Cms_AssetHandler_S3::setBucket | ( | $ | bucket | ) |
Used to set the bucket for storage.
string | null | $bucket | The bucket to store assets under |
{ if (!is_string($bucket) && !is_null($bucket)) { throw new InvalidArgumentException("Bucket must be a string or null"); } $this->_bucket = $bucket; return $this; }
P4Cms_AssetHandler_S3::setSecretKey | ( | $ | key | ) |
Used to set the s3 secret key.
string | null | $key | The secret key to use |
{ if (!is_string($key) && !is_null($key)) { throw new InvalidArgumentException("Secret Key must be a string or null"); } $this->_s3Service = null; $this->_secretKey = $key; return $this; }
P4Cms_AssetHandler_S3::uri | ( | $ | id | ) |
Provide a URI for the indicated asset ID.
string | $id | The ID to get a URI for |
Implements P4Cms_AssetHandlerInterface.
{ return 'http://' . Zend_Service_Amazon_S3::S3_ENDPOINT . '/' . $this->getBucket() . '/' . $id; }
P4Cms_AssetHandler_S3::$_accessKey [protected] |
P4Cms_AssetHandler_S3::$_bucket [protected] |
P4Cms_AssetHandler_S3::$_s3Service [protected] |
P4Cms_AssetHandler_S3::$_secretKey [protected] |