TemporarySignatureSample.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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 common operations in temporary signature way
  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 GuzzleHttp\Client;
  32. use GuzzleHttp\Exception\ClientException;
  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. $httpClient = new Client(['verify' => false]);
  49. /*
  50. * Create bucket
  51. */
  52. $method = 'PUT';
  53. $res = $obsClient -> createSignedUrl(['Bucket' => $bucketName, 'Method' => $method]);
  54. doAction('Create bucket', $method, $res['SignedUrl']);
  55. /*
  56. * Set/Get/Delete bucket cors
  57. */
  58. $method = 'PUT';
  59. $content = '<CORSConfiguration><CORSRule><AllowedMethod>PUT</AllowedMethod><AllowedOrigin>http://www.a.com</AllowedOrigin><AllowedHeader>header1</AllowedHeader><MaxAgeSeconds>100</MaxAgeSeconds><ExposeHeader>header2</ExposeHeader></CORSRule></CORSConfiguration>';
  60. $headers = ['Content-Length'=> strval(strlen($content)), 'Content-MD5' => base64_encode(md5($content, true))];
  61. $res = $obsClient -> createSignedUrl(['Bucket' => $bucketName, 'Method' => $method, 'SpecialParam' => 'cors', 'Headers' => $headers]);
  62. doAction('Set bucket cors ', $method, $res['SignedUrl'], $content, $res['ActualSignedRequestHeaders']);
  63. $method = 'GET';
  64. $res= $obsClient -> createSignedUrl(['Bucket' => $bucketName, 'Method' => $method, 'SpecialParam' => 'cors']);
  65. doAction('Get bucket cors ', $method, $res['SignedUrl']);
  66. $method = 'DELETE';
  67. $res= $obsClient -> createSignedUrl(['Bucket' => $bucketName, 'Method' => $method, 'SpecialParam' => 'cors']);
  68. doAction('Delete bucket cors ', $method, $res['SignedUrl']);
  69. /*
  70. * Create object
  71. */
  72. $method = 'PUT';
  73. $content = 'Hello OBS';
  74. $headers = ['Content-Length'=> strval(strlen($content))];
  75. $res = $obsClient -> createSignedUrl(['Method' => $method, 'Bucket' => $bucketName, 'Key' => $objectKey, 'Headers'=> $headers]);
  76. doAction('Create object', $method, $res['SignedUrl'], $content, $res['ActualSignedRequestHeaders']);
  77. /*
  78. * Get object
  79. */
  80. $method = 'GET';
  81. $res = $obsClient -> createSignedUrl(['Method' => $method, 'Bucket' => $bucketName, 'Key' => $objectKey]);
  82. doAction('Get object', $method, $res['SignedUrl']);
  83. /*
  84. * Set/Get object acl
  85. */
  86. $method = 'PUT';
  87. $headers = ['x-amz-acl'=> ObsClient::AclPublicRead];
  88. $res = $obsClient -> createSignedUrl(['Method' => $method, 'Bucket' => $bucketName, 'Key' => $objectKey, 'Headers'=> $headers, 'SpecialParam' => 'acl']);
  89. doAction('Set object Acl', $method, $res['SignedUrl'], null, $res['ActualSignedRequestHeaders']);
  90. $method = 'GET';
  91. $res = $obsClient -> createSignedUrl(['Method' => $method, 'Bucket' => $bucketName, 'Key' => $objectKey, 'SpecialParam' => 'acl']);
  92. doAction('Get object Acl', $method, $res['SignedUrl']);
  93. /*
  94. * Delete object
  95. */
  96. $method = 'DELETE';
  97. $res = $obsClient -> createSignedUrl(['Method' => $method, 'Bucket' => $bucketName, 'Key' => $objectKey]);
  98. doAction('Delete object', $method, $res['SignedUrl']);
  99. /*
  100. * Delete bucket
  101. */
  102. $method = 'DELETE';
  103. $res = $obsClient -> createSignedUrl(['Bucket' => $bucketName, 'Method' => $method]);
  104. doAction('Delete bucket', $method, $res['SignedUrl']);
  105. function doAction($msg, $method, $url, $content=null, $headers=null){
  106. global $httpClient;
  107. try{
  108. $response = $httpClient -> request($method, $url, ['body' => $content, 'headers'=> $headers]);
  109. printf("%s using temporary signature url:\n", $msg);
  110. printf("\t%s successfully.\n", $url);
  111. printf("\tStatus:%d\n", $response -> getStatusCode());
  112. printf("\tContent:%s\n", $response -> getBody() -> getContents());
  113. $response -> getBody()-> close();
  114. }catch (ClientException $ex){
  115. printf("%s using temporary signature url:\n", $msg);
  116. printf("\t%s failed!\n", $url);
  117. printf('Exception message:%s', $ex ->getMessage());
  118. }
  119. printf("\n");
  120. }