Copy.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. <?php
  2. namespace Qcloud\Cos;
  3. use GuzzleHttp\Pool;
  4. class Copy {
  5. const MIN_PART_SIZE = 1048576;
  6. const MAX_PART_SIZE = 5368709120;
  7. const DEFAULT_PART_SIZE = 52428800;
  8. const MAX_PARTS = 10000;
  9. private $client;
  10. private $copySource;
  11. private $options;
  12. private $partSize;
  13. private $parts;
  14. private $size;
  15. private $commandList = [];
  16. private $requestList = [];
  17. public function __construct($client, $source, $options = array()) {
  18. $minPartSize = $options['PartSize'];
  19. unset($options['PartSize']);
  20. $this->client = $client;
  21. $this->copySource = $source;
  22. $this->options = $options;
  23. $this->size = $source['ContentLength'];
  24. unset($source['ContentLength']);
  25. $this->partSize = $this->calculatePartSize($minPartSize);
  26. $this->concurrency = isset($options['Concurrency']) ? $options['Concurrency'] : 10;
  27. $this->retry = isset($options['Retry']) ? $options['Retry'] : 5;
  28. }
  29. public function copy() {
  30. $uploadId= $this->initiateMultipartUpload();
  31. for ($i = 0; $i < $this->retry; $i += 1) {
  32. $rt = $this->uploadParts($uploadId);
  33. if ($rt == 0) {
  34. break;
  35. }
  36. sleep(1 << $i);
  37. }
  38. foreach ( $this->parts as $key => $row ){
  39. $num1[$key] = $row ['PartNumber'];
  40. $num2[$key] = $row ['ETag'];
  41. }
  42. array_multisort($num1, SORT_ASC, $num2, SORT_ASC, $this->parts);
  43. return $this->client->completeMultipartUpload(array(
  44. 'Bucket' => $this->options['Bucket'],
  45. 'Key' => $this->options['Key'],
  46. 'UploadId' => $uploadId,
  47. 'Parts' => $this->parts)
  48. );
  49. }
  50. public function uploadParts($uploadId) {
  51. $copyRequests = function ($uploadId) {
  52. $offset = 0;
  53. $partNumber = 1;
  54. $partSize = $this->partSize;
  55. $finishedNum = 0;
  56. $this->parts = array();
  57. for ($index = 1; ; $index ++) {
  58. if ($offset + $partSize >= $this->size)
  59. {
  60. $partSize = $this->size - $offset;
  61. }
  62. $copySourcePath = $this->copySource['Bucket']. '.cos.'. $this->copySource['Region'].
  63. ".myqcloud.com/". $this->copySource['Key']. "?versionId=". $this->copySource['VersionId'];
  64. $params = array(
  65. 'Bucket' => $this->options['Bucket'],
  66. 'Key' => $this->options['Key'],
  67. 'UploadId' => $uploadId,
  68. 'PartNumber' => $partNumber,
  69. 'CopySource'=> $copySourcePath,
  70. 'CopySourceRange' => 'bytes='.((string)$offset).'-'.(string)($offset+$partSize - 1),
  71. );
  72. if(!isset($this->parts[$partNumber])) {
  73. $command = $this->client->getCommand('uploadPartCopy', $params);
  74. $request = $this->client->commandToRequestTransformer($command);
  75. $this->commandList[$index] = $command;
  76. $this->requestList[$index] = $request;
  77. yield $request;
  78. }
  79. ++$partNumber;
  80. $offset += $partSize;
  81. if ($this->size == $offset) {
  82. break;
  83. }
  84. }
  85. };
  86. $pool = new Pool($this->client->httpClient, $copyRequests($uploadId), [
  87. 'concurrency' => $this->concurrency,
  88. 'fulfilled' => function ($response, $index) {
  89. $index = $index + 1;
  90. $response = $this->client->responseToResultTransformer($response, $this->requestList[$index], $this->commandList[$index]);
  91. $part = array('PartNumber' => $index, 'ETag' => $response['ETag']);
  92. $this->parts[$index] = $part;
  93. },
  94. 'rejected' => function ($reason, $index) {
  95. $index = $index += 1;
  96. $retry = 2;
  97. for ($i = 1; $i <= $retry; $i++) {
  98. try {
  99. $rt =$this->client->execute($this->commandList[$index]);
  100. $part = array('PartNumber' => $index, 'ETag' => $rt['ETag']);
  101. $this->parts[$index] = $part;
  102. } catch(\Exception $e) {
  103. if ($i == $retry) {
  104. throw($e);
  105. }
  106. }
  107. }
  108. },
  109. ]);
  110. // Initiate the transfers and create a promise
  111. $promise = $pool->promise();
  112. // Force the pool of requests to complete.
  113. $promise->wait();
  114. }
  115. private function calculatePartSize($minPartSize)
  116. {
  117. $partSize = intval(ceil(($this->size / self::MAX_PARTS)));
  118. $partSize = max($minPartSize, $partSize);
  119. $partSize = min($partSize, self::MAX_PART_SIZE);
  120. $partSize = max($partSize, self::MIN_PART_SIZE);
  121. return $partSize;
  122. }
  123. private function initiateMultipartUpload() {
  124. $result = $this->client->createMultipartUpload($this->options);
  125. return $result['UploadId'];
  126. }
  127. }