PageRenderTime 49ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/src/application/libraries/Zend/Cloud/StorageService/Adapter/S3.php

https://bitbucket.org/masnug/grc276-blog-laravel
PHP | 327 lines | 152 code | 25 blank | 150 comment | 14 complexity | 0c0a59c7b1b0670d51032c7ddf5e3d03 MD5 | raw file
  1. <?php
  2. /**
  3. * LICENSE
  4. *
  5. * This source file is subject to the new BSD license that is bundled
  6. * with this package in the file LICENSE.txt.
  7. * It is also available through the world-wide-web at this URL:
  8. * http://framework.zend.com/license/new-bsd
  9. * If you did not receive a copy of the license and are unable to
  10. * obtain it through the world-wide-web, please send an email
  11. * to license@zend.com so we can send you a copy immediately.
  12. *
  13. * @category Zend
  14. * @package Zend_Cloud
  15. * @subpackage StorageService
  16. * @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com)
  17. * @license http://framework.zend.com/license/new-bsd New BSD License
  18. */
  19. require_once 'Zend/Service/Amazon/S3.php';
  20. require_once 'Zend/Cloud/StorageService/Adapter.php';
  21. require_once 'Zend/Cloud/StorageService/Exception.php';
  22. /**
  23. * S3 adapter for unstructured cloud storage.
  24. *
  25. * @category Zend
  26. * @package Zend_Cloud
  27. * @subpackage StorageService
  28. * @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com)
  29. * @license http://framework.zend.com/license/new-bsd New BSD License
  30. */
  31. class Zend_Cloud_StorageService_Adapter_S3
  32. implements Zend_Cloud_StorageService_Adapter
  33. {
  34. /*
  35. * Options array keys for the S3 adapter.
  36. */
  37. const BUCKET_NAME = 'bucket_name';
  38. const BUCKET_AS_DOMAIN = 'bucket_as_domain?';
  39. const FETCH_STREAM = 'fetch_stream';
  40. const METADATA = 'metadata';
  41. /**
  42. * AWS constants
  43. */
  44. const AWS_ACCESS_KEY = 'aws_accesskey';
  45. const AWS_SECRET_KEY = 'aws_secretkey';
  46. /**
  47. * S3 service instance.
  48. * @var Zend_Service_Amazon_S3
  49. */
  50. protected $_s3;
  51. protected $_defaultBucketName = null;
  52. protected $_defaultBucketAsDomain = false;
  53. /**
  54. * Constructor
  55. *
  56. * @param array|Zend_Config $options
  57. * @return void
  58. */
  59. public function __construct($options = array())
  60. {
  61. if ($options instanceof Zend_Config) {
  62. $options = $options->toArray();
  63. }
  64. if (!is_array($options)) {
  65. throw new Zend_Cloud_StorageService_Exception('Invalid options provided');
  66. }
  67. if (!isset($options[self::AWS_ACCESS_KEY]) || !isset($options[self::AWS_SECRET_KEY])) {
  68. throw new Zend_Cloud_StorageService_Exception('AWS keys not specified!');
  69. }
  70. try {
  71. $this->_s3 = new Zend_Service_Amazon_S3($options[self::AWS_ACCESS_KEY],
  72. $options[self::AWS_SECRET_KEY]);
  73. } catch (Zend_Service_Amazon_S3_Exception $e) {
  74. throw new Zend_Cloud_StorageService_Exception('Error on create: '.$e->getMessage(), $e->getCode(), $e);
  75. }
  76. if (isset($options[self::HTTP_ADAPTER])) {
  77. $this->_s3->getHttpClient()->setAdapter($options[self::HTTP_ADAPTER]);
  78. }
  79. if (isset($options[self::BUCKET_NAME])) {
  80. $this->_defaultBucketName = $options[self::BUCKET_NAME];
  81. }
  82. if (isset($options[self::BUCKET_AS_DOMAIN])) {
  83. $this->_defaultBucketAsDomain = $options[self::BUCKET_AS_DOMAIN];
  84. }
  85. }
  86. /**
  87. * Get an item from the storage service.
  88. *
  89. * @TODO Support streams
  90. *
  91. * @param string $path
  92. * @param array $options
  93. * @return string
  94. */
  95. public function fetchItem($path, $options = array())
  96. {
  97. $fullPath = $this->_getFullPath($path, $options);
  98. try {
  99. if (!empty($options[self::FETCH_STREAM])) {
  100. return $this->_s3->getObjectStream($fullPath, $options[self::FETCH_STREAM]);
  101. } else {
  102. return $this->_s3->getObject($fullPath);
  103. }
  104. } catch (Zend_Service_Amazon_S3_Exception $e) {
  105. throw new Zend_Cloud_StorageService_Exception('Error on fetch: '.$e->getMessage(), $e->getCode(), $e);
  106. }
  107. }
  108. /**
  109. * Store an item in the storage service.
  110. *
  111. * WARNING: This operation overwrites any item that is located at
  112. * $destinationPath.
  113. *
  114. * @TODO Support streams
  115. *
  116. * @param string $destinationPath
  117. * @param string|resource $data
  118. * @param array $options
  119. * @return void
  120. */
  121. public function storeItem($destinationPath, $data, $options = array())
  122. {
  123. try {
  124. $fullPath = $this->_getFullPath($destinationPath, $options);
  125. return $this->_s3->putObject(
  126. $fullPath,
  127. $data,
  128. empty($options[self::METADATA]) ? null : $options[self::METADATA]
  129. );
  130. } catch (Zend_Service_Amazon_S3_Exception $e) {
  131. throw new Zend_Cloud_StorageService_Exception('Error on store: '.$e->getMessage(), $e->getCode(), $e);
  132. }
  133. }
  134. /**
  135. * Delete an item in the storage service.
  136. *
  137. * @param string $path
  138. * @param array $options
  139. * @return void
  140. */
  141. public function deleteItem($path, $options = array())
  142. {
  143. try {
  144. $this->_s3->removeObject($this->_getFullPath($path, $options));
  145. } catch (Zend_Service_Amazon_S3_Exception $e) {
  146. throw new Zend_Cloud_StorageService_Exception('Error on delete: '.$e->getMessage(), $e->getCode(), $e);
  147. }
  148. }
  149. /**
  150. * Copy an item in the storage service to a given path.
  151. *
  152. * WARNING: This operation is *very* expensive for services that do not
  153. * support copying an item natively.
  154. *
  155. * @TODO Support streams for those services that don't support natively
  156. *
  157. * @param string $sourcePath
  158. * @param string $destination path
  159. * @param array $options
  160. * @return void
  161. */
  162. public function copyItem($sourcePath, $destinationPath, $options = array())
  163. {
  164. try {
  165. // TODO We *really* need to add support for object copying in the S3 adapter
  166. $item = $this->fetch($_getFullPath(sourcePath), $options);
  167. $this->storeItem($item, $destinationPath, $options);
  168. } catch (Zend_Service_Amazon_S3_Exception $e) {
  169. throw new Zend_Cloud_StorageService_Exception('Error on copy: '.$e->getMessage(), $e->getCode(), $e);
  170. }
  171. }
  172. /**
  173. * Move an item in the storage service to a given path.
  174. *
  175. * @TODO Support streams for those services that don't support natively
  176. *
  177. * @param string $sourcePath
  178. * @param string $destination path
  179. * @param array $options
  180. * @return void
  181. */
  182. public function moveItem($sourcePath, $destinationPath, $options = array())
  183. {
  184. try {
  185. $fullSourcePath = $this->_getFullPath($sourcePath, $options);
  186. $fullDestPath = $this->_getFullPath($destinationPath, $options);
  187. return $this->_s3->moveObject(
  188. $fullSourcePath,
  189. $fullDestPath,
  190. empty($options[self::METADATA]) ? null : $options[self::METADATA]
  191. );
  192. } catch (Zend_Service_Amazon_S3_Exception $e) {
  193. throw new Zend_Cloud_StorageService_Exception('Error on move: '.$e->getMessage(), $e->getCode(), $e);
  194. }
  195. }
  196. /**
  197. * Rename an item in the storage service to a given name.
  198. *
  199. *
  200. * @param string $path
  201. * @param string $name
  202. * @param array $options
  203. * @return void
  204. */
  205. public function renameItem($path, $name, $options = null)
  206. {
  207. require_once 'Zend/Cloud/OperationNotAvailableException.php';
  208. throw new Zend_Cloud_OperationNotAvailableException('Rename not implemented');
  209. }
  210. /**
  211. * List items in the given directory in the storage service
  212. *
  213. * The $path must be a directory
  214. *
  215. *
  216. * @param string $path Must be a directory
  217. * @param array $options
  218. * @return array A list of item names
  219. */
  220. public function listItems($path, $options = null)
  221. {
  222. try {
  223. // TODO Support 'prefix' parameter for Zend_Service_Amazon_S3::getObjectsByBucket()
  224. return $this->_s3->getObjectsByBucket($this->_defaultBucketName);
  225. } catch (Zend_Service_Amazon_S3_Exception $e) {
  226. throw new Zend_Cloud_StorageService_Exception('Error on list: '.$e->getMessage(), $e->getCode(), $e);
  227. }
  228. }
  229. /**
  230. * Get a key/value array of metadata for the given path.
  231. *
  232. * @param string $path
  233. * @param array $options
  234. * @return array
  235. */
  236. public function fetchMetadata($path, $options = array())
  237. {
  238. try {
  239. return $this->_s3->getInfo($this->_getFullPath($path, $options));
  240. } catch (Zend_Service_Amazon_S3_Exception $e) {
  241. throw new Zend_Cloud_StorageService_Exception('Error on fetch: '.$e->getMessage(), $e->getCode(), $e);
  242. }
  243. }
  244. /**
  245. * Store a key/value array of metadata at the given path.
  246. * WARNING: This operation overwrites any metadata that is located at
  247. * $destinationPath.
  248. *
  249. * @param string $destinationPath
  250. * @param array $options
  251. * @return void
  252. */
  253. public function storeMetadata($destinationPath, $metadata, $options = array())
  254. {
  255. require_once 'Zend/Cloud/OperationNotAvailableException.php';
  256. throw new Zend_Cloud_OperationNotAvailableException('Storing separate metadata is not supported, use storeItem() with \'metadata\' option key');
  257. }
  258. /**
  259. * Delete a key/value array of metadata at the given path.
  260. *
  261. * @param string $path
  262. * @param array $options
  263. * @return void
  264. */
  265. public function deleteMetadata($path)
  266. {
  267. require_once 'Zend/Cloud/OperationNotAvailableException.php';
  268. throw new Zend_Cloud_OperationNotAvailableException('Deleting metadata not supported');
  269. }
  270. /**
  271. * Get full path, including bucket, for an object
  272. *
  273. * @param string $path
  274. * @param array $options
  275. * @return void
  276. */
  277. protected function _getFullPath($path, $options)
  278. {
  279. if (isset($options[self::BUCKET_NAME])) {
  280. $bucket = $options[self::BUCKET_NAME];
  281. } else if (isset($this->_defaultBucketName)) {
  282. $bucket = $this->_defaultBucketName;
  283. } else {
  284. require_once 'Zend/Cloud/StorageService/Exception.php';
  285. throw new Zend_Cloud_StorageService_Exception('Bucket name must be specified for S3 adapter.');
  286. }
  287. if (isset($options[self::BUCKET_AS_DOMAIN])) {
  288. // TODO: support bucket domain names
  289. require_once 'Zend/Cloud/StorageService/Exception.php';
  290. throw new Zend_Cloud_StorageService_Exception('The S3 adapter does not currently support buckets in domain names.');
  291. }
  292. return trim($bucket) . '/' . trim($path);
  293. }
  294. /**
  295. * Get the concrete client.
  296. * @return Zend_Service_Amazon_S3
  297. */
  298. public function getClient()
  299. {
  300. return $this->_s3;
  301. }
  302. }