ListVersionsSample.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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 versions 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. * Enable bucket versioning
  55. */
  56. $obsClient -> setBucketVersioningConfiguration(['Bucket' => $bucketName, 'Status' => 'Enabled']);
  57. /*
  58. * First prepare folders and sub folders
  59. */
  60. $keys = [];
  61. $promise = null;
  62. $keyPrefix = 'MyObjectKey';
  63. $folderPrefix = 'src';
  64. $subFolderPrefix = 'test';
  65. for($i = 0; $i<5; $i++){
  66. $key = $folderPrefix . $i . '/';
  67. $obsClient -> putObject(['Bucket'=>$bucketName, 'Key' => $key]);
  68. $keys[] = ['Key' => $key];
  69. for($j = 0; $j < 3; $j++){
  70. $subKey = $key . $subFolderPrefix . $j . '/';
  71. $obsClient -> putObject(['Bucket'=>$bucketName, 'Key' => $subKey]);
  72. $keys[] = ['Key' => $subKey];
  73. }
  74. }
  75. /*
  76. * Insert 2 objects in each folder
  77. */
  78. $resp = $obsClient -> listObjects(['Bucket' => $bucketName]);
  79. foreach ($resp ['Contents'] as $content ) {
  80. for($k =0; $k < 2; $k++){
  81. $objectKey = $content['Key'] . $keyPrefix . $k;
  82. $obsClient -> putObject(['Bucket'=>$bucketName, 'Key' => $objectKey, 'Body' => 'Hello OBS']);
  83. $keys[] = ['Key' => $objectKey];
  84. }
  85. }
  86. /*
  87. * Insert 2 objects in root path
  88. */
  89. $obsClient -> putObject(['Bucket'=>$bucketName, 'Key' => $keyPrefix . '0', 'Body' => 'Hello OBS']);
  90. $obsClient -> putObject(['Bucket'=>$bucketName, 'Key' => $keyPrefix . '1', 'Body' => 'Hello OBS']);
  91. printf("Put %d objects completed.\n\n", count($keys));
  92. $keys = [];
  93. /*
  94. * List versions using default parameters, will return up to 1000 objects
  95. */
  96. $resp = $obsClient -> listVersions (['Bucket' => $bucketName ]);
  97. printf("\tVersions:\n");
  98. foreach ( $resp ['Versions'] as $version ) {
  99. printf("\t%s etag[%s] versionid[%s]\n", $version['Key'], $version['ETag'],$version['VersionId']);
  100. $keys[] = ['Key' => $version['Key'], 'VersionId' => $version['VersionId']];
  101. }
  102. printf("\n");
  103. printf("\tDeleteMarkers:\n");
  104. foreach ( $resp ['DeleteMarkers'] as $deleteMarker ) {
  105. printf("\t%s versionid[%s]\n", $deleteMarker['Key'], $deleteMarker['VersionId']);
  106. $keys[] = ['Key' => $deleteMarker['Key'], 'VersionId' => $deleteMarker['VersionId']];
  107. }
  108. printf("\n");
  109. /*
  110. * List all the versions in way of pagination
  111. */
  112. printf("List all the versions in way of pagination:\n");
  113. $nextMarker = null;
  114. $index = 1;
  115. do{
  116. $resp = $obsClient -> listVersions(['Bucket' => $bucketName, 'MaxKeys' => 10, 'KeyMarker' => $nextMarker]);
  117. $nextMarker = $resp['NextKeyMarker'];
  118. printf("Page:%d\n", $index++);
  119. printf("\tVersions:\n");
  120. foreach ( $resp ['Versions'] as $version ) {
  121. printf("\t%s etag[%s] versionid[%s]\n", $version['Key'], $version['ETag'],$version['VersionId']);
  122. }
  123. printf("\n");
  124. printf("\tDeleteMarkers:\n");
  125. foreach ( $resp ['DeleteMarkers'] as $deleteMarker ) {
  126. printf("\t%s versionid[%s]\n", $deleteMarker['Key'], $deleteMarker['VersionId']);
  127. }
  128. }while($resp['IsTruncated']);
  129. printf("\n");
  130. /*
  131. * List all versions group by folder
  132. */
  133. printf("List all versions group by folder \n");
  134. $resp = $obsClient -> listVersions(['Bucket' => $bucketName, 'Delimiter' => '/']);
  135. printf("Root path:\n");
  136. printf("\tVersions:\n");
  137. foreach ( $resp ['Versions'] as $version ) {
  138. printf("\t%s etag[%s] versionid[%s]\n", $version['Key'], $version['ETag'],$version['VersionId']);
  139. }
  140. printf("\n");
  141. printf("\tDeleteMarkers:\n");
  142. foreach ( $resp ['DeleteMarkers'] as $deleteMarker ) {
  143. printf("\t%s versionid[%s]\n", $deleteMarker['Key'], $deleteMarker['VersionId']);
  144. }
  145. listVersionsByPrefix($resp);
  146. printf("\n");
  147. /*
  148. * Delete all the objects created
  149. */
  150. $resp = $obsClient->deleteObjects([
  151. 'Bucket'=>$bucketName,
  152. 'Objects'=>$keys,
  153. 'Quiet'=> false,
  154. ]);
  155. printf("Delete results:\n\n");
  156. $i = 0;
  157. foreach ($resp['Deleteds'] as $delete)
  158. {
  159. printf("\tDeleteds[$i][Key]:%s,Deleted[$i][VersionId]:%s,Deleted[$i][DeleteMarker]:%s,Deleted[$i][DeleteMarkerVersionId]:%s\n",
  160. $delete['Key'],$delete['VersionId'],$delete['DeleteMarker'],$delete['DeleteMarkerVersionId']);
  161. $i++;
  162. }
  163. printf("\n");
  164. printf("Error results:\n\n");
  165. $i = 0;
  166. foreach ($resp['Errors'] as $error)
  167. {
  168. printf("\tErrors[$i][Key]:%s,Errors[$i][VersionId]:%s,Errors[$i][Code]:%s,Errors[$i][Message]:%s\n",
  169. $error['Key'],$error['VersionId'],$error['Code'],$error['Message']);
  170. $i++;
  171. }
  172. } catch ( ObsException $e ) {
  173. echo 'Response Code:' . $e->getStatusCode () . PHP_EOL;
  174. echo 'Error Message:' . $e->getExceptionMessage () . PHP_EOL;
  175. echo 'Error Code:' . $e->getExceptionCode () . PHP_EOL;
  176. echo 'Request ID:' . $e->getRequestId () . PHP_EOL;
  177. echo 'Exception Type:' . $e->getExceptionType () . PHP_EOL;
  178. } finally{
  179. $obsClient->close ();
  180. }
  181. function listVersionsByPrefix($resp){
  182. global $obsClient;
  183. global $bucketName;
  184. while(!empty($resp ['CommonPrefixes'])){
  185. foreach ($resp ['CommonPrefixes'] as $commonPrefix){
  186. $commonPrefix = $commonPrefix['Prefix'];
  187. printf("Folder %s:\n", $commonPrefix);
  188. $resp = $obsClient -> listVersions(['Bucket' => $bucketName, 'Delimiter' => '/', 'Prefix' => $commonPrefix]);
  189. printf("\tVersions:\n");
  190. foreach ( $resp ['Versions'] as $version ) {
  191. printf("\t%s etag[%s] versionid[%s]\n", $version['Key'], $version['ETag'],$version['VersionId']);
  192. }
  193. printf("\n");
  194. printf("\tDeleteMarkers:\n");
  195. foreach ( $resp ['DeleteMarkers'] as $deleteMarker ) {
  196. printf("\t%s versionid[%s]\n", $deleteMarker['Key'], $deleteMarker['VersionId']);
  197. }
  198. listVersionsByPrefix($resp);
  199. }
  200. }
  201. }