MultipartCopy.php 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  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|array */
  11. private $source;
  12. /** @var string */
  13. private $sourceVersionId;
  14. /** @var ResultInterface */
  15. private $sourceMetadata;
  16. /**
  17. * Creates a multipart upload for copying an S3 object.
  18. *
  19. * The valid configuration options are as follows:
  20. *
  21. * - acl: (string) ACL to set on the object being upload. Objects are
  22. * private by default.
  23. * - before_complete: (callable) Callback to invoke before the
  24. * `CompleteMultipartUpload` operation. The callback should have a
  25. * function signature like `function (Aws\Command $command) {...}`.
  26. * - before_initiate: (callable) Callback to invoke before the
  27. * `CreateMultipartUpload` operation. The callback should have a function
  28. * signature like `function (Aws\Command $command) {...}`.
  29. * - before_upload: (callable) Callback to invoke before `UploadPartCopy`
  30. * operations. The callback should have a function signature like
  31. * `function (Aws\Command $command) {...}`.
  32. * - bucket: (string, required) Name of the bucket to which the object is
  33. * being uploaded.
  34. * - concurrency: (int, default=int(5)) Maximum number of concurrent
  35. * `UploadPart` operations allowed during the multipart upload.
  36. * - key: (string, required) Key to use for the object being uploaded.
  37. * - params: (array) An array of key/value parameters that will be applied
  38. * to each of the sub-commands run by the uploader as a base.
  39. * Auto-calculated options will override these parameters. If you need
  40. * more granularity over parameters to each sub-command, use the before_*
  41. * options detailed above to update the commands directly.
  42. * - part_size: (int, default=int(5242880)) Part size, in bytes, to use when
  43. * doing a multipart upload. This must between 5 MB and 5 GB, inclusive.
  44. * - state: (Aws\Multipart\UploadState) An object that represents the state
  45. * of the multipart upload and that is used to resume a previous upload.
  46. * When this option is provided, the `bucket`, `key`, and `part_size`
  47. * options are ignored.
  48. * - source_metadata: (Aws\ResultInterface) An object that represents the
  49. * result of executing a HeadObject command on the copy source.
  50. *
  51. * @param S3ClientInterface $client Client used for the upload.
  52. * @param string|array $source Location of the data to be copied (in the
  53. * form /<bucket>/<key>). If the key contains a '?'
  54. * character, instead pass an array of source_key,
  55. * source_bucket, and source_version_id.
  56. * @param array $config Configuration used to perform the upload.
  57. */
  58. public function __construct(
  59. S3ClientInterface $client,
  60. $source,
  61. array $config = []
  62. ) {
  63. if (is_array($source)) {
  64. $this->source = $source;
  65. } else {
  66. $this->source = $this->getInputSource($source);
  67. }
  68. parent::__construct(
  69. $client,
  70. array_change_key_case($config) + ['source_metadata' => null]
  71. );
  72. }
  73. /**
  74. * An alias of the self::upload method.
  75. *
  76. * @see self::upload
  77. */
  78. public function copy()
  79. {
  80. return $this->upload();
  81. }
  82. protected function loadUploadWorkflowInfo()
  83. {
  84. return [
  85. 'command' => [
  86. 'initiate' => 'CreateMultipartUpload',
  87. 'upload' => 'UploadPartCopy',
  88. 'complete' => 'CompleteMultipartUpload',
  89. ],
  90. 'id' => [
  91. 'bucket' => 'Bucket',
  92. 'key' => 'Key',
  93. 'upload_id' => 'UploadId',
  94. ],
  95. 'part_num' => 'PartNumber',
  96. ];
  97. }
  98. protected function getUploadCommands(callable $resultHandler)
  99. {
  100. $parts = ceil($this->getSourceSize() / $this->determinePartSize());
  101. for ($partNumber = 1; $partNumber <= $parts; $partNumber++) {
  102. // If we haven't already uploaded this part, yield a new part.
  103. if (!$this->state->hasPartBeenUploaded($partNumber)) {
  104. $command = $this->client->getCommand(
  105. $this->info['command']['upload'],
  106. $this->createPart($partNumber, $parts) + $this->getState()->getId()
  107. );
  108. $command->getHandlerList()->appendSign($resultHandler, 'mup');
  109. yield $command;
  110. }
  111. }
  112. }
  113. private function createPart($partNumber, $partsCount)
  114. {
  115. $data = [];
  116. // Apply custom params to UploadPartCopy data
  117. $config = $this->getConfig();
  118. $params = isset($config['params']) ? $config['params'] : [];
  119. foreach ($params as $k => $v) {
  120. $data[$k] = $v;
  121. }
  122. // The source parameter here is usually a string, but can be overloaded as an array
  123. // if the key contains a '?' character to specify where the query parameters start
  124. if (is_array($this->source)) {
  125. $key = str_replace('%2F', '/', rawurlencode($this->source['source_key']));
  126. $bucket = $this->source['source_bucket'];
  127. } else {
  128. list($bucket, $key) = explode('/', ltrim($this->source, '/'), 2);
  129. $key = implode(
  130. '/',
  131. array_map(
  132. 'urlencode',
  133. explode('/', rawurldecode($key))
  134. )
  135. );
  136. }
  137. $uri = ArnParser::isArn($bucket) ? '' : '/';
  138. $uri .= $bucket . '/' . $key;
  139. $data['CopySource'] = $uri;
  140. $data['PartNumber'] = $partNumber;
  141. if (!empty($this->sourceVersionId)) {
  142. $data['CopySource'] .= "?versionId=" . $this->sourceVersionId;
  143. }
  144. $defaultPartSize = $this->determinePartSize();
  145. $startByte = $defaultPartSize * ($partNumber - 1);
  146. $data['ContentLength'] = $partNumber < $partsCount
  147. ? $defaultPartSize
  148. : $this->getSourceSize() - ($defaultPartSize * ($partsCount - 1));
  149. $endByte = $startByte + $data['ContentLength'] - 1;
  150. $data['CopySourceRange'] = "bytes=$startByte-$endByte";
  151. return $data;
  152. }
  153. protected function extractETag(ResultInterface $result)
  154. {
  155. return $result->search('CopyPartResult.ETag');
  156. }
  157. protected function getSourceMimeType()
  158. {
  159. return $this->getSourceMetadata()['ContentType'];
  160. }
  161. protected function getSourceSize()
  162. {
  163. return $this->getSourceMetadata()['ContentLength'];
  164. }
  165. private function getSourceMetadata()
  166. {
  167. if (empty($this->sourceMetadata)) {
  168. $this->sourceMetadata = $this->fetchSourceMetadata();
  169. }
  170. return $this->sourceMetadata;
  171. }
  172. private function fetchSourceMetadata()
  173. {
  174. if ($this->config['source_metadata'] instanceof ResultInterface) {
  175. return $this->config['source_metadata'];
  176. }
  177. //if the source variable was overloaded with an array, use the inputs for key and bucket
  178. if (is_array($this->source)) {
  179. $headParams = [
  180. 'Key' => $this->source['source_key'],
  181. 'Bucket' => $this->source['source_bucket']
  182. ];
  183. if (isset($this->source['source_version_id'])) {
  184. $this->sourceVersionId = $this->source['source_version_id'];
  185. $headParams['VersionId'] = $this->sourceVersionId;
  186. }
  187. //otherwise, use the default source parsing behavior
  188. } else {
  189. list($bucket, $key) = explode('/', ltrim($this->source, '/'), 2);
  190. $headParams = [
  191. 'Bucket' => $bucket,
  192. 'Key' => $key,
  193. ];
  194. if (strpos($key, '?')) {
  195. list($key, $query) = explode('?', $key, 2);
  196. $headParams['Key'] = $key;
  197. $query = Psr7\Query::parse($query, false);
  198. if (isset($query['versionId'])) {
  199. $this->sourceVersionId = $query['versionId'];
  200. $headParams['VersionId'] = $this->sourceVersionId;
  201. }
  202. }
  203. }
  204. return $this->client->headObject($headParams);
  205. }
  206. /**
  207. * Get the url decoded input source, starting with a slash if it is not an
  208. * ARN to standardize the source location syntax.
  209. *
  210. * @param string $inputSource The source that was passed to the constructor
  211. * @return string The source, starting with a slash if it's not an arn
  212. */
  213. private function getInputSource($inputSource)
  214. {
  215. $sourceBuilder = ArnParser::isArn($inputSource) ? '' : '/';
  216. $sourceBuilder .= ltrim(rawurldecode($inputSource), '/');
  217. return $sourceBuilder;
  218. }
  219. }