Copy.php 5.2 KB

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