ListObjectsInFolderSample.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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 list objects under a specified folder of a bucket
  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. /*
  37. * Constructs a obs client instance with your account for accessing OBS
  38. */
  39. $obsClient = ObsClient::factory ( [
  40. 'key' => $ak,
  41. 'secret' => $sk,
  42. 'endpoint' => $endpoint,
  43. 'socket_timeout' => 30,
  44. 'connect_timeout' => 10
  45. ] );
  46. try
  47. {
  48. /*
  49. * Create bucket
  50. */
  51. printf("Create a new bucket for demo\n\n");
  52. $obsClient -> createBucket(['Bucket' => $bucketName]);
  53. /*
  54. * First prepare folders and sub folders
  55. */
  56. $keys = [];
  57. $promise = null;
  58. $keyPrefix = 'MyObjectKey';
  59. $folderPrefix = 'src';
  60. $subFolderPrefix = 'test';
  61. for($i = 0; $i<5; $i++){
  62. $key = $folderPrefix . $i . '/';
  63. $obsClient -> putObject(['Bucket'=>$bucketName, 'Key' => $key]);
  64. $keys[] = ['Key' => $key];
  65. for($j = 0; $j < 3; $j++){
  66. $subKey = $key . $subFolderPrefix . $j . '/';
  67. $obsClient -> putObject(['Bucket'=>$bucketName, 'Key' => $subKey]);
  68. $keys[] = ['Key' => $subKey];
  69. }
  70. }
  71. /*
  72. * Insert 2 objects in each folder
  73. */
  74. $resp = $obsClient -> listObjects(['Bucket' => $bucketName]);
  75. foreach ($resp ['Contents'] as $content ) {
  76. for($k =0; $k < 2; $k++){
  77. $objectKey = $content['Key'] . $keyPrefix . $k;
  78. $obsClient -> putObject(['Bucket'=>$bucketName, 'Key' => $objectKey, 'Body' => 'Hello OBS']);
  79. $keys[] = ['Key' => $objectKey];
  80. }
  81. }
  82. /*
  83. * Insert 2 objects in root path
  84. */
  85. $obsClient -> putObject(['Bucket'=>$bucketName, 'Key' => $keyPrefix . '0', 'Body' => 'Hello OBS']);
  86. $obsClient -> putObject(['Bucket'=>$bucketName, 'Key' => $keyPrefix . '1', 'Body' => 'Hello OBS']);
  87. printf("Put %d objects completed.\n\n", count($keys));
  88. /*
  89. * List all objects in folder src0/
  90. */
  91. printf("List all objects in folder src0/\n\n");
  92. $resp = $obsClient -> listObjects(['Bucket' => $bucketName, 'Prefix' => 'src0/']);
  93. foreach ( $resp ['Contents'] as $content ) {
  94. printf("\t%s etag[%s]\n", $content ['Key'], $content ['ETag']);
  95. }
  96. printf("\n");
  97. /*
  98. * List all objects in sub folder src0/test0/
  99. */
  100. printf("List all objects in folder src0/test0/\n\n");
  101. $resp = $obsClient -> listObjects(['Bucket' => $bucketName, 'Prefix' => 'src0/test0/']);
  102. foreach ( $resp ['Contents'] as $content ) {
  103. printf("\t%s etag[%s]\n", $content ['Key'], $content ['ETag']);
  104. }
  105. printf("\n");
  106. /*
  107. * List all objects group by folder
  108. */
  109. printf("List all objects group by folder\n\n");
  110. $resp = $obsClient -> listObjects(['Bucket' => $bucketName, 'Delimiter' => '/']);
  111. printf("Root path:\n");
  112. foreach ( $resp ['Contents'] as $content ) {
  113. printf("\t%s etag[%s]\n", $content ['Key'], $content ['ETag']);
  114. }
  115. listObjectsByPrefix($resp);
  116. printf("\n");
  117. /*
  118. * Delete all the objects created
  119. */
  120. $resp = $obsClient->deleteObjects([
  121. 'Bucket'=>$bucketName,
  122. 'Objects'=>$keys,
  123. 'Quiet'=> false,
  124. ]);
  125. printf("Delete results:\n\n");
  126. $i = 0;
  127. foreach ($resp['Deleteds'] as $delete)
  128. {
  129. printf("\tDeleteds[$i][Key]:%s,Deleted[$i][VersionId]:%s,Deleted[$i][DeleteMarker]:%s,Deleted[$i][DeleteMarkerVersionId]:%s\n",
  130. $delete['Key'],$delete['VersionId'],$delete['DeleteMarker'],$delete['DeleteMarkerVersionId']);
  131. $i++;
  132. }
  133. printf("\n");
  134. printf("Error results:\n\n");
  135. $i = 0;
  136. foreach ($resp['Errors'] as $error)
  137. {
  138. printf("\tErrors[$i][Key]:%s,Errors[$i][VersionId]:%s,Errors[$i][Code]:%s,Errors[$i][Message]:%s\n",
  139. $error['Key'],$error['VersionId'],$error['Code'],$error['Message']);
  140. $i++;
  141. }
  142. } catch ( ObsException $e ) {
  143. echo 'Response Code:' . $e->getStatusCode () . PHP_EOL;
  144. echo 'Error Message:' . $e->getExceptionMessage () . PHP_EOL;
  145. echo 'Error Code:' . $e->getExceptionCode () . PHP_EOL;
  146. echo 'Request ID:' . $e->getRequestId () . PHP_EOL;
  147. echo 'Exception Type:' . $e->getExceptionType () . PHP_EOL;
  148. } finally{
  149. $obsClient->close ();
  150. }
  151. function listObjectsByPrefix($resp){
  152. global $obsClient;
  153. global $bucketName;
  154. while(!empty($resp ['CommonPrefixes'])){
  155. foreach ($resp ['CommonPrefixes'] as $commonPrefix){
  156. $commonPrefix = $commonPrefix['Prefix'];
  157. printf("Folder %s:\n", $commonPrefix);
  158. $resp = $obsClient -> listObjects(['Bucket' => $bucketName, 'Delimiter' => '/', 'Prefix' => $commonPrefix]);
  159. foreach ( $resp ['Contents'] as $content ) {
  160. printf("\t%s etag[%s]\n", $content ['Key'], $content ['ETag']);
  161. }
  162. listObjectsByPrefix($resp);
  163. }
  164. }
  165. }