ListObjectsSample.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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 specified 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 insert 100 objects for demo
  55. */
  56. $promise = null;
  57. $keyPrefix = 'MyObjectKey';
  58. for($i = 0;$i < 100;$i++){
  59. $key = $keyPrefix . strval($i);
  60. $p = $obsClient -> putObjectAsync(['Bucket' => $bucketName, 'Key' => $key, 'Body' => 'Hello OBS'],function(){});
  61. if($promise === null){
  62. $promise = $p;
  63. }
  64. $keys[] = ['Key' => $key];
  65. }
  66. $promise -> wait();
  67. printf("Put %d objects completed.\n\n", count($keys));
  68. /*
  69. * List objects using default parameters, will return up to 1000 objects
  70. */
  71. printf("List objects using default parameters:\n");
  72. $resp = $obsClient -> listObjects(['Bucket' => $bucketName]);
  73. foreach ( $resp ['Contents'] as $content ) {
  74. printf("\t%s etag[%s]\n", $content ['Key'], $content ['ETag']);
  75. }
  76. printf("\n");
  77. /*
  78. * List the first 10 objects
  79. */
  80. printf("List the first 10 objects:\n");
  81. $resp = $obsClient -> listObjects(['Bucket' => $bucketName, 'MaxKeys' => 10]);
  82. foreach ( $resp ['Contents'] as $content ) {
  83. printf("\t%s etag[%s]\n", $content ['Key'], $content ['ETag']);
  84. }
  85. printf("\n");
  86. $theSecond10ObjectsMarker = $resp['NextMarker'];
  87. /*
  88. * List the second 10 objects using marker
  89. */
  90. printf("List the second 10 objects using marker:\n");
  91. $resp = $obsClient -> listObjects(['Bucket' => $bucketName, 'MaxKeys' => 10, 'Marker' => $theSecond10ObjectsMarker]);
  92. foreach ( $resp ['Contents'] as $content ) {
  93. printf("\t%s etag[%s]\n", $content ['Key'], $content ['ETag']);
  94. }
  95. printf("\n");
  96. /*
  97. * List objects with prefix and max keys
  98. */
  99. printf("List objects with prefix and max keys:\n");
  100. $resp = $obsClient -> listObjects(['Bucket' => $bucketName, 'MaxKeys' => 5, 'Prefix' => $keyPrefix . '2']);
  101. foreach ( $resp ['Contents'] as $content ) {
  102. printf("\t%s etag[%s]\n", $content ['Key'], $content ['ETag']);
  103. }
  104. printf("\n");
  105. /*
  106. * List all the objects in way of pagination
  107. */
  108. printf("List all the objects in way of pagination:\n");
  109. $nextMarker = null;
  110. $index = 1;
  111. do{
  112. $resp = $obsClient -> listObjects(['Bucket' => $bucketName, 'MaxKeys' => 10, 'Marker' => $nextMarker]);
  113. $nextMarker = $resp['NextMarker'];
  114. printf("Page:%d\n", $index++);
  115. foreach ( $resp ['Contents'] as $content ) {
  116. printf("\t%s etag[%s]\n", $content ['Key'], $content ['ETag']);
  117. }
  118. }while($resp['IsTruncated']);
  119. printf("\n");
  120. /*
  121. * Delete all the objects created
  122. */
  123. $resp = $obsClient->deleteObjects([
  124. 'Bucket'=>$bucketName,
  125. 'Objects'=>$keys,
  126. 'Quiet'=> false,
  127. ]);
  128. printf("Delete results:\n\n");
  129. $i = 0;
  130. foreach ($resp['Deleteds'] as $delete)
  131. {
  132. printf("\tDeleteds[$i][Key]:%s,Deleted[$i][VersionId]:%s,Deleted[$i][DeleteMarker]:%s,Deleted[$i][DeleteMarkerVersionId]:%s\n",
  133. $delete['Key'],$delete['VersionId'],$delete['DeleteMarker'],$delete['DeleteMarkerVersionId']);
  134. $i++;
  135. }
  136. printf("\n");
  137. printf("Error results:\n\n");
  138. $i = 0;
  139. foreach ($resp['Errors'] as $error)
  140. {
  141. printf("\tErrors[$i][Key]:%s,Errors[$i][VersionId]:%s,Errors[$i][Code]:%s,Errors[$i][Message]:%s\n",
  142. $error['Key'],$error['VersionId'],$error['Code'],$error['Message']);
  143. $i++;
  144. }
  145. } catch ( ObsException $e ) {
  146. echo 'Response Code:' . $e->getStatusCode () . PHP_EOL;
  147. echo 'Error Message:' . $e->getExceptionMessage () . PHP_EOL;
  148. echo 'Error Code:' . $e->getExceptionCode () . PHP_EOL;
  149. echo 'Request ID:' . $e->getRequestId () . PHP_EOL;
  150. echo 'Exception Type:' . $e->getExceptionType () . PHP_EOL;
  151. } finally{
  152. $obsClient->close ();
  153. }