MultipartCopy.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. <?php
  2. namespace Aws\S3;
  3. use Aws\Arn\ArnParser;
  4. use Aws\Multipart\AbstractUploadManager;
  5. use Aws\ResultInterface;
  6. use GuzzleHttp\Psr7;
  7. class MultipartCopy extends AbstractUploadManager
  8. {
  9. use MultipartUploadingTrait;
  10. /** @var string */
  11. private $source;
  12. /** @var ResultInterface */
  13. private $sourceMetadata;
  14. /**
  15. * Creates a multipart upload for copying an S3 object.
  16. *
  17. * The valid configuration options are as follows:
  18. *
  19. * - acl: (string) ACL to set on the object being upload. Objects are
  20. * private by default.
  21. * - before_complete: (callable) Callback to invoke before the
  22. * `CompleteMultipartUpload` operation. The callback should have a
  23. * function signature like `function (Aws\Command $command) {...}`.
  24. * - before_initiate: (callable) Callback to invoke before the
  25. * `CreateMultipartUpload` operation. The callback should have a function
  26. * signature like `function (Aws\Command $command) {...}`.
  27. * - before_upload: (callable) Callback to invoke before `UploadPartCopy`
  28. * operations. The callback should have a function signature like
  29. * `function (Aws\Command $command) {...}`.
  30. * - bucket: (string, required) Name of the bucket to which the object is
  31. * being uploaded.
  32. * - concurrency: (int, default=int(5)) Maximum number of concurrent
  33. * `UploadPart` operations allowed during the multipart upload.
  34. * - key: (string, required) Key to use for the object being uploaded.
  35. * - params: (array) An array of key/value parameters that will be applied
  36. * to each of the sub-commands run by the uploader as a base.
  37. * Auto-calculated options will override these parameters. If you need
  38. * more granularity over parameters to each sub-command, use the before_*
  39. * options detailed above to update the commands directly.
  40. * - part_size: (int, default=int(5242880)) Part size, in bytes, to use when
  41. * doing a multipart upload. This must between 5 MB and 5 GB, inclusive.
  42. * - state: (Aws\Multipart\UploadState) An object that represents the state
  43. * of the multipart upload and that is used to resume a previous upload.
  44. * When this option is provided, the `bucket`, `key`, and `part_size`
  45. * options are ignored.
  46. * - source_metadata: (Aws\ResultInterface) An object that represents the
  47. * result of executing a HeadObject command on the copy source.
  48. *
  49. * @param S3ClientInterface $client Client used for the upload.
  50. * @param string $source Location of the data to be copied
  51. * (in the form /<bucket>/<key>).
  52. * @param array $config Configuration used to perform the upload.
  53. */
  54. public function __construct(
  55. S3ClientInterface $client,
  56. $source,
  57. array $config = []
  58. ) {
  59. if (ArnParser::isArn($source)) {
  60. $this->source = '';
  61. } else {
  62. $this->source = "/";
  63. }
  64. $this->source .= ltrim($source, '/');
  65. parent::__construct(
  66. $client,
  67. array_change_key_case($config) + ['source_metadata' => null]
  68. );
  69. }
  70. /**
  71. * An alias of the self::upload method.
  72. *
  73. * @see self::upload
  74. */
  75. public function copy()
  76. {
  77. return $this->upload();
  78. }
  79. protected function loadUploadWorkflowInfo()
  80. {
  81. return [
  82. 'command' => [
  83. 'initiate' => 'CreateMultipartUpload',
  84. 'upload' => 'UploadPartCopy',
  85. 'complete' => 'CompleteMultipartUpload',
  86. ],
  87. 'id' => [
  88. 'bucket' => 'Bucket',
  89. 'key' => 'Key',
  90. 'upload_id' => 'UploadId',
  91. ],
  92. 'part_num' => 'PartNumber',
  93. ];
  94. }
  95. protected function getUploadCommands(callable $resultHandler)
  96. {
  97. $parts = ceil($this->getSourceSize() / $this->determinePartSize());
  98. for ($partNumber = 1; $partNumber <= $parts; $partNumber++) {
  99. // If we haven't already uploaded this part, yield a new part.
  100. if (!$this->state->hasPartBeenUploaded($partNumber)) {
  101. $command = $this->client->getCommand(
  102. $this->info['command']['upload'],
  103. $this->createPart($partNumber, $parts) + $this->getState()->getId()
  104. );
  105. $command->getHandlerList()->appendSign($resultHandler, 'mup');
  106. yield $command;
  107. }
  108. }
  109. }
  110. private function createPart($partNumber, $partsCount)
  111. {
  112. $data = [];
  113. // Apply custom params to UploadPartCopy data
  114. $config = $this->getConfig();
  115. $params = isset($config['params']) ? $config['params'] : [];
  116. foreach ($params as $k => $v) {
  117. $data[$k] = $v;
  118. }
  119. list($bucket, $key) = explode('/', ltrim($this->source, '/'), 2);
  120. $data['CopySource'] = '/' . $bucket . '/' . implode(
  121. '/',
  122. array_map(
  123. 'urlencode',
  124. explode('/', rawurldecode($key))
  125. )
  126. );
  127. $data['PartNumber'] = $partNumber;
  128. $defaultPartSize = $this->determinePartSize();
  129. $startByte = $defaultPartSize * ($partNumber - 1);
  130. $data['ContentLength'] = $partNumber < $partsCount
  131. ? $defaultPartSize
  132. : $this->getSourceSize() - ($defaultPartSize * ($partsCount - 1));
  133. $endByte = $startByte + $data['ContentLength'] - 1;
  134. $data['CopySourceRange'] = "bytes=$startByte-$endByte";
  135. return $data;
  136. }
  137. protected function extractETag(ResultInterface $result)
  138. {
  139. return $result->search('CopyPartResult.ETag');
  140. }
  141. protected function getSourceMimeType()
  142. {
  143. return $this->getSourceMetadata()['ContentType'];
  144. }
  145. protected function getSourceSize()
  146. {
  147. return $this->getSourceMetadata()['ContentLength'];
  148. }
  149. private function getSourceMetadata()
  150. {
  151. if (empty($this->sourceMetadata)) {
  152. $this->sourceMetadata = $this->fetchSourceMetadata();
  153. }
  154. return $this->sourceMetadata;
  155. }
  156. private function fetchSourceMetadata()
  157. {
  158. if ($this->config['source_metadata'] instanceof ResultInterface) {
  159. return $this->config['source_metadata'];
  160. }
  161. list($bucket, $key) = explode('/', ltrim($this->source, '/'), 2);
  162. $headParams = [
  163. 'Bucket' => $bucket,
  164. 'Key' => $key,
  165. ];
  166. if (strpos($key, '?')) {
  167. list($key, $query) = explode('?', $key, 2);
  168. $headParams['Key'] = $key;
  169. $query = Psr7\parse_query($query, false);
  170. if (isset($query['versionId'])) {
  171. $headParams['VersionId'] = $query['versionId'];
  172. }
  173. }
  174. return $this->client->headObject($headParams);
  175. }
  176. }