PageRenderTime 42ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/ZendFramework/library/Zend/Cloud/StorageService/Adapter/S3.php

https://bitbucket.org/Dal-Papa/is-340-publish-base
PHP | 332 lines | 157 code | 26 blank | 149 comment | 14 complexity | 787ec9d22f26e73178c4641059d2cb28 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-2012 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-2012 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. $fullSourcePath = $this->_getFullPath($sourcePath, $options);
  166. $fullDestPath = $this->_getFullPath($destinationPath, $options);
  167. return $this->_s3->copyObject(
  168. $fullSourcePath,
  169. $fullDestPath,
  170. empty($options[self::METADATA]) ? null : $options[self::METADATA]
  171. );
  172. } catch (Zend_Service_Amazon_S3_Exception $e) {
  173. throw new Zend_Cloud_StorageService_Exception('Error on copy: '.$e->getMessage(), $e->getCode(), $e);
  174. }
  175. }
  176. /**
  177. * Move an item in the storage service to a given path.
  178. *
  179. * @TODO Support streams for those services that don't support natively
  180. *
  181. * @param string $sourcePath
  182. * @param string $destination path
  183. * @param array $options
  184. * @return void
  185. */
  186. public function moveItem($sourcePath, $destinationPath, $options = array())
  187. {
  188. try {
  189. $fullSourcePath = $this->_getFullPath($sourcePath, $options);
  190. $fullDestPath = $this->_getFullPath($destinationPath, $options);
  191. return $this->_s3->moveObject(
  192. $fullSourcePath,
  193. $fullDestPath,
  194. empty($options[self::METADATA]) ? null : $options[self::METADATA]
  195. );
  196. } catch (Zend_Service_Amazon_S3_Exception $e) {
  197. throw new Zend_Cloud_StorageService_Exception('Error on move: '.$e->getMessage(), $e->getCode(), $e);
  198. }
  199. }
  200. /**
  201. * Rename an item in the storage service to a given name.
  202. *
  203. *
  204. * @param string $path
  205. * @param string $name
  206. * @param array $options
  207. * @return void
  208. */
  209. public function renameItem($path, $name, $options = null)
  210. {
  211. require_once 'Zend/Cloud/OperationNotAvailableException.php';
  212. throw new Zend_Cloud_OperationNotAvailableException('Rename not implemented');
  213. }
  214. /**
  215. * List items in the given directory in the storage service
  216. *
  217. * The $path must be a directory
  218. *
  219. *
  220. * @param string $path Must be a directory
  221. * @param array $options
  222. * @return array A list of item names
  223. */
  224. public function listItems($path, $options = null)
  225. {
  226. try {
  227. // TODO Support 'prefix' parameter for Zend_Service_Amazon_S3::getObjectsByBucket()
  228. return $this->_s3->getObjectsByBucket($this->_defaultBucketName);
  229. } catch (Zend_Service_Amazon_S3_Exception $e) {
  230. throw new Zend_Cloud_StorageService_Exception('Error on list: '.$e->getMessage(), $e->getCode(), $e);
  231. }
  232. }
  233. /**
  234. * Get a key/value array of metadata for the given path.
  235. *
  236. * @param string $path
  237. * @param array $options
  238. * @return array
  239. */
  240. public function fetchMetadata($path, $options = array())
  241. {
  242. try {
  243. return $this->_s3->getInfo($this->_getFullPath($path, $options));
  244. } catch (Zend_Service_Amazon_S3_Exception $e) {
  245. throw new Zend_Cloud_StorageService_Exception('Error on fetch: '.$e->getMessage(), $e->getCode(), $e);
  246. }
  247. }
  248. /**
  249. * Store a key/value array of metadata at the given path.
  250. * WARNING: This operation overwrites any metadata that is located at
  251. * $destinationPath.
  252. *
  253. * @param string $destinationPath
  254. * @param array $options
  255. * @return void
  256. */
  257. public function storeMetadata($destinationPath, $metadata, $options = array())
  258. {
  259. require_once 'Zend/Cloud/OperationNotAvailableException.php';
  260. throw new Zend_Cloud_OperationNotAvailableException('Storing separate metadata is not supported, use storeItem() with \'metadata\' option key');
  261. }
  262. /**
  263. * Delete a key/value array of metadata at the given path.
  264. *
  265. * @param string $path
  266. * @param array $options
  267. * @return void
  268. */
  269. public function deleteMetadata($path)
  270. {
  271. require_once 'Zend/Cloud/OperationNotAvailableException.php';
  272. throw new Zend_Cloud_OperationNotAvailableException('Deleting metadata not supported');
  273. }
  274. /**
  275. * Get full path, including bucket, for an object
  276. *
  277. * @param string $path
  278. * @param array $options
  279. * @return void
  280. */
  281. protected function _getFullPath($path, $options)
  282. {
  283. if (isset($options[self::BUCKET_NAME])) {
  284. $bucket = $options[self::BUCKET_NAME];
  285. } else if (isset($this->_defaultBucketName)) {
  286. $bucket = $this->_defaultBucketName;
  287. } else {
  288. require_once 'Zend/Cloud/StorageService/Exception.php';
  289. throw new Zend_Cloud_StorageService_Exception('Bucket name must be specified for S3 adapter.');
  290. }
  291. if (isset($options[self::BUCKET_AS_DOMAIN])) {
  292. // TODO: support bucket domain names
  293. require_once 'Zend/Cloud/StorageService/Exception.php';
  294. throw new Zend_Cloud_StorageService_Exception('The S3 adapter does not currently support buckets in domain names.');
  295. }
  296. return trim($bucket) . '/' . trim($path);
  297. }
  298. /**
  299. * Get the concrete client.
  300. * @return Zend_Service_Amazon_S3
  301. */
  302. public function getClient()
  303. {
  304. return $this->_s3;
  305. }
  306. }