ObjectMetaSample.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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 set/get self-defined metadata for object
  18. * on 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. $objectKey = 'my-obs-object-key-demo';
  37. /*
  38. * Constructs a obs client instance with your account for accessing OBS
  39. */
  40. $obsClient = ObsClient::factory ( [
  41. 'key' => $ak,
  42. 'secret' => $sk,
  43. 'endpoint' => $endpoint,
  44. 'socket_timeout' => 30,
  45. 'connect_timeout' => 10
  46. ] );
  47. try
  48. {
  49. /*
  50. * Create bucket
  51. */
  52. printf("Create a new bucket for demo\n\n");
  53. $obsClient -> createBucket(['Bucket' => $bucketName]);
  54. /*
  55. * Create object
  56. */
  57. $content = 'Hello OBS';
  58. /*
  59. * Setting self-defined metadata
  60. */
  61. $metadata = [];
  62. $metadata['meta1'] = 'value1';
  63. $metadata['meta2'] = 'value2';
  64. $obsClient -> putObject(['Bucket' => $bucketName, 'Key' => $objectKey, 'Body' => $content, 'Metadata' => $metadata]);
  65. printf("Create object %s successfully!\n\n", $objectKey);
  66. /*
  67. * Get object metadata
  68. */
  69. $resp = $obsClient -> getObjectMetadata(['Bucket' => $bucketName, 'Key' => $objectKey]);
  70. printf("Getting object metadata:\n");
  71. foreach ($resp['Metadata'] as $key => $value){
  72. printf("\t%s=%s\n", $key, $value);
  73. }
  74. /*
  75. * Delete object
  76. */
  77. $obsClient -> deleteObject(['Bucket' => $bucketName, 'Key' => $objectKey]);
  78. } catch ( ObsException $e ) {
  79. echo 'Response Code:' . $e->getStatusCode () . PHP_EOL;
  80. echo 'Error Message:' . $e->getExceptionMessage () . PHP_EOL;
  81. echo 'Error Code:' . $e->getExceptionCode () . PHP_EOL;
  82. echo 'Request ID:' . $e->getRequestId () . PHP_EOL;
  83. echo 'Exception Type:' . $e->getExceptionType () . PHP_EOL;
  84. } finally{
  85. $obsClient->close ();
  86. }