CreateFolderSample.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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 create an empty folder under
  18. * specified bucket to 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. echo "Create a new bucket for demo\n\n";
  53. $obsClient -> createBucket(['Bucket' => $bucketName]);
  54. /*
  55. * Create an empty folder without request body, note that the key must be
  56. * suffixed with a slash
  57. */
  58. $keySuffixWithSlash = "MyObjectKey1/";
  59. $obsClient -> putObject(['Bucket' => $bucketName, 'Key' => $keySuffixWithSlash]);
  60. echo "Creating an empty folder " . $keySuffixWithSlash . "\n\n";
  61. /*
  62. * Verify whether the size of the empty folder is zero
  63. */
  64. $resp = $obsClient -> getObject(['Bucket' => $bucketName, 'Key' => $keySuffixWithSlash]);
  65. echo "Size of the empty folder '" . $keySuffixWithSlash. "' is " . $resp['ContentLength'] . "\n\n";
  66. if($resp['Body']){
  67. $resp['Body'] -> close();
  68. }
  69. /*
  70. * Create an object under the folder just created
  71. */
  72. $obsClient -> putObject(['Bucket' => $bucketName, 'Key' => $keySuffixWithSlash . $objectKey, 'Body' => 'Hello OBS']);
  73. } catch ( ObsException $e ) {
  74. echo 'Response Code:' . $e->getStatusCode () . PHP_EOL;
  75. echo 'Error Message:' . $e->getExceptionMessage () . PHP_EOL;
  76. echo 'Error Code:' . $e->getExceptionCode () . PHP_EOL;
  77. echo 'Request ID:' . $e->getRequestId () . PHP_EOL;
  78. echo 'Exception Type:' . $e->getExceptionType () . PHP_EOL;
  79. } finally{
  80. $obsClient->close ();
  81. }