DeleteObjectsSample.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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 delete 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. echo "Create a new bucket for demo\n\n";
  52. $obsClient -> createBucket(['Bucket' => $bucketName]);
  53. /*
  54. * Batch put objects into the bucket
  55. */
  56. $content = 'Thank you for using Object Storage Service';
  57. $keyPrefix = 'MyObjectKey';
  58. $keys = [];
  59. $start = microtime(true);
  60. // doUploadSync($keys, $keyPrefix, $content);
  61. doUploadAsync($keys, $keyPrefix, $content);
  62. printf("Cost " . round(microtime(true) - $start, 3) * 1000 . " ms to upload 100 objects\n\n");
  63. /*
  64. * Delete all objects uploaded recently under the bucket
  65. */
  66. printf("Deleting all objects\n\n");
  67. $resp = $obsClient->deleteObjects([
  68. 'Bucket'=>$bucketName,
  69. 'Objects'=>$keys,
  70. 'Quiet'=> false,
  71. ]);
  72. printf("Delete results:\n\n");
  73. $i = 0;
  74. foreach ($resp['Deleteds'] as $delete)
  75. {
  76. printf("\tDeleteds[$i][Key]:%s,Deleted[$i][VersionId]:%s,Deleted[$i][DeleteMarker]:%s,Deleted[$i][DeleteMarkerVersionId]:%s\n",
  77. $delete['Key'],$delete['VersionId'],$delete['DeleteMarker'],$delete['DeleteMarkerVersionId']);
  78. $i++;
  79. }
  80. printf("\n");
  81. printf("Error results:\n\n");
  82. $i = 0;
  83. foreach ($resp['Errors'] as $error)
  84. {
  85. printf("\tErrors[$i][Key]:%s,Errors[$i][VersionId]:%s,Errors[$i][Code]:%s,Errors[$i][Message]:%s\n",
  86. $error['Key'],$error['VersionId'],$error['Code'],$error['Message']);
  87. $i++;
  88. }
  89. } catch ( ObsException $e ) {
  90. echo 'Response Code:' . $e->getStatusCode () . PHP_EOL;
  91. echo 'Error Message:' . $e->getExceptionMessage () . PHP_EOL;
  92. echo 'Error Code:' . $e->getExceptionCode () . PHP_EOL;
  93. echo 'Request ID:' . $e->getRequestId () . PHP_EOL;
  94. echo 'Exception Type:' . $e->getExceptionType () . PHP_EOL;
  95. } finally{
  96. $obsClient->close ();
  97. }
  98. function doUploadSync(&$keys, $keyPrefix, $content)
  99. {
  100. global $obsClient;
  101. global $bucketName;
  102. for($i = 0;$i < 100;$i++){
  103. $key = $keyPrefix . strval($i);
  104. $obsClient -> putObject(['Bucket' => $bucketName, 'Key' => $key, 'Body' => $content]);
  105. printf("Succeed to put object %s\n\n", $key);
  106. $keys[] = ['Key' => $key];
  107. }
  108. }
  109. function doUploadAsync(&$keys, $keyPrefix, $content)
  110. {
  111. global $obsClient;
  112. global $bucketName;
  113. $promise = null;
  114. for($i = 0;$i < 100;$i++){
  115. $key = $keyPrefix . strval($i);
  116. $p = $obsClient -> putObjectAsync(['Bucket' => $bucketName, 'Key' => $key, 'Body' => $content],
  117. function($exception, $resp) use ($key){
  118. printf("Succeed to put object %s\n\n", $key);
  119. });
  120. if($promise === null){
  121. $promise = $p;
  122. }
  123. $keys[] = ['Key' => $key];
  124. }
  125. $promise -> wait();
  126. }