MultipartUpload.php 5.9 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. //*******************************简单使用***************************************************************
  10. /**
  11. * 查看完整用法中的 "putObjectByRawApis"函数,查看使用基础的分片上传api进行文件上传,用户可以基于这个自行实现断点续传等功能
  12. */
  13. // 使用分片上传接口上传文件, 接口会根据文件大小决定是使用普通上传还是分片上传
  14. $ossClient->multiuploadFile($bucket, "file.php", __FILE__, array());
  15. Common::println("local file " . __FILE__ . " is uploaded to the bucket $bucket, file.php");
  16. // 上传本地目录到bucket内的targetdir子目录中
  17. $ossClient->uploadDir($bucket, "targetdir", __DIR__);
  18. Common::println("local dir " . __DIR__ . " is uploaded to the bucket $bucket, targetdir/");
  19. // 列出当前未完成的分片上传
  20. $listMultipartUploadInfo = $ossClient->listMultipartUploads($bucket, array());
  21. //******************************* 完整用法参考下面函数 ****************************************************
  22. multiuploadFile($ossClient, $bucket);
  23. putObjectByRawApis($ossClient, $bucket);
  24. uploadDir($ossClient, $bucket);
  25. listMultipartUploads($ossClient, $bucket);
  26. /**
  27. * 通过multipart上传文件
  28. *
  29. * @param OssClient $ossClient OssClient实例
  30. * @param string $bucket 存储空间名称
  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. * 使用基本的api分阶段进行分片上传
  49. *
  50. * @param OssClient $ossClient OssClient实例
  51. * @param string $bucket 存储空间名称
  52. * @throws OssException
  53. */
  54. function putObjectByRawApis($ossClient, $bucket)
  55. {
  56. $object = "test/multipart-test.txt";
  57. /**
  58. * step 1. 初始化一个分块上传事件, 也就是初始化上传Multipart, 获取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. 上传分片
  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. 将每一分片上传到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. 完成上传
  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. * 按照目录上传文件
  123. *
  124. * @param OssClient $ossClient OssClient
  125. * @param string $bucket 存储空间名称
  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. * 获取当前未完成的分片上传列表
  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. }