S3ClientTrait.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  1. <?php
  2. namespace Aws\S3;
  3. use Aws\Api\Parser\PayloadParserTrait;
  4. use Aws\CommandInterface;
  5. use Aws\Exception\AwsException;
  6. use Aws\HandlerList;
  7. use Aws\ResultInterface;
  8. use Aws\S3\Exception\PermanentRedirectException;
  9. use Aws\S3\Exception\S3Exception;
  10. use GuzzleHttp\Promise\PromiseInterface;
  11. use GuzzleHttp\Promise\RejectedPromise;
  12. use Psr\Http\Message\ResponseInterface;
  13. /**
  14. * A trait providing S3-specific functionality. This is meant to be used in
  15. * classes implementing \Aws\S3\S3ClientInterface
  16. */
  17. trait S3ClientTrait
  18. {
  19. use PayloadParserTrait;
  20. /**
  21. * @see S3ClientInterface::upload()
  22. */
  23. public function upload(
  24. $bucket,
  25. $key,
  26. $body,
  27. $acl = 'private',
  28. array $options = []
  29. ) {
  30. return $this
  31. ->uploadAsync($bucket, $key, $body, $acl, $options)
  32. ->wait();
  33. }
  34. /**
  35. * @see S3ClientInterface::uploadAsync()
  36. */
  37. public function uploadAsync(
  38. $bucket,
  39. $key,
  40. $body,
  41. $acl = 'private',
  42. array $options = []
  43. ) {
  44. return (new ObjectUploader($this, $bucket, $key, $body, $acl, $options))
  45. ->promise();
  46. }
  47. /**
  48. * @see S3ClientInterface::copy()
  49. */
  50. public function copy(
  51. $fromB,
  52. $fromK,
  53. $destB,
  54. $destK,
  55. $acl = 'private',
  56. array $opts = []
  57. ) {
  58. return $this->copyAsync($fromB, $fromK, $destB, $destK, $acl, $opts)
  59. ->wait();
  60. }
  61. /**
  62. * @see S3ClientInterface::copyAsync()
  63. */
  64. public function copyAsync(
  65. $fromB,
  66. $fromK,
  67. $destB,
  68. $destK,
  69. $acl = 'private',
  70. array $opts = []
  71. ) {
  72. $source = [
  73. 'Bucket' => $fromB,
  74. 'Key' => $fromK,
  75. ];
  76. if (isset($opts['version_id'])) {
  77. $source['VersionId'] = $opts['version_id'];
  78. }
  79. $destination = [
  80. 'Bucket' => $destB,
  81. 'Key' => $destK
  82. ];
  83. return (new ObjectCopier($this, $source, $destination, $acl, $opts))
  84. ->promise();
  85. }
  86. /**
  87. * @see S3ClientInterface::registerStreamWrapper()
  88. */
  89. public function registerStreamWrapper()
  90. {
  91. StreamWrapper::register($this);
  92. }
  93. /**
  94. * @see S3ClientInterface::registerStreamWrapperV2()
  95. */
  96. public function registerStreamWrapperV2()
  97. {
  98. StreamWrapper::register(
  99. $this,
  100. 's3',
  101. null,
  102. true
  103. );
  104. }
  105. /**
  106. * @see S3ClientInterface::deleteMatchingObjects()
  107. */
  108. public function deleteMatchingObjects(
  109. $bucket,
  110. $prefix = '',
  111. $regex = '',
  112. array $options = []
  113. ) {
  114. $this->deleteMatchingObjectsAsync($bucket, $prefix, $regex, $options)
  115. ->wait();
  116. }
  117. /**
  118. * @see S3ClientInterface::deleteMatchingObjectsAsync()
  119. */
  120. public function deleteMatchingObjectsAsync(
  121. $bucket,
  122. $prefix = '',
  123. $regex = '',
  124. array $options = []
  125. ) {
  126. if (!$prefix && !$regex) {
  127. return new RejectedPromise(
  128. new \RuntimeException('A prefix or regex is required.')
  129. );
  130. }
  131. $params = ['Bucket' => $bucket, 'Prefix' => $prefix];
  132. $iter = $this->getIterator('ListObjects', $params);
  133. if ($regex) {
  134. $iter = \Aws\filter($iter, function ($c) use ($regex) {
  135. return preg_match($regex, $c['Key']);
  136. });
  137. }
  138. return BatchDelete::fromIterator($this, $bucket, $iter, $options)
  139. ->promise();
  140. }
  141. /**
  142. * @see S3ClientInterface::uploadDirectory()
  143. */
  144. public function uploadDirectory(
  145. $directory,
  146. $bucket,
  147. $keyPrefix = null,
  148. array $options = []
  149. ) {
  150. $this->uploadDirectoryAsync($directory, $bucket, $keyPrefix, $options)
  151. ->wait();
  152. }
  153. /**
  154. * @see S3ClientInterface::uploadDirectoryAsync()
  155. */
  156. public function uploadDirectoryAsync(
  157. $directory,
  158. $bucket,
  159. $keyPrefix = null,
  160. array $options = []
  161. ) {
  162. $d = "s3://$bucket" . ($keyPrefix ? '/' . ltrim($keyPrefix, '/') : '');
  163. return (new Transfer($this, $directory, $d, $options))->promise();
  164. }
  165. /**
  166. * @see S3ClientInterface::downloadBucket()
  167. */
  168. public function downloadBucket(
  169. $directory,
  170. $bucket,
  171. $keyPrefix = '',
  172. array $options = []
  173. ) {
  174. $this->downloadBucketAsync($directory, $bucket, $keyPrefix, $options)
  175. ->wait();
  176. }
  177. /**
  178. * @see S3ClientInterface::downloadBucketAsync()
  179. */
  180. public function downloadBucketAsync(
  181. $directory,
  182. $bucket,
  183. $keyPrefix = '',
  184. array $options = []
  185. ) {
  186. $s = "s3://$bucket" . ($keyPrefix ? '/' . ltrim($keyPrefix, '/') : '');
  187. return (new Transfer($this, $s, $directory, $options))->promise();
  188. }
  189. /**
  190. * @see S3ClientInterface::determineBucketRegion()
  191. */
  192. public function determineBucketRegion($bucketName)
  193. {
  194. return $this->determineBucketRegionAsync($bucketName)->wait();
  195. }
  196. /**
  197. * @see S3ClientInterface::determineBucketRegionAsync()
  198. *
  199. * @param string $bucketName
  200. *
  201. * @return PromiseInterface
  202. */
  203. public function determineBucketRegionAsync($bucketName)
  204. {
  205. $command = $this->getCommand('HeadBucket', ['Bucket' => $bucketName]);
  206. $handlerList = clone $this->getHandlerList();
  207. $handlerList->remove('s3.permanent_redirect');
  208. $handlerList->remove('signer');
  209. $handler = $handlerList->resolve();
  210. return $handler($command)
  211. ->then(static function (ResultInterface $result) {
  212. return $result['@metadata']['headers']['x-amz-bucket-region'];
  213. }, function (AwsException $e) {
  214. $response = $e->getResponse();
  215. if ($response === null) {
  216. throw $e;
  217. }
  218. if ($e->getAwsErrorCode() === 'AuthorizationHeaderMalformed') {
  219. $region = $this->determineBucketRegionFromExceptionBody(
  220. $response
  221. );
  222. if (!empty($region)) {
  223. return $region;
  224. }
  225. throw $e;
  226. }
  227. return $response->getHeaderLine('x-amz-bucket-region');
  228. });
  229. }
  230. private function determineBucketRegionFromExceptionBody(ResponseInterface $response)
  231. {
  232. try {
  233. $element = $this->parseXml($response->getBody(), $response);
  234. if (!empty($element->Region)) {
  235. return (string)$element->Region;
  236. }
  237. } catch (\Exception $e) {
  238. // Fallthrough on exceptions from parsing
  239. }
  240. return false;
  241. }
  242. /**
  243. * @see S3ClientInterface::doesBucketExist()
  244. */
  245. public function doesBucketExist($bucket)
  246. {
  247. return $this->checkExistenceWithCommand(
  248. $this->getCommand('HeadBucket', ['Bucket' => $bucket])
  249. );
  250. }
  251. /**
  252. * @see S3ClientInterface::doesBucketExistV2()
  253. */
  254. public function doesBucketExistV2($bucket, $accept403 = false)
  255. {
  256. $command = $this->getCommand('HeadBucket', ['Bucket' => $bucket]);
  257. try {
  258. $this->execute($command);
  259. return true;
  260. } catch (S3Exception $e) {
  261. if (
  262. ($accept403 && $e->getStatusCode() === 403)
  263. || $e instanceof PermanentRedirectException
  264. ) {
  265. return true;
  266. }
  267. if ($e->getStatusCode() === 404) {
  268. return false;
  269. }
  270. throw $e;
  271. }
  272. }
  273. /**
  274. * @see S3ClientInterface::doesObjectExist()
  275. */
  276. public function doesObjectExist($bucket, $key, array $options = [])
  277. {
  278. return $this->checkExistenceWithCommand(
  279. $this->getCommand('HeadObject', [
  280. 'Bucket' => $bucket,
  281. 'Key' => $key
  282. ] + $options)
  283. );
  284. }
  285. /**
  286. * @see S3ClientInterface::doesObjectExistV2()
  287. */
  288. public function doesObjectExistV2(
  289. $bucket,
  290. $key,
  291. $includeDeleteMarkers = false,
  292. array $options = []
  293. ){
  294. $command = $this->getCommand('HeadObject', [
  295. 'Bucket' => $bucket,
  296. 'Key' => $key
  297. ] + $options
  298. );
  299. try {
  300. $this->execute($command);
  301. return true;
  302. } catch (S3Exception $e) {
  303. if ($includeDeleteMarkers
  304. && $this->useDeleteMarkers($e)
  305. ) {
  306. return true;
  307. }
  308. if ($e->getStatusCode() === 404) {
  309. return false;
  310. }
  311. throw $e;
  312. }
  313. }
  314. private function useDeleteMarkers($exception)
  315. {
  316. $response = $exception->getResponse();
  317. return !empty($response)
  318. && $response->getHeader('x-amz-delete-marker');
  319. }
  320. /**
  321. * Determines whether or not a resource exists using a command
  322. *
  323. * @param CommandInterface $command Command used to poll for the resource
  324. *
  325. * @return bool
  326. * @throws S3Exception|\Exception if there is an unhandled exception
  327. */
  328. private function checkExistenceWithCommand(CommandInterface $command)
  329. {
  330. try {
  331. $this->execute($command);
  332. return true;
  333. } catch (S3Exception $e) {
  334. if ($e->getAwsErrorCode() == 'AccessDenied') {
  335. return true;
  336. }
  337. if ($e->getStatusCode() >= 500) {
  338. throw $e;
  339. }
  340. return false;
  341. }
  342. }
  343. /**
  344. * @see S3ClientInterface::execute()
  345. */
  346. abstract public function execute(CommandInterface $command);
  347. /**
  348. * @see S3ClientInterface::getCommand()
  349. */
  350. abstract public function getCommand($name, array $args = []);
  351. /**
  352. * @see S3ClientInterface::getHandlerList()
  353. *
  354. * @return HandlerList
  355. */
  356. abstract public function getHandlerList();
  357. /**
  358. * @see S3ClientInterface::getIterator()
  359. *
  360. * @return \Iterator
  361. */
  362. abstract public function getIterator($name, array $args = []);
  363. }