ctms/vendor/huawei/obsclient/SimpleMultipartUpload.php
2025-04-10 23:19:13 +08:00

109 lines
2.9 KiB
PHP
Executable File

<?php
# @Author: 嗨噜客(三亚) <fm453>
# @Date: 2022-05-18T16:24:32+08:00
# @Email: fm453@lukegzs.com
# @Last modified by: fm453
# @Last modified time: 2022-05-18T16:24:32+08:00
# @Copyright: www.hiluker.cn
/**
* Copyright 2019 Huawei Technologies Co.,Ltd.
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use
* this file except in compliance with the License. You may obtain a copy of the
* License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed
* under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
* CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*
*/
namespace vendor\huawei\obsclient;
if (file_exists('vendor/autoload.php')) {
require 'vendor/autoload.php';
}
if (file_exists('obs-autoloader.php')) {
require 'obs-autoloader.php';
}
use Obs\ObsClient;
use Obs\ObsException;
$ak = '*** Provide your Access Key ***';
$sk = '*** Provide your Secret Key ***';
$endpoint = 'https://your-endpoint:443';
$bucketName = 'my-obs-bucket-demo';
$objectKey = 'my-obs-object-key-demo';
/*
* Constructs a obs client instance with your account for accessing OBS
*/
$obsClient = ObsClient::factory([
'key' => $ak,
'secret' => $sk,
'endpoint' => $endpoint,
'socket_timeout' => 30,
'connect_timeout' => 10
]);
try {
printf("Create a new bucket for demo\n\n");
$obsClient -> createBucket(['Bucket' => $bucketName]);
/*
* Step 1: initiate multipart upload
*/
printf("Step 1: initiate multipart upload\n\n");
$resp = $obsClient -> initiateMultipartUpload(['Bucket'=>$bucketName,
'Key'=>$objectKey]);
$uploadId = $resp['UploadId'];
/*
* Step 2: upload a part
*/
printf("Step 2: upload a part\n\n");
$resp = $obsClient->uploadPart([
'Bucket'=>$bucketName,
'Key' => $objectKey,
'UploadId'=>$uploadId,
'PartNumber'=>1,
'Body' => 'Hello OBS'
]);
$etag = $resp['ETag'];
/*
* Step 3: complete multipart upload
*/
printf("Step 3: complete multipart upload\n\n");
$obsClient->completeMultipartUpload([
'Bucket'=>$bucketName,
'Key'=>$objectKey,
'UploadId'=>$uploadId,
'Parts'=>[
['PartNumber'=>1,'ETag'=>$etag]
],
]);
} catch (ObsException $e) {
echo 'Response Code:' . $e->getStatusCode() . PHP_EOL;
echo 'Error Message:' . $e->getExceptionMessage() . PHP_EOL;
echo 'Error Code:' . $e->getExceptionCode() . PHP_EOL;
echo 'Request ID:' . $e->getRequestId() . PHP_EOL;
echo 'Exception Type:' . $e->getExceptionType() . PHP_EOL;
} finally {
$obsClient->close();
}