RestoreObjectSample.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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 download an cold object
  18. * from 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-cold-bucket-demo';
  36. $objectKey = 'my-obs-cold-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 a cold bucket
  51. */
  52. printf("Create a new cold bucket for demo\n\n");
  53. $obsClient -> createBucket(['Bucket' => $bucketName, 'StorageClass' => ObsClient::StorageClassCold]);
  54. /*
  55. * Create a cold object
  56. */
  57. printf("Create a new cold object for demo\n\n");
  58. $content = 'Hello OBS';
  59. $obsClient -> putObject(['Bucket' => $bucketName, 'Key' => $objectKey, 'Body' => $content]);
  60. /*
  61. * Restore the cold object
  62. */
  63. printf("Restore the cold object\n\n");
  64. $obsClient -> restoreObject([
  65. 'Bucket' => $bucketName,
  66. 'Key' => $objectKey,
  67. 'Days' => 1,
  68. 'Tier' => ObsClient::RestoreTierExpedited
  69. ]);
  70. /*
  71. * Wait 6 minute to get the object
  72. */
  73. sleep(60 * 6);
  74. /*
  75. * Get the cold object
  76. */
  77. printf("Get the cold object\n");
  78. $resp = $obsClient -> getObject(['Bucket' => $bucketName, 'Key' => $objectKey]);
  79. printf("\t%s\n\n", $resp['Body']);
  80. /*
  81. * Delete the cold object
  82. */
  83. $obsClient -> deleteObject(['Bucket' => $bucketName, 'Key' => $objectKey]);
  84. } catch ( ObsException $e ) {
  85. echo 'Response Code:' . $e->getStatusCode () . PHP_EOL;
  86. echo 'Error Message:' . $e->getExceptionMessage () . PHP_EOL;
  87. echo 'Error Code:' . $e->getExceptionCode () . PHP_EOL;
  88. echo 'Request ID:' . $e->getRequestId () . PHP_EOL;
  89. echo 'Exception Type:' . $e->getExceptionType () . PHP_EOL;
  90. } finally{
  91. $obsClient->close ();
  92. }