DownloadSample.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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 object
  18. * from OBS in different ways 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. * Upload an object to your bucket
  56. */
  57. printf("Uploading a new object to OBS\n\n");
  58. $content = "abcdefghijklmnopqrstuvwxyz\n\t0123456789011234567890\n";
  59. $obsClient -> putObject(['Bucket' => $bucketName, 'Key' => $objectKey, 'Body' => $content]);
  60. /*
  61. * Download the object as an inputstream and display it directly
  62. */
  63. printf("Downloading an object\n");
  64. $resp = $obsClient -> getObject(['Bucket' => $bucketName, 'Key' => $objectKey]);
  65. printf("\t%s\n\n", $resp['Body']);
  66. /*
  67. * Download the object to a file
  68. */
  69. printf("Downloading an object to local file\n");
  70. $resp = $obsClient -> getObject(['Bucket' => $bucketName, 'Key' => $objectKey, 'SaveAsFile' => '/temp/' .$objectKey]);
  71. printf("\tSaveAsFile:%s\n\n", $resp['SaveAsFile']);
  72. printf("Deleting object %s \n\n", $objectKey);
  73. $obsClient -> deleteObject(['Bucket' => $bucketName, 'Key' => $objectKey]);
  74. } catch ( ObsException $e ) {
  75. echo 'Response Code:' . $e->getStatusCode () . PHP_EOL;
  76. echo 'Error Message:' . $e->getExceptionMessage () . PHP_EOL;
  77. echo 'Error Code:' . $e->getExceptionCode () . PHP_EOL;
  78. echo 'Request ID:' . $e->getRequestId () . PHP_EOL;
  79. echo 'Exception Type:' . $e->getExceptionType () . PHP_EOL;
  80. } finally{
  81. $obsClient->close ();
  82. }