OssClientListObjectsV2Test.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. <?php
  2. namespace OSS\Tests;
  3. use OSS\Core\OssException;
  4. use OSS\OssClient;
  5. require_once __DIR__ . DIRECTORY_SEPARATOR . 'TestOssClientBase.php';
  6. class OssClientListObjectsV2Test extends TestOssClientBase
  7. {
  8. protected function setUp(): void
  9. {
  10. parent::setUp();
  11. //folder
  12. for ($i = 0; $i < 12; $i++) {
  13. $key = 'folder/'. sprintf("%02d",$i);
  14. $this->ossClient->putObject($this->bucket, $key, "content");
  15. }
  16. //test
  17. for ($i = 0; $i < 8; $i++) {
  18. $key = 'test/'. sprintf("%02d",$i);
  19. $this->ossClient->putObject($this->bucket, $key, "content");
  20. }
  21. //work
  22. for ($i = 0; $i < 5; $i++) {
  23. $key = 'work/'. sprintf("%02d",$i);
  24. $this->ossClient->putObject($this->bucket, $key, "content");
  25. }
  26. //sub++
  27. for ($i = 0; $i < 3; $i++) {
  28. $key = 'sub++/'. sprintf("%02d",$i);
  29. $this->ossClient->putObject($this->bucket, $key, "content");
  30. }
  31. //file++
  32. for ($i = 0; $i < 2; $i++) {
  33. $key = 'file++'. sprintf("%02d",$i);
  34. $this->ossClient->putObject($this->bucket, $key, "content");
  35. }
  36. }
  37. protected function tearDown(): void
  38. {
  39. parent::tearDown();
  40. }
  41. public function testListObjectsDefault()
  42. {
  43. try {
  44. $listObjectInfo = $this->ossClient->listObjectsV2($this->bucket);
  45. $objectList = $listObjectInfo->getObjectList();
  46. $prefixList = $listObjectInfo->getPrefixList();
  47. $this->assertNotNull($objectList);
  48. $this->assertNotNull($prefixList);
  49. $this->assertTrue(is_array($objectList));
  50. $this->assertTrue(is_array($prefixList));
  51. $this->assertEquals((2), count($objectList));
  52. $this->assertEquals(4, count($prefixList));
  53. $this->assertEquals('file++00', $objectList[0]->getKey());
  54. $this->assertEquals('file++01', $objectList[1]->getKey());
  55. $this->assertEquals('folder/', $prefixList[0]->getPrefix());
  56. $this->assertEquals('sub++/', $prefixList[1]->getPrefix());
  57. $this->assertEquals('test/', $prefixList[2]->getPrefix());
  58. $this->assertEquals('work/', $prefixList[3]->getPrefix());
  59. } catch (OssException $e) {
  60. $this->assertTrue(false);
  61. }
  62. }
  63. public function testListObjectsWithPrefix()
  64. {
  65. /**
  66. * List the files in your bucket.
  67. */
  68. $prefix = 'folder/';
  69. $delimiter = '';
  70. $maxkeys = 1000;
  71. $options = array(
  72. 'delimiter' => $delimiter,
  73. 'prefix' => $prefix,
  74. 'max-keys' => $maxkeys,
  75. );
  76. try {
  77. $listObjectInfo = $this->ossClient->listObjectsV2($this->bucket, $options);
  78. $objectList = $listObjectInfo->getObjectList();
  79. $prefixList = $listObjectInfo->getPrefixList();
  80. $this->assertNotNull($objectList);
  81. $this->assertNotNull($prefixList);
  82. $this->assertTrue(is_array($objectList));
  83. $this->assertTrue(is_array($prefixList));
  84. $this->assertEquals(12, count($objectList));
  85. $this->assertEquals(0, count($prefixList));
  86. $this->assertEquals('folder/00', $objectList[0]->getKey());
  87. $this->assertEquals('folder/01', $objectList[1]->getKey());
  88. $this->assertEquals('folder/11', $objectList[11]->getKey());
  89. } catch (OssException $e) {
  90. $this->assertTrue(false);
  91. }
  92. }
  93. public function testListObjectsWithMaxKeysAndMarker()
  94. {
  95. $count = 0;
  96. $options = array(
  97. 'delimiter' => '',
  98. 'max-keys' => 2,
  99. );
  100. while (true) {
  101. try {
  102. $listObjectInfo = $this->ossClient->listObjectsV2($this->bucket, $options);
  103. } catch (OssException $e) {
  104. $this->assertTrue(false);
  105. }
  106. $options[OssClient::OSS_CONTINUATION_TOKEN] = $listObjectInfo->getNextContinuationToken();
  107. $listObject = $listObjectInfo->getObjectList();
  108. $count += count($listObject);
  109. $this->assertEquals(2, count($listObject));
  110. if ($listObjectInfo->getIsTruncated() !== "true") {
  111. break;
  112. }
  113. }
  114. $this->assertEquals(12 + 8 + 5 + 3 + 2, $count);
  115. }
  116. public function testListObjectsWithStartAfter()
  117. {
  118. $count = 0;
  119. $options = array(
  120. 'delimiter' => '',
  121. 'start-after' => 'folder/11',
  122. 'max-keys' => 1,
  123. );
  124. while (true) {
  125. try {
  126. $listObjectInfo = $this->ossClient->listObjectsV2($this->bucket, $options);
  127. } catch (OssException $e) {
  128. $this->assertTrue(false);
  129. }
  130. $options[OssClient::OSS_CONTINUATION_TOKEN] = $listObjectInfo->getNextContinuationToken();
  131. $listObject = $listObjectInfo->getObjectList();
  132. $count += count($listObject);
  133. $this->assertEquals(1, count($listObject));
  134. if ($listObjectInfo->getIsTruncated() !== "true") {
  135. break;
  136. }
  137. }
  138. $this->assertEquals(8 + 5 + 3, $count);
  139. $options2 = array(
  140. 'delimiter' => '',
  141. 'start-after' => 'folder/11',
  142. 'max-keys' => 5,
  143. );
  144. try {
  145. $listObjectInfo = $this->ossClient->listObjectsV2($this->bucket, $options2);
  146. } catch (OssException $e) {
  147. $this->assertTrue(false);
  148. }
  149. $listObject = $listObjectInfo->getObjectList();
  150. $this->assertEquals(5, count($listObject));
  151. $this->assertEquals("true", $listObjectInfo->getIsTruncated());
  152. }
  153. }