MultipartUpload.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. <?php
  2. require_once __DIR__ . '/Common.php';
  3. use OSS\OssClient;
  4. use OSS\Core\OssUtil;
  5. use OSS\Core\OssException;
  6. $bucket = Common::getBucketName();
  7. $ossClient = Common::getOssClient();
  8. if (is_null($ossClient)) exit(1);
  9. //******************************* Simple usage ***************************************************************
  10. /**
  11. * See the putObjectByRawAPis usage in complete example to check out basic multipart upload APIs which can be used as resumable upload.
  12. */
  13. // Upload a file using the multipart upload interface, which determines to use simple upload or multipart upload based on the file size.
  14. $ossClient->multiuploadFile($bucket, "file.php", __FILE__, array());
  15. Common::println("local file " . __FILE__ . " is uploaded to the bucket $bucket, file.php");
  16. // Upload local directory's data into target dir
  17. $ossClient->uploadDir($bucket, "targetdir", __DIR__);
  18. Common::println("local dir " . __DIR__ . " is uploaded to the bucket $bucket, targetdir/");
  19. // List the incomplete multipart uploads
  20. $listMultipartUploadInfo = $ossClient->listMultipartUploads($bucket, array());
  21. //******************************* For complete usage, see the following functions ****************************************************
  22. multiuploadFile($ossClient, $bucket);
  23. putObjectByRawApis($ossClient, $bucket);
  24. uploadDir($ossClient, $bucket);
  25. listMultipartUploads($ossClient, $bucket);
  26. /**
  27. * Upload files using multipart upload
  28. *
  29. * @param OssClient $ossClient OssClient instance
  30. * @param string $bucket bucket name
  31. * @return null
  32. */
  33. function multiuploadFile($ossClient, $bucket)
  34. {
  35. $object = "test/multipart-test.txt";
  36. $file = __FILE__;
  37. $options = array();
  38. try {
  39. $ossClient->multiuploadFile($bucket, $object, $file, $options);
  40. } catch (OssException $e) {
  41. printf(__FUNCTION__ . ": FAILED\n");
  42. printf($e->getMessage() . "\n");
  43. return;
  44. }
  45. print(__FUNCTION__ . ": OK" . "\n");
  46. }
  47. /**
  48. * Use basic multipart upload for file upload.
  49. *
  50. * @param OssClient $ossClient OssClient instance
  51. * @param string $bucket bucket name
  52. * @throws OssException
  53. */
  54. function putObjectByRawApis($ossClient, $bucket)
  55. {
  56. $object = "test/multipart-test.txt";
  57. /**
  58. * step 1. Initialize a block upload event, that is, a multipart upload process to get an upload id
  59. */
  60. try {
  61. $uploadId = $ossClient->initiateMultipartUpload($bucket, $object);
  62. } catch (OssException $e) {
  63. printf(__FUNCTION__ . ": initiateMultipartUpload FAILED\n");
  64. printf($e->getMessage() . "\n");
  65. return;
  66. }
  67. print(__FUNCTION__ . ": initiateMultipartUpload OK" . "\n");
  68. /*
  69. * step 2. Upload parts
  70. */
  71. $partSize = 10 * 1024 * 1024;
  72. $uploadFile = __FILE__;
  73. $uploadFileSize = filesize($uploadFile);
  74. $pieces = $ossClient->generateMultiuploadParts($uploadFileSize, $partSize);
  75. $responseUploadPart = array();
  76. $uploadPosition = 0;
  77. $isCheckMd5 = true;
  78. foreach ($pieces as $i => $piece) {
  79. $fromPos = $uploadPosition + (integer)$piece[$ossClient::OSS_SEEK_TO];
  80. $toPos = (integer)$piece[$ossClient::OSS_LENGTH] + $fromPos - 1;
  81. $upOptions = array(
  82. $ossClient::OSS_FILE_UPLOAD => $uploadFile,
  83. $ossClient::OSS_PART_NUM => ($i + 1),
  84. $ossClient::OSS_SEEK_TO => $fromPos,
  85. $ossClient::OSS_LENGTH => $toPos - $fromPos + 1,
  86. $ossClient::OSS_CHECK_MD5 => $isCheckMd5,
  87. );
  88. if ($isCheckMd5) {
  89. $contentMd5 = OssUtil::getMd5SumForFile($uploadFile, $fromPos, $toPos);
  90. $upOptions[$ossClient::OSS_CONTENT_MD5] = $contentMd5;
  91. }
  92. //2. Upload each part to OSS
  93. try {
  94. $responseUploadPart[] = $ossClient->uploadPart($bucket, $object, $uploadId, $upOptions);
  95. } catch (OssException $e) {
  96. printf(__FUNCTION__ . ": initiateMultipartUpload, uploadPart - part#{$i} FAILED\n");
  97. printf($e->getMessage() . "\n");
  98. return;
  99. }
  100. printf(__FUNCTION__ . ": initiateMultipartUpload, uploadPart - part#{$i} OK\n");
  101. }
  102. $uploadParts = array();
  103. foreach ($responseUploadPart as $i => $eTag) {
  104. $uploadParts[] = array(
  105. 'PartNumber' => ($i + 1),
  106. 'ETag' => $eTag,
  107. );
  108. }
  109. /**
  110. * step 3. Complete the upload
  111. */
  112. try {
  113. $ossClient->completeMultipartUpload($bucket, $object, $uploadId, $uploadParts);
  114. } catch (OssException $e) {
  115. printf(__FUNCTION__ . ": completeMultipartUpload FAILED\n");
  116. printf($e->getMessage() . "\n");
  117. return;
  118. }
  119. printf(__FUNCTION__ . ": completeMultipartUpload OK\n");
  120. }
  121. /**
  122. * Upload by directories
  123. *
  124. * @param OssClient $ossClient OssClient
  125. * @param string $bucket bucket name
  126. *
  127. */
  128. function uploadDir($ossClient, $bucket)
  129. {
  130. $localDirectory = ".";
  131. $prefix = "samples/codes";
  132. try {
  133. $ossClient->uploadDir($bucket, $prefix, $localDirectory);
  134. } catch (OssException $e) {
  135. printf(__FUNCTION__ . ": FAILED\n");
  136. printf($e->getMessage() . "\n");
  137. return;
  138. }
  139. printf(__FUNCTION__ . ": completeMultipartUpload OK\n");
  140. }
  141. /**
  142. * Get ongoing multipart uploads
  143. *
  144. * @param $ossClient OssClient
  145. * @param $bucket string
  146. */
  147. function listMultipartUploads($ossClient, $bucket)
  148. {
  149. $options = array(
  150. 'max-uploads' => 100,
  151. 'key-marker' => '',
  152. 'prefix' => '',
  153. 'upload-id-marker' => ''
  154. );
  155. try {
  156. $listMultipartUploadInfo = $ossClient->listMultipartUploads($bucket, $options);
  157. } catch (OssException $e) {
  158. printf(__FUNCTION__ . ": listMultipartUploads FAILED\n");
  159. printf($e->getMessage() . "\n");
  160. return;
  161. }
  162. printf(__FUNCTION__ . ": listMultipartUploads OK\n");
  163. $listUploadInfo = $listMultipartUploadInfo->getUploads();
  164. var_dump($listUploadInfo);
  165. }