ConcurrentDownloadObjectSample.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. <?php
  2. /**
  3. * Copyright 2019 Huawei Technologies Co.,Ltd.
  4. * Licensed under the Apache License, Version 2.0 (the "License"); you may not use
  5. * this file except in compliance with the License. You may obtain a copy of the
  6. * License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software distributed
  11. * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
  12. * CONDITIONS OF ANY KIND, either express or implied. See the License for the
  13. * specific language governing permissions and limitations under the License.
  14. *
  15. */
  16. /**
  17. * This sample demonstrates how to download an object concurrently
  18. * from OBS using the OBS SDK for PHP.
  19. */
  20. if (file_exists('vendor/autoload.php')) {
  21. require 'vendor/autoload.php';
  22. } else {
  23. require '../vendor/autoload.php'; // sample env
  24. }
  25. if (file_exists('obs-autoloader.php')) {
  26. require 'obs-autoloader.php';
  27. } else {
  28. require '../obs-autoloader.php'; // sample env
  29. }
  30. use Obs\ObsClient;
  31. use Obs\ObsException;
  32. $ak = '*** Provide your Access Key ***';
  33. $sk = '*** Provide your Secret Key ***';
  34. $endpoint = 'https://your-endpoint:443';
  35. $bucketName = 'my-obs-bucket-demo';
  36. $objectKey = 'my-obs-object-key-demo';
  37. $localFilePath = '/temp/' . $objectKey;
  38. /*
  39. * Constructs a obs client instance with your account for accessing OBS
  40. */
  41. $obsClient = ObsClient::factory([
  42. 'key' => $ak,
  43. 'secret' => $sk,
  44. 'endpoint' => $endpoint,
  45. 'socket_timeout' => 30,
  46. 'connect_timeout' => 10
  47. ]);
  48. try {
  49. /*
  50. * Create bucket
  51. */
  52. printf("Create a new bucket for demo\n\n");
  53. $obsClient->createBucket([
  54. 'Bucket' => $bucketName
  55. ]);
  56. $sampleFilePath = '/temp/test.txt'; // sample large file path
  57. // you can prepare a large file in you filesystem first
  58. createSampleFile($sampleFilePath);
  59. /*
  60. * Upload an object to your bucket
  61. */
  62. printf("Uploading a new object to OBS from a file\n\n");
  63. $obsClient->putObject([
  64. 'Bucket' => $bucketName,
  65. 'Key' => $objectKey,
  66. 'SourceFile' => $sampleFilePath
  67. ]);
  68. /*
  69. * Get size of the object and pre-create a random access file to hold object data
  70. */
  71. $resp = $obsClient->getObjectMetadata([
  72. 'Bucket' => $bucketName,
  73. 'Key' => $objectKey
  74. ]);
  75. $objectSize = $resp ['ContentLength'];
  76. printf("Object size from metadata:%d\n\n", $objectSize);
  77. $dir = dirname($localFilePath);
  78. if (! is_dir($dir)) {
  79. mkdir($dir, 0755, true);
  80. }
  81. /*
  82. * Calculate how many blocks to be divided
  83. */
  84. $blockSize = 5 * 1024 * 1024; // 5MB
  85. $blockCount = intval($objectSize / $blockSize);
  86. if ($objectSize % $blockSize !== 0) {
  87. $blockCount ++;
  88. }
  89. printf("Total blocks count:%d\n\n", $blockCount);
  90. /*
  91. * Download the object concurrently
  92. */
  93. printf("Start to download %s\n\n", $objectKey);
  94. $fp = fopen($localFilePath, 'w');
  95. $promise = null;
  96. for($i = 0; $i < $blockCount;) {
  97. $startPos = $i ++ * $blockSize;
  98. $endPos = ($i == $blockCount) ? $objectSize - 1 : ($i * $blockSize - 1);
  99. $range = sprintf('bytes=%d-%d', $startPos, $endPos);
  100. $p = $obsClient->getObjectAsync([
  101. 'Bucket' => $bucketName,
  102. 'Key' => $objectKey,
  103. 'Range' => $range
  104. ], function ($exception, $resp) use ($startPos, $fp, $i, $range) {
  105. fseek($fp, $startPos, 0);
  106. printf("%s\n", $range);
  107. try {
  108. while ( ! $resp ['Body']->eof() ) {
  109. $str = $resp ['Body']->read(65536);
  110. fwrite($fp, $str);
  111. }
  112. } catch ( Exception $exception ) {
  113. printf($exception);
  114. }
  115. $resp ['Body']->close();
  116. printf("Part#" . strval($i) . " done\n\n");
  117. });
  118. if ($promise === null) {
  119. $promise = $p;
  120. }
  121. }
  122. /*
  123. * Waiting for all blocks finished
  124. */
  125. $promise->wait();
  126. fclose($fp);
  127. if (file_exists($sampleFilePath)) {
  128. unlink($sampleFilePath);
  129. }
  130. /*
  131. * Deleting object
  132. */
  133. printf("Deleting object %s \n\n", $objectKey);
  134. $obsClient->deleteObject([
  135. 'Bucket' => $bucketName,
  136. 'Key' => $objectKey
  137. ]);
  138. } catch ( ObsException $e ) {
  139. echo 'Response Code:' . $e->getStatusCode() . PHP_EOL;
  140. echo 'Error Message:' . $e->getExceptionMessage() . PHP_EOL;
  141. echo 'Error Code:' . $e->getExceptionCode() . PHP_EOL;
  142. echo 'Request ID:' . $e->getRequestId() . PHP_EOL;
  143. echo 'Exception Type:' . $e->getExceptionType() . PHP_EOL;
  144. } finally{
  145. $obsClient->close();
  146. }
  147. function createSampleFile($filePath) {
  148. if (file_exists($filePath)) {
  149. return;
  150. }
  151. $filePath = iconv('UTF-8', 'GBK', $filePath);
  152. if (is_string($filePath) && $filePath !== '') {
  153. $fp = null;
  154. $dir = dirname($filePath);
  155. try {
  156. if (! is_dir($dir)) {
  157. mkdir($dir, 0755, true);
  158. }
  159. if (($fp = fopen($filePath, 'w'))) {
  160. for($i = 0; $i < 1000000; $i ++) {
  161. fwrite($fp, uniqid() . "\n");
  162. fwrite($fp, uniqid() . "\n");
  163. if ($i % 100 === 0) {
  164. fflush($fp);
  165. }
  166. }
  167. }
  168. } finally{
  169. if ($fp) {
  170. fclose($fp);
  171. }
  172. }
  173. }
  174. }