Example #1
0
/**
 * Functions registered with the worker
 *
 * @param object $job 
 * @return void
 */
function remove_cdn_file($job)
{
    //Get the info of the job
    list($remoteFilename) = unserialize($job->workload());
    //Do some checks
    if (empty($remoteFilename)) {
        logError(sprintf("%s: The workload do not contain the required info to remove a file from the CDN\n\n", date('r')));
        $job->sendFail();
        return FALSE;
    }
    echo sprintf("%s: Received job to remove %s from the CDN\n", date('r'), $remoteFilename);
    //Get the Amazon S3 client
    $config = getConfig();
    $amazonS3Client = Zend_Cloud_StorageService_Factory::getAdapter(array(Zend_Cloud_StorageService_Factory::STORAGE_ADAPTER_KEY => 'Zend_Cloud_StorageService_Adapter_S3', Zend_Cloud_StorageService_Adapter_S3::AWS_ACCESS_KEY => $config->amazon->aws_access_key, Zend_Cloud_StorageService_Adapter_S3::AWS_SECRET_KEY => $config->amazon->aws_private_key));
    //Get the Amazon Cloud Front client
    $cloudFrontConfig = array('accessKey' => $config->amazon->aws_access_key, 'privateKey' => $config->amazon->aws_private_key, 'distributionId' => $config->amazon->cloudfront->distribution_id);
    $cloudFront = new App_Amazon_CloudFront($cloudFrontConfig);
    //Remove from S3
    echo sprintf("%s: Removing file from S3\n", date('r'));
    try {
        $amazonS3Client->deleteItem($remoteFilename, array(Zend_Cloud_StorageService_Adapter_S3::BUCKET_NAME => $config->amazon->s3->assets_bucket));
    } catch (Zend_Cloud_StorageService_Exception $e) {
        logError(sprintf("%s: Error removing the file %s from S3\n\n", date('r'), $remoteFilename));
        $job->sendFail();
        return FALSE;
    }
    echo sprintf("%s: File %s removed from S3\n", date('r'), $remoteFilename);
    //Invalidate the image in the CDN
    echo sprintf("%s: Invalidating file %s from the CDN\n", date('r'), $remoteFilename);
    if (!$cloudFront->invalidate($remoteFilename)) {
        logError(sprintf("%s: Error invalidating the file %s in the CDN\n\n", date('r'), $remoteFilename));
        $job->sendFail();
        return FALSE;
    }
    echo sprintf("%s: File %s invalidated from the CDN\n", date('r'), $remoteFilename);
    $job->sendComplete(TRUE);
    echo sprintf("%s: Job finished successfully\n\n", date('r'));
    return TRUE;
}
Example #2
0
 public function testNoParams()
 {
     $config = array(Zend_Cloud_StorageService_Factory::STORAGE_ADAPTER_KEY => $this->_config->get(Zend_Cloud_StorageService_Factory::STORAGE_ADAPTER_KEY));
     $this->setExpectedException('Zend_Cloud_StorageService_Exception');
     $s = Zend_Cloud_StorageService_Factory::getAdapter($config);
 }
 public function testGetAdapterWithArray()
 {
     // No need to overdo it; we'll test the array config with just one adapter.
     $fileSystemConfig = array(Zend_Cloud_StorageService_Factory::STORAGE_ADAPTER_KEY => 'Zend_Cloud_StorageService_Adapter_FileSystem', Zend_Cloud_StorageService_Adapter_FileSystem::LOCAL_DIRECTORY => dirname(__FILE__) . "/_files/data");
     $fileSystemAdapter = Zend_Cloud_StorageService_Factory::getAdapter($fileSystemConfig);
     $this->assertEquals('Zend_Cloud_StorageService_Adapter_FileSystem', get_class($fileSystemAdapter));
 }
<?php

$storage = Zend_Cloud_StorageService_Factory::getAdapter(array(Zend_Cloud_StorageService_Factory::STORAGE_ADAPTER_KEY => 'Zend_Cloud_StorageService_Adapter_S3', Zend_Cloud_StorageService_Adapter_S3::AWS_ACCESS_KEY => 'chiave di accesso', Zend_Cloud_StorageService_Adapter_S3::AWS_SECRET_KEY => 'chiave segreta'));
$data = file_get_contents('/my/local/dir/picture.jpg');
$returnedData = $storage->storeItem('/my/remote/path/picture.jpg', $data);
// Utilizzo di S3 bucket: test
// Rende il file pubblico
$returnedData = $storage->storeItem('/my/remote/path/picture.jpg', $data, array(Zend_Cloud_StorageService_Adapter_S3::BUCKET_NAME => "test", Zend_Cloud_StorageService_Adapter_S3::METADATA => array(Zend_Service_Amazon_S3::S3_ACL_HEADER => Zend_Service_Amazon_S3::S3_ACL_PUBLIC_READ)));
Example #5
0
/**
 * Upload the file to Amazon
 *
 * @param string $localFilename 
 * @param string $remoteFilename
 * @return boolean
 */
function upload($localFilename, $remoteFilename)
{
    //Create the Amazon S3 client
    $config = getConfig();
    $amazonS3Client = Zend_Cloud_StorageService_Factory::getAdapter(array(Zend_Cloud_StorageService_Factory::STORAGE_ADAPTER_KEY => 'Zend_Cloud_StorageService_Adapter_S3', Zend_Cloud_StorageService_Adapter_S3::AWS_ACCESS_KEY => $config->amazon->aws_access_key, Zend_Cloud_StorageService_Adapter_S3::AWS_SECRET_KEY => $config->amazon->aws_private_key));
    //Get the contents of the file
    $fileContent = file_get_contents(ROOT_PATH . '/public/frontend/tmp/' . $localFilename);
    $fileInfo = new SplFileInfo(ROOT_PATH . '/public/frontend/tmp/' . $localFilename);
    //Upload the file to Amazon
    echo sprintf("%s: Uploading file to Amazon S3\n", date('r'));
    try {
        $amazonS3Client->storeItem($remoteFilename, $fileContent, array(Zend_Cloud_StorageService_Adapter_S3::BUCKET_NAME => $config->amazon->s3->assets_bucket, Zend_Cloud_StorageService_Adapter_S3::METADATA => array(Zend_Service_Amazon_S3::S3_ACL_HEADER => Zend_Service_Amazon_S3::S3_ACL_PUBLIC_READ)));
        echo sprintf("%s: File uploaded to Amazon S3 (%s bytes)\n", date('r'), $fileInfo->getSize());
        return TRUE;
    } catch (Zend_Cloud_StorageService_Exception $e) {
        return $e;
    }
}
 public function preDispatch()
 {
     $this->_storage = Zend_Cloud_StorageService_Factory::getAdapter($this->config->storage);
 }
Example #7
0
 /**
  * This method will instantiate the object, configure it and return it
  *
  * @return Zend_Cache_Manager
  */
 public static function getInstance()
 {
     $config = App_DI_Container::get('ConfigObject');
     $storage = Zend_Cloud_StorageService_Factory::getAdapter(array(Zend_Cloud_StorageService_Factory::STORAGE_ADAPTER_KEY => 'Zend_Cloud_StorageService_Adapter_S3', Zend_Cloud_StorageService_Adapter_S3::AWS_ACCESS_KEY => $config->amazon->aws_access_key, Zend_Cloud_StorageService_Adapter_S3::AWS_SECRET_KEY => $config->amazon->aws_private_key));
     return $storage;
 }