SAAS-516: Replace Zend_Service_Amazon_S3 with AWS SDK for PHP

This commit is contained in:
drigato 2014-12-09 17:58:36 -05:00
parent ec189fc27a
commit 644e6b00d8
4 changed files with 84 additions and 74 deletions

View File

@ -10,6 +10,9 @@ if (!@include_once('propel/propel1/runtime/lib/Propel.php'))
Propel::init(__DIR__."/configs/airtime-conf-production.php"); Propel::init(__DIR__."/configs/airtime-conf-production.php");
//Composer's autoloader
require_once 'autoload.php';
require_once __DIR__."/configs/constants.php"; require_once __DIR__."/configs/constants.php";
require_once 'Preference.php'; require_once 'Preference.php';
require_once 'Locale.php'; require_once 'Locale.php';

View File

@ -2,71 +2,60 @@
require_once 'StorageBackend.php'; require_once 'StorageBackend.php';
use Aws\S3\S3Client;
class Amazon_S3 extends StorageBackend class Amazon_S3 extends StorageBackend
{ {
private $zendServiceAmazonS3;
private $s3Client;
public function Amazon_S3($securityCredentials) public function Amazon_S3($securityCredentials)
{ {
$this->setBucket($securityCredentials['bucket']); $this->setBucket($securityCredentials['bucket']);
$this->setAccessKey($securityCredentials['api_key']); $this->setAccessKey($securityCredentials['api_key']);
$this->setSecretKey($securityCredentials['api_key_secret']); $this->setSecretKey($securityCredentials['api_key_secret']);
$this->zendServiceAmazonS3 = new Zend_Service_Amazon_S3( $this->s3Client = S3Client::factory(array(
$this->getAccessKey(), 'key' => $securityCredentials['api_key'],
$this->getSecretKey()); 'secret' => $securityCredentials['api_key_secret'],
));
} }
public function getAbsoluteFilePath($resourceId) public function getAbsoluteFilePath($resourceId)
{ {
$endpoint = $this->zendServiceAmazonS3->getEndpoint(); return $this->s3Client->getObjectUrl($this->getBucket(), $resourceId);
$scheme = $endpoint->getScheme();
$host = $endpoint->getHost();
$bucket = $this->getBucket();
return "$scheme://$bucket.$host/".utf8_encode($resourceId);
} }
public function getSignedURL($resourceId) public function getSignedURL($resourceId)
{ {
//URL will be active for 30 minutes return $this->s3Client->getObjectUrl($this->getBucket(), $resourceId, '+60 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) public function getFileSize($resourceId)
{ {
$bucket = $this->getBucket(); $obj = $this->s3Client->getObject(array(
'Bucket' => $this->getBucket(),
$amz_resource = utf8_encode("$bucket/$resourceId"); 'Key' => $resourceId,
$amz_resource_info = $this->zendServiceAmazonS3->getInfo($amz_resource); ));
return (int)$amz_resource_info["size"]; if (isset($obj["ContentLength"])) {
return (int)$obj["ContentLength"];
} else {
return 0;
}
} }
public function deletePhysicalFile($resourceId) public function deletePhysicalFile($resourceId)
{ {
$bucket = $this->getBucket(); $bucket = $this->getBucket();
$amz_resource = utf8_encode("$bucket/$resourceId");
if ($this->s3Client->doesObjectExist($bucket, $resourceId)) {
if ($this->zendServiceAmazonS3->isObjectAvailable($amz_resource)) {
// removeObject() returns true even if the object was not deleted (bug?) $result = $this->s3Client->deleteObject(array(
// so that is not a good way to do error handling. isObjectAvailable() 'Bucket' => $bucket,
// does however return the correct value; We have to assume that if the 'Key' => $resourceId,
// object is available the removeObject() function will work. ));
$this->zendServiceAmazonS3->removeObject($amz_resource); } else {
} else { throw new Exception("ERROR: Could not locate file to delete.");
throw new Exception("ERROR: Could not locate object on Amazon S3"); }
}
} }
} }

View File

@ -1,5 +1,6 @@
{ {
"require": { "require": {
"propel/propel1": "1.7.0-stable" "propel/propel1": "1.7.0-stable",
"aws/aws-sdk-php": "2.7.9"
} }
} }

81
composer.lock generated
View File

@ -4,32 +4,32 @@
"Read more about it at http://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file", "Read more about it at http://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file",
"This file is @generated automatically" "This file is @generated automatically"
], ],
"hash": "5776cc789514d71faac7021474c96d58", "hash": "311983cf9bb84cfacbfcc6c5dfc9e841",
"packages": [ "packages": [
{ {
"name": "aws/aws-sdk-php", "name": "aws/aws-sdk-php",
"version": "dev-master", "version": "2.7.9",
"source": { "source": {
"type": "git", "type": "git",
"url": "https://github.com/aws/aws-sdk-php.git", "url": "https://github.com/aws/aws-sdk-php.git",
"reference": "c540fa2c66daf91383a2cd7fc044765f2da2aa52" "reference": "f39354df58eec97f0ef22ccf3caf753607a47dca"
}, },
"dist": { "dist": {
"type": "zip", "type": "zip",
"url": "https://api.github.com/repos/aws/aws-sdk-php/zipball/c540fa2c66daf91383a2cd7fc044765f2da2aa52", "url": "https://api.github.com/repos/aws/aws-sdk-php/zipball/f39354df58eec97f0ef22ccf3caf753607a47dca",
"reference": "c540fa2c66daf91383a2cd7fc044765f2da2aa52", "reference": "f39354df58eec97f0ef22ccf3caf753607a47dca",
"shasum": "" "shasum": ""
}, },
"require": { "require": {
"guzzle/guzzle": ">=3.7,<4", "guzzle/guzzle": "~3.7",
"php": ">=5.3.3" "php": ">=5.3.3"
}, },
"require-dev": { "require-dev": {
"doctrine/cache": "~1.0", "doctrine/cache": "~1.0",
"ext-openssl": "*", "ext-openssl": "*",
"monolog/monolog": "1.4.*", "monolog/monolog": "~1.4",
"phpunit/phpunit": "4.*", "phpunit/phpunit": "~4.0",
"symfony/yaml": "2.*" "symfony/yaml": "~2.1"
}, },
"suggest": { "suggest": {
"doctrine/cache": "Adds support for caching of credentials and responses", "doctrine/cache": "Adds support for caching of credentials and responses",
@ -41,7 +41,7 @@
"type": "library", "type": "library",
"extra": { "extra": {
"branch-alias": { "branch-alias": {
"dev-master": "2.7.x-dev" "dev-master": "2.7-dev"
} }
}, },
"autoload": { "autoload": {
@ -71,7 +71,7 @@
"s3", "s3",
"sdk" "sdk"
], ],
"time": "2014-10-08 19:18:30" "time": "2014-12-08 21:56:46"
}, },
{ {
"name": "guzzle/guzzle", "name": "guzzle/guzzle",
@ -167,24 +167,38 @@
}, },
{ {
"name": "phing/phing", "name": "phing/phing",
"version": "2.8.2", "version": "2.9.1",
"source": { "source": {
"type": "git", "type": "git",
"url": "https://github.com/phingofficial/phing.git", "url": "https://github.com/phingofficial/phing.git",
"reference": "345e8716122cf6c6b59c4894701d8b736f27fca9" "reference": "393edeffa8a85d43636ce0c9b4deb1ff9ac60a5c"
}, },
"dist": { "dist": {
"type": "zip", "type": "zip",
"url": "https://api.github.com/repos/phingofficial/phing/zipball/345e8716122cf6c6b59c4894701d8b736f27fca9", "url": "https://api.github.com/repos/phingofficial/phing/zipball/393edeffa8a85d43636ce0c9b4deb1ff9ac60a5c",
"reference": "345e8716122cf6c6b59c4894701d8b736f27fca9", "reference": "393edeffa8a85d43636ce0c9b4deb1ff9ac60a5c",
"shasum": "" "shasum": ""
}, },
"require": { "require": {
"php": ">=5.2.0" "php": ">=5.2.0"
}, },
"require-dev": { "require-dev": {
"ext-pdo_sqlite": "*",
"lastcraft/simpletest": "@dev",
"pdepend/pdepend": "1.x",
"pear-pear.php.net/http_request2": "2.2.x",
"pear-pear.php.net/net_growl": "2.7.x",
"pear-pear.php.net/pear_packagefilemanager": "1.7.x",
"pear-pear.php.net/pear_packagefilemanager2": "1.0.x",
"pear-pear.php.net/xml_serializer": "0.20.x",
"pear/pear_exception": "@dev",
"pear/versioncontrol_git": "@dev",
"pear/versioncontrol_svn": "@dev",
"phpdocumentor/phpdocumentor": "2.x", "phpdocumentor/phpdocumentor": "2.x",
"phpunit/phpunit": ">=3.7" "phploc/phploc": "2.x",
"phpunit/phpunit": ">=3.7",
"sebastian/phpcpd": "2.x",
"squizlabs/php_codesniffer": "1.5.x"
}, },
"suggest": { "suggest": {
"pdepend/pdepend": "PHP version of JDepend", "pdepend/pdepend": "PHP version of JDepend",
@ -203,6 +217,11 @@
"bin/phing" "bin/phing"
], ],
"type": "library", "type": "library",
"extra": {
"branch-alias": {
"dev-master": "2.9.x-dev"
}
},
"autoload": { "autoload": {
"classmap": [ "classmap": [
"classes/phing/" "classes/phing/"
@ -216,14 +235,13 @@
"LGPL-3.0" "LGPL-3.0"
], ],
"authors": [ "authors": [
{
"name": "Michiel Rook",
"email": "mrook@php.net",
"role": "Lead"
},
{ {
"name": "Phing Community", "name": "Phing Community",
"homepage": "http://www.phing.info/trac/wiki/Development/Contributors" "homepage": "http://www.phing.info/trac/wiki/Development/Contributors"
},
{
"name": "Michiel Rook",
"email": "mrook@php.net"
} }
], ],
"description": "PHing Is Not GNU make; it's a PHP project build system or build tool based on Apache Ant.", "description": "PHing Is Not GNU make; it's a PHP project build system or build tool based on Apache Ant.",
@ -234,7 +252,7 @@
"task", "task",
"tool" "tool"
], ],
"time": "2014-07-18 10:23:54" "time": "2014-12-03 09:18:46"
}, },
{ {
"name": "propel/propel1", "name": "propel/propel1",
@ -301,17 +319,17 @@
}, },
{ {
"name": "symfony/event-dispatcher", "name": "symfony/event-dispatcher",
"version": "v2.5.5", "version": "v2.6.1",
"target-dir": "Symfony/Component/EventDispatcher", "target-dir": "Symfony/Component/EventDispatcher",
"source": { "source": {
"type": "git", "type": "git",
"url": "https://github.com/symfony/EventDispatcher.git", "url": "https://github.com/symfony/EventDispatcher.git",
"reference": "f6281337bf5f985f585d1db6a83adb05ce531f46" "reference": "720fe9bca893df7ad1b4546649473b5afddf0216"
}, },
"dist": { "dist": {
"type": "zip", "type": "zip",
"url": "https://api.github.com/repos/symfony/EventDispatcher/zipball/f6281337bf5f985f585d1db6a83adb05ce531f46", "url": "https://api.github.com/repos/symfony/EventDispatcher/zipball/720fe9bca893df7ad1b4546649473b5afddf0216",
"reference": "f6281337bf5f985f585d1db6a83adb05ce531f46", "reference": "720fe9bca893df7ad1b4546649473b5afddf0216",
"shasum": "" "shasum": ""
}, },
"require": { "require": {
@ -320,7 +338,8 @@
"require-dev": { "require-dev": {
"psr/log": "~1.0", "psr/log": "~1.0",
"symfony/config": "~2.0", "symfony/config": "~2.0",
"symfony/dependency-injection": "~2.0,<2.6.0", "symfony/dependency-injection": "~2.6",
"symfony/expression-language": "~2.6",
"symfony/stopwatch": "~2.2" "symfony/stopwatch": "~2.2"
}, },
"suggest": { "suggest": {
@ -330,7 +349,7 @@
"type": "library", "type": "library",
"extra": { "extra": {
"branch-alias": { "branch-alias": {
"dev-master": "2.5-dev" "dev-master": "2.6-dev"
} }
}, },
"autoload": { "autoload": {
@ -354,15 +373,13 @@
], ],
"description": "Symfony EventDispatcher Component", "description": "Symfony EventDispatcher Component",
"homepage": "http://symfony.com", "homepage": "http://symfony.com",
"time": "2014-09-28 15:56:11" "time": "2014-12-02 20:19:20"
} }
], ],
"packages-dev": [], "packages-dev": [],
"aliases": [], "aliases": [],
"minimum-stability": "stable", "minimum-stability": "stable",
"stability-flags": { "stability-flags": [],
"aws/aws-sdk-php": 20
},
"prefer-stable": false, "prefer-stable": false,
"platform": [], "platform": [],
"platform-dev": [] "platform-dev": []