PostObjectSample.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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 post object under specified bucket from
  18. * 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. $signature = 'obs';
  39. /*
  40. * Constructs a obs client instance with your account for accessing OBS
  41. */
  42. $obsClient = ObsClient::factory ( [
  43. 'key' => $ak,
  44. 'secret' => $sk,
  45. 'endpoint' => $endpoint,
  46. 'socket_timeout' => 30,
  47. 'connect_timeout' => 10,
  48. 'signature' => $signature
  49. ]);
  50. /*
  51. * Create bucket
  52. */
  53. printf("Create a new bucket for demo\n\n");
  54. $obsClient -> createBucket(['Bucket' => $bucketName]);
  55. /*
  56. * Create sample file
  57. */
  58. $sampleFilePath = '/temp/text.txt';
  59. createSampleFile($sampleFilePath);
  60. /*
  61. * Claim a post object request
  62. */
  63. $formParams = [];
  64. if (strcasecmp($signature, 'obs') === 0) {
  65. $formParams['x-obs-acl'] = ObsClient::AclPublicRead;
  66. } else {
  67. $formParams['acl'] = ObsClient::AclPublicRead;
  68. }
  69. $formParams['content-type'] = 'text/plain';
  70. $res = $obsClient -> createPostSignature(['Bucket' => $bucketName, 'Key' => $objectKey, 'Expires' => 3600, 'FormParams' => $formParams]);
  71. $formParams['key'] = $objectKey;
  72. $formParams['policy'] = $res['Policy'];
  73. if (strcasecmp($signature, 'obs') === 0) {
  74. $formParams['Accesskeyid'] = $ak;
  75. } else {
  76. $formParams['AWSAccesskeyid'] = $ak;
  77. }
  78. $formParams['signature'] = $res['Signature'];
  79. printf("Creating object in browser-based post way\n\n");
  80. $boundary = '9431149156168';
  81. $buffers = [];
  82. $contentLength = 0;
  83. /*
  84. * Construct form data
  85. */
  86. $buffer = [];
  87. $first = true;
  88. foreach ($formParams as $key => $val){
  89. if(!$first){
  90. $buffer[] = "\r\n";
  91. }else{
  92. $first = false;
  93. }
  94. $buffer[] = "--";
  95. $buffer[] = $boundary;
  96. $buffer[] = "\r\n";
  97. $buffer[] = "Content-Disposition: form-data; name=\"";
  98. $buffer[] = strval($key);
  99. $buffer[] = "\"\r\n\r\n";
  100. $buffer[] = strval($val);
  101. }
  102. $buffer = implode('', $buffer);
  103. $contentLength += strlen($buffer);
  104. $buffers[] = $buffer;
  105. /*
  106. * Construct file description
  107. */
  108. $buffer = [];
  109. $buffer[] = "\r\n";
  110. $buffer[] = "--";
  111. $buffer[] = $boundary;
  112. $buffer[] = "\r\n";
  113. $buffer[] = "Content-Disposition: form-data; name=\"file\"; filename=\"";
  114. $buffer[] = "myfile";
  115. $buffer[] = "\"\r\n";
  116. $buffer[] = "Content-Type: text/plain";
  117. $buffer[] = "\r\n\r\n";
  118. $buffer = implode('', $buffer);
  119. $contentLength += strlen($buffer);
  120. $buffers[] = $buffer;
  121. /*
  122. * Construct file data
  123. */
  124. $buffer = [];
  125. $fp = fopen($sampleFilePath, 'r');
  126. if($fp){
  127. while(!feof($fp)){
  128. $buffer[] = fgetc($fp);
  129. }
  130. fclose($fp);
  131. }
  132. $buffer = implode('', $buffer);
  133. $contentLength += strlen($buffer);
  134. $buffers[] = $buffer;
  135. /*
  136. * Contruct end data
  137. */
  138. $buffer = [];
  139. $buffer[] = "\r\n--";
  140. $buffer[] = $boundary;
  141. $buffer[] = "--\r\n";
  142. $buffer = implode('', $buffer);
  143. $contentLength += strlen($buffer);
  144. $buffers[] = $buffer;
  145. $httpClient = new Client(['verify' => false]);
  146. $host = parse_url($endpoint)['host'];
  147. $host = $bucketName . '.' . $host;
  148. $url = 'https://' . $host . ':443';
  149. $headers = ['Content-Length' => strval($contentLength), 'Content-Type' => 'multipart/form-data; boundary=' . $boundary];
  150. try{
  151. $response = $httpClient -> request('POST', $url, ['body' => implode('', $buffers), 'headers'=> $headers]);
  152. printf('Post object successfully!');
  153. $response -> getBody()-> close();
  154. }catch (ClientException $ex){
  155. printf('Exception message:%s', $ex ->getMessage());
  156. }
  157. if(file_exists($sampleFilePath)){
  158. unlink($sampleFilePath);
  159. }
  160. function createSampleFile($filePath)
  161. {
  162. if(file_exists($filePath)){
  163. return;
  164. }
  165. $filePath = iconv('UTF-8', 'GBK', $filePath);
  166. if(is_string($filePath) && $filePath !== '')
  167. {
  168. $fp = null;
  169. $dir = dirname($filePath);
  170. try{
  171. if(!is_dir($dir))
  172. {
  173. mkdir($dir,0755,true);
  174. }
  175. if(($fp = fopen($filePath, 'w+')))
  176. {
  177. fwrite($fp, uniqid() . "\n");
  178. fwrite($fp, uniqid() . "\n");
  179. }
  180. }finally{
  181. if($fp){
  182. fclose($fp);
  183. }
  184. }
  185. }
  186. }