SimpleMultipartUploadSample.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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 upload multiparts to OBS
  18. * 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. printf("Create a new bucket for demo\n\n");
  50. $obsClient -> createBucket(['Bucket' => $bucketName]);
  51. /*
  52. * Step 1: initiate multipart upload
  53. */
  54. printf("Step 1: initiate multipart upload\n\n");
  55. $resp = $obsClient -> initiateMultipartUpload(['Bucket'=>$bucketName,
  56. 'Key'=>$objectKey]);
  57. $uploadId = $resp['UploadId'];
  58. /*
  59. * Step 2: upload a part
  60. */
  61. printf("Step 2: upload a part\n\n");
  62. $resp = $obsClient->uploadPart([
  63. 'Bucket'=>$bucketName,
  64. 'Key' => $objectKey,
  65. 'UploadId'=>$uploadId,
  66. 'PartNumber'=>1,
  67. 'Body' => 'Hello OBS'
  68. ]);
  69. $etag = $resp['ETag'];
  70. /*
  71. * Step 3: complete multipart upload
  72. */
  73. printf("Step 3: complete multipart upload\n\n");
  74. $obsClient->completeMultipartUpload([
  75. 'Bucket'=>$bucketName,
  76. 'Key'=>$objectKey,
  77. 'UploadId'=>$uploadId,
  78. 'Parts'=>[
  79. ['PartNumber'=>1,'ETag'=>$etag]
  80. ],
  81. ]);
  82. } catch ( ObsException $e ) {
  83. echo 'Response Code:' . $e->getStatusCode () . PHP_EOL;
  84. echo 'Error Message:' . $e->getExceptionMessage () . PHP_EOL;
  85. echo 'Error Code:' . $e->getExceptionCode () . PHP_EOL;
  86. echo 'Request ID:' . $e->getRequestId () . PHP_EOL;
  87. echo 'Exception Type:' . $e->getExceptionType () . PHP_EOL;
  88. } finally{
  89. $obsClient->close ();
  90. }