ObjectOperationsSample.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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 do object-related operations
  18. * (such as create/delete/get/copy object, do object ACL/OPTIONS)
  19. * on OBS using the OBS SDK for PHP.
  20. */
  21. if (file_exists ( 'vendor/autoload.php' )) {
  22. require 'vendor/autoload.php';
  23. } else {
  24. require '../vendor/autoload.php'; // sample env
  25. }
  26. if (file_exists ( 'obs-autoloader.php' )) {
  27. require 'obs-autoloader.php';
  28. } else {
  29. require '../obs-autoloader.php'; // sample env
  30. }
  31. use Obs\ObsClient;
  32. use Obs\ObsException;
  33. $ak = '*** Provide your Access Key ***';
  34. $sk = '*** Provide your Secret Key ***';
  35. $endpoint = 'https://your-endpoint:443';
  36. $bucketName = 'my-obs-bucket-demo';
  37. $objectKey = 'my-obs-object-key-demo';
  38. /*
  39. * Constructs a obs client instance with your account for accessing OBS
  40. */
  41. $obsClient = ObsClient::factory ( [
  42. 'key' => $ak,
  43. 'secret' => $sk,
  44. 'endpoint' => $endpoint,
  45. 'socket_timeout' => 30,
  46. 'connect_timeout' => 10
  47. ] );
  48. try
  49. {
  50. /*
  51. * Create bucket
  52. */
  53. printf("Create a new bucket for demo\n\n");
  54. $obsClient -> createBucket(['Bucket' => $bucketName]);
  55. /*
  56. * Create object
  57. */
  58. $content = 'Hello OBS';
  59. $obsClient -> putObject(['Bucket' => $bucketName, 'Key' => $objectKey, 'Body' => $content]);
  60. printf("Create object: %s successfully!\n\n", $objectKey);
  61. /*
  62. * Get object metadata
  63. */
  64. printf("Getting object metadata\n");
  65. $resp = $obsClient->getObjectMetadata([
  66. 'Bucket'=>$bucketName,
  67. 'Key'=>$objectKey,
  68. ]);
  69. printf("\tMetadata:%s\n\n", json_encode($resp));
  70. /*
  71. * Get object
  72. */
  73. printf("Getting object content\n");
  74. $resp = $obsClient -> getObject(['Bucket' => $bucketName, 'Key' => $objectKey]);
  75. printf("\t%s\n\n", $resp['Body']);
  76. /*
  77. * Copy object
  78. */
  79. $sourceBucketName = $bucketName;
  80. $destBucketName = $bucketName;
  81. $sourceObjectKey = $objectKey;
  82. $destObjectKey = $objectKey . '-back';
  83. printf("Copying object\n\n");
  84. $obsClient -> copyObject([
  85. 'Bucket'=> $destBucketName,
  86. 'Key'=> $destObjectKey,
  87. 'CopySource'=>$sourceBucketName . '/' . $sourceObjectKey,
  88. 'MetadataDirective' => ObsClient::CopyMetadata
  89. ]);
  90. /*
  91. * Options object
  92. */
  93. doObjectOptions();
  94. /*
  95. * Put/Get object acl operations
  96. */
  97. doObjectAclOperations();
  98. /*
  99. * Delete object
  100. */
  101. printf("Deleting objects\n\n");
  102. $obsClient -> deleteObject(['Bucket' => $bucketName, 'Key' => $objectKey]);
  103. $obsClient -> deleteObject(['Bucket' => $bucketName, 'Key' => $destObjectKey]);
  104. } catch ( ObsException $e ) {
  105. echo 'Response Code:' . $e->getStatusCode () . PHP_EOL;
  106. echo 'Error Message:' . $e->getExceptionMessage () . PHP_EOL;
  107. echo 'Error Code:' . $e->getExceptionCode () . PHP_EOL;
  108. echo 'Request ID:' . $e->getRequestId () . PHP_EOL;
  109. echo 'Exception Type:' . $e->getExceptionType () . PHP_EOL;
  110. } finally{
  111. $obsClient->close ();
  112. }
  113. function doObjectOptions()
  114. {
  115. global $obsClient;
  116. global $bucketName;
  117. global $objectKey;
  118. $obsClient->setBucketCors ( [
  119. 'Bucket' => $bucketName,
  120. 'CorsRule' => [
  121. [
  122. 'AllowedMethod' => ['HEAD', 'GET', 'PUT'],
  123. 'AllowedOrigin' => ['http://www.a.com', 'http://www.b.com'],
  124. 'AllowedHeader'=> ['Authorization'],
  125. 'ExposeHeaders' => ['x-obs-test1', 'x-obs-test2'],
  126. 'MaxAgeSeconds' => 100
  127. ]
  128. ]
  129. ] );
  130. $resp = $obsClient->optionsObject([
  131. 'Bucket'=>$bucketName,
  132. 'Key' => $objectKey,
  133. 'Origin'=>'http://www.a.com',
  134. 'AccessControlRequestMethods' => ['PUT'],
  135. 'AccessControlRequestHeaders'=> ['Authorization']
  136. ]);
  137. printf ("Options bucket: %s\n\n", json_encode($resp -> toArray()));
  138. }
  139. function doObjectAclOperations()
  140. {
  141. global $obsClient;
  142. global $bucketName;
  143. global $objectKey;
  144. printf("Setting object ACL to " . ObsClient::AclPublicRead . "\n\n");
  145. $obsClient ->setObjectAcl([
  146. 'Bucket' => $bucketName,
  147. 'Key' => $objectKey,
  148. 'ACL' => ObsClient::AclPublicRead
  149. ]);
  150. printf("Getting object ACL\n");
  151. $resp = $obsClient -> getObjectAcl([
  152. 'Bucket' => $bucketName,
  153. 'Key' => $objectKey
  154. ]);
  155. printf("\tOwner:%s\n", json_encode($resp['Owner']));
  156. printf("\tGrants:%s\n\n", json_encode($resp['Grants']));
  157. }