OssClientMultipartUploadTest.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. <?php
  2. namespace OSS\Tests;
  3. use OSS\Core\OssException;
  4. use OSS\OssClient;
  5. use OSS\Core\OssUtil;
  6. require_once __DIR__ . DIRECTORY_SEPARATOR . 'TestOssClientBase.php';
  7. class OssClientMultipartUploadTest extends TestOssClientBase
  8. {
  9. public function testInvalidDir()
  10. {
  11. try {
  12. $this->ossClient->uploadDir($this->bucket, "", "abc/ds/s/s/notexitst");
  13. $this->assertFalse(true);
  14. } catch (OssException $e) {
  15. $this->assertEquals("parameter error: abc/ds/s/s/notexitst is not a directory, please check it", $e->getMessage());
  16. }
  17. }
  18. public function testMultipartUploadBigFile()
  19. {
  20. $bigFileName = __DIR__ . DIRECTORY_SEPARATOR . "/bigfile.tmp";
  21. $localFilename = __DIR__ . DIRECTORY_SEPARATOR . "/localfile.tmp";
  22. OssUtil::generateFile($bigFileName, 6 * 1024 * 1024);
  23. $object = 'mpu/multipart-bigfile-test.tmp';
  24. try {
  25. $this->ossClient->multiuploadFile($this->bucket, $object, $bigFileName, array(OssClient::OSS_PART_SIZE => 1));
  26. $options = array(OssClient::OSS_FILE_DOWNLOAD => $localFilename);
  27. $this->ossClient->getObject($this->bucket, $object, $options);
  28. $this->assertEquals(md5_file($bigFileName), md5_file($localFilename));
  29. } catch (OssException $e) {
  30. var_dump($e->getMessage());
  31. $this->assertFalse(true);
  32. }
  33. unlink($bigFileName);
  34. unlink($localFilename);
  35. }
  36. public function testMultipartUploadBigFileWithMD5Check()
  37. {
  38. $bigFileName = __DIR__ . DIRECTORY_SEPARATOR . "/bigfile.tmp";
  39. $localFilename = __DIR__ . DIRECTORY_SEPARATOR . "/localfile.tmp";
  40. OssUtil::generateFile($bigFileName, 6 * 1024 * 1024);
  41. $object = 'mpu/multipart-bigfile-test.tmp';
  42. $options = array(
  43. OssClient::OSS_CHECK_MD5 => true,
  44. OssClient::OSS_PART_SIZE => 1,
  45. );
  46. try {
  47. $this->ossClient->multiuploadFile($this->bucket, $object, $bigFileName, $options);
  48. $options = array(OssClient::OSS_FILE_DOWNLOAD => $localFilename);
  49. $this->ossClient->getObject($this->bucket, $object, $options);
  50. $this->assertEquals(md5_file($bigFileName), md5_file($localFilename));
  51. } catch (OssException $e) {
  52. var_dump($e->getMessage());
  53. $this->assertFalse(true);
  54. }
  55. unlink($bigFileName);
  56. unlink($localFilename);
  57. }
  58. public function testCopyPart()
  59. {
  60. $object = "mpu/multipart-test.txt";
  61. $copiedObject = "mpu/multipart-test.txt.copied";
  62. $this->ossClient->putObject($this->bucket, $copiedObject, file_get_contents(__FILE__));
  63. /**
  64. * step 1. 初始化一个分块上传事件, 也就是初始化上传Multipart, 获取upload id
  65. */
  66. try {
  67. $upload_id = $this->ossClient->initiateMultipartUpload($this->bucket, $object);
  68. } catch (OssException $e) {
  69. $this->assertFalse(true);
  70. }
  71. /*
  72. * step 2. uploadPartCopy
  73. */
  74. $copyId = 1;
  75. $eTag = $this->ossClient->uploadPartCopy($this->bucket, $copiedObject, $this->bucket, $object, $copyId, $upload_id);
  76. $upload_parts[] = array(
  77. 'PartNumber' => $copyId,
  78. 'ETag' => $eTag,
  79. );
  80. try {
  81. $listPartsInfo = $this->ossClient->listParts($this->bucket, $object, $upload_id);
  82. $this->assertNotNull($listPartsInfo);
  83. } catch (OssException $e) {
  84. $this->assertTrue(false);
  85. }
  86. /**
  87. * step 3.
  88. */
  89. try {
  90. $this->ossClient->completeMultipartUpload($this->bucket, $object, $upload_id, $upload_parts);
  91. } catch (OssException $e) {
  92. var_dump($e->getMessage());
  93. $this->assertTrue(false);
  94. }
  95. $this->assertEquals($this->ossClient->getObject($this->bucket, $object), file_get_contents(__FILE__));
  96. $this->assertEquals($this->ossClient->getObject($this->bucket, $copiedObject), file_get_contents(__FILE__));
  97. }
  98. public function testAbortMultipartUpload()
  99. {
  100. $object = "mpu/multipart-test.txt";
  101. /**
  102. * step 1. 初始化一个分块上传事件, 也就是初始化上传Multipart, 获取upload id
  103. */
  104. try {
  105. $upload_id = $this->ossClient->initiateMultipartUpload($this->bucket, $object);
  106. } catch (OssException $e) {
  107. $this->assertFalse(true);
  108. }
  109. /*
  110. * step 2. 上传分片
  111. */
  112. $part_size = 10 * 1024 * 1024;
  113. $upload_file = __FILE__;
  114. $upload_filesize = filesize($upload_file);
  115. $pieces = $this->ossClient->generateMultiuploadParts($upload_filesize, $part_size);
  116. $response_upload_part = array();
  117. $upload_position = 0;
  118. $is_check_md5 = true;
  119. foreach ($pieces as $i => $piece) {
  120. $from_pos = $upload_position + (integer)$piece[OssClient::OSS_SEEK_TO];
  121. $to_pos = (integer)$piece[OssClient::OSS_LENGTH] + $from_pos - 1;
  122. $up_options = array(
  123. OssClient::OSS_FILE_UPLOAD => $upload_file,
  124. OssClient::OSS_PART_NUM => ($i + 1),
  125. OssClient::OSS_SEEK_TO => $from_pos,
  126. OssClient::OSS_LENGTH => $to_pos - $from_pos + 1,
  127. OssClient::OSS_CHECK_MD5 => $is_check_md5,
  128. );
  129. if ($is_check_md5) {
  130. $content_md5 = OssUtil::getMd5SumForFile($upload_file, $from_pos, $to_pos);
  131. $up_options[OssClient::OSS_CONTENT_MD5] = $content_md5;
  132. }
  133. //2. 将每一分片上传到OSS
  134. try {
  135. $response_upload_part[] = $this->ossClient->uploadPart($this->bucket, $object, $upload_id, $up_options);
  136. } catch (OssException $e) {
  137. $this->assertFalse(true);
  138. }
  139. }
  140. $upload_parts = array();
  141. foreach ($response_upload_part as $i => $eTag) {
  142. $upload_parts[] = array(
  143. 'PartNumber' => ($i + 1),
  144. 'ETag' => $eTag,
  145. );
  146. }
  147. try {
  148. $listPartsInfo = $this->ossClient->listParts($this->bucket, $object, $upload_id);
  149. $this->assertNotNull($listPartsInfo);
  150. } catch (OssException $e) {
  151. $this->assertTrue(false);
  152. }
  153. $this->assertEquals(1, count($listPartsInfo->getListPart()));
  154. $numOfMultipartUpload1 = 0;
  155. $options = null;
  156. try {
  157. $listMultipartUploadInfo = $listMultipartUploadInfo = $this->ossClient->listMultipartUploads($this->bucket, $options);
  158. $this->assertNotNull($listMultipartUploadInfo);
  159. $numOfMultipartUpload1 = count($listMultipartUploadInfo->getUploads());
  160. } catch (OssException $e) {
  161. $this->assertFalse(true);
  162. }
  163. try {
  164. $this->ossClient->abortMultipartUpload($this->bucket, $object, $upload_id);
  165. } catch (OssException $e) {
  166. $this->assertTrue(false);
  167. }
  168. $numOfMultipartUpload2 = 0;
  169. try {
  170. $listMultipartUploadInfo = $listMultipartUploadInfo = $this->ossClient->listMultipartUploads($this->bucket, $options);
  171. $this->assertNotNull($listMultipartUploadInfo);
  172. $numOfMultipartUpload2 = count($listMultipartUploadInfo->getUploads());
  173. } catch (OssException $e) {
  174. $this->assertFalse(true);
  175. }
  176. $this->assertEquals($numOfMultipartUpload1 - 1, $numOfMultipartUpload2);
  177. }
  178. public function testPutObjectByRawApis()
  179. {
  180. $object = "mpu/multipart-test.txt";
  181. /**
  182. * step 1. 初始化一个分块上传事件, 也就是初始化上传Multipart, 获取upload id
  183. */
  184. try {
  185. $upload_id = $this->ossClient->initiateMultipartUpload($this->bucket, $object);
  186. } catch (OssException $e) {
  187. $this->assertFalse(true);
  188. }
  189. /*
  190. * step 2. 上传分片
  191. */
  192. $part_size = 10 * 1024 * 1024;
  193. $upload_file = __FILE__;
  194. $upload_filesize = filesize($upload_file);
  195. $pieces = $this->ossClient->generateMultiuploadParts($upload_filesize, $part_size);
  196. $response_upload_part = array();
  197. $upload_position = 0;
  198. $is_check_md5 = true;
  199. foreach ($pieces as $i => $piece) {
  200. $from_pos = $upload_position + (integer)$piece[OssClient::OSS_SEEK_TO];
  201. $to_pos = (integer)$piece[OssClient::OSS_LENGTH] + $from_pos - 1;
  202. $up_options = array(
  203. OssClient::OSS_FILE_UPLOAD => $upload_file,
  204. OssClient::OSS_PART_NUM => ($i + 1),
  205. OssClient::OSS_SEEK_TO => $from_pos,
  206. OssClient::OSS_LENGTH => $to_pos - $from_pos + 1,
  207. OssClient::OSS_CHECK_MD5 => $is_check_md5,
  208. );
  209. if ($is_check_md5) {
  210. $content_md5 = OssUtil::getMd5SumForFile($upload_file, $from_pos, $to_pos);
  211. $up_options[OssClient::OSS_CONTENT_MD5] = $content_md5;
  212. }
  213. //2. 将每一分片上传到OSS
  214. try {
  215. $response_upload_part[] = $this->ossClient->uploadPart($this->bucket, $object, $upload_id, $up_options);
  216. } catch (OssException $e) {
  217. $this->assertFalse(true);
  218. }
  219. }
  220. $upload_parts = array();
  221. foreach ($response_upload_part as $i => $eTag) {
  222. $upload_parts[] = array(
  223. 'PartNumber' => ($i + 1),
  224. 'ETag' => $eTag,
  225. );
  226. }
  227. try {
  228. $listPartsInfo = $this->ossClient->listParts($this->bucket, $object, $upload_id);
  229. $this->assertNotNull($listPartsInfo);
  230. } catch (OssException $e) {
  231. $this->assertTrue(false);
  232. }
  233. /**
  234. * step 3.
  235. */
  236. try {
  237. $this->ossClient->completeMultipartUpload($this->bucket, $object, $upload_id, $upload_parts);
  238. } catch (OssException $e) {
  239. $this->assertTrue(false);
  240. }
  241. }
  242. function testPutObjectsByDir()
  243. {
  244. $localDirectory = dirname(__FILE__);
  245. $prefix = "samples/codes";
  246. try {
  247. $this->ossClient->uploadDir($this->bucket, $prefix, $localDirectory);
  248. } catch (OssException $e) {
  249. var_dump($e->getMessage());
  250. $this->assertFalse(true);
  251. }
  252. $this->assertTrue($this->ossClient->doesObjectExist($this->bucket, 'samples/codes/' . "OssClientMultipartUploadTest.php"));
  253. }
  254. public function testPutObjectByMultipartUpload()
  255. {
  256. $object = "mpu/multipart-test.txt";
  257. $file = __FILE__;
  258. $options = array();
  259. try {
  260. $this->ossClient->multiuploadFile($this->bucket, $object, $file, $options);
  261. } catch (OssException $e) {
  262. $this->assertFalse(true);
  263. }
  264. }
  265. public function testPutObjectByMultipartUploadWithMD5Check()
  266. {
  267. $object = "mpu/multipart-test.txt";
  268. $file = __FILE__;
  269. $options = array(OssClient::OSS_CHECK_MD5 => true);
  270. try {
  271. $this->ossClient->multiuploadFile($this->bucket, $object, $file, $options);
  272. } catch (OssException $e) {
  273. $this->assertFalse(true);
  274. }
  275. }
  276. public function testListMultipartUploads()
  277. {
  278. $options = null;
  279. try {
  280. $listMultipartUploadInfo = $this->ossClient->listMultipartUploads($this->bucket, $options);
  281. $this->assertNotNull($listMultipartUploadInfo);
  282. } catch (OssException $e) {
  283. $this->assertFalse(true);
  284. }
  285. }
  286. }