Uploading Objects in Batches
If you have any questions during development, post them on the Issues page of GitHub. For details about parameters and usage of each API, see API Reference.
Sample Code
This example uploads files in a local directory to a specified OBS bucket in batches. You can recursively scan the local directory and upload the files concurrently based on the specified concurrency. The OBS object keys are generated according to the relative paths of the files. This method is suitable for scenarios such as local file batch migration and data backup uploads.
<?php
// Import the dependency library.
require 'vendor/autoload.php';
// Import the SDK code library during the installation with the source code.
// require 'obs-autoloader.php';
// Declare the namespace.
use Obs\ObsClient;
use GuzzleHttp\Promise\EachPromise;
use RecursiveDirectoryIterator;
use RecursiveIteratorIterator;
// Create an ObsClient instance.
$obsClient = new ObsClient([
// Obtain an AK/SK pair using environment variables (recommended) or import an AK/SK pair in other ways. Using hard coding may result in leakage.
// Obtain an AK/SK pair on the management console. For details, see https://support.huaweicloud.com/intl/en-us/usermanual-ca/ca_01_0003.html.
'key' => getenv('ACCESS_KEY_ID'),
'secret' => getenv('SECRET_ACCESS_KEY'),
// Enter the endpoint corresponding to the bucket. CN-Hong Kong is used here as an example. Replace it with the one currently in use.
'endpoint' => "obs.ap-southeast-1.myhuaweicloud.com",
'signature' => 'obs'
]);
$bucketName = 'bucketname';
$localDir = '/tmp/upload-dir';
$keyPrefix = 'backup/';
$concurrency = 5;
// Recursively scan the local directory.
$files = [];
$iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($localDir));
foreach ($iterator as $file) {
if ($file->isFile()) {
$relativePath = substr($file->getPathname(), strlen($localDir) + 1);
$files[] = ['Key' => $keyPrefix . str_replace('\\', '/', $relativePath), 'SourceFile' => $file->getPathname()];
}
}
printf("Found %d files to upload\n\n", count($files));
// Upload objects concurrently. A single putObject request cannot upload a file larger than 5 GB. For large files, use multipart upload.
// In the production environment, you are advised to add a retry mechanism to prevent data incompleteness caused by network fluctuation or temporary faults.
$successCount = 0;
$failCount = 0;
$promiseGenerator = function () use ($obsClient, $bucketName, $files, &$successCount, &$failCount) {
foreach ($files as $object) {
yield $obsClient->putObjectAsync(['Bucket' => $bucketName,
'Key' => $object['Key'],
'SourceFile' => $object['SourceFile']
], function ($exception, $resp) use ($object, &$successCount, &$failCount) {
if ($exception === null) {
printf("Key:%s, ETag:%s, RequestId:%s\n", $object['Key'], $resp['ETag'], $resp['RequestId']); $successCount++;
} else {
pprintf("UploadFailed: Key:%s, Status:%d, Message:%s\n", $object['Key'], $exception->getStatusCode(), $exception->getExceptionMessage() );
$failCount++;
}
}); }
};
$eachPromise = new EachPromise($promiseGenerator(), [
'concurrency' => $concurrency
]);
$eachPromise->promise()->wait();
printf("\nResult: success=%d, fail=%d\n", $successCount, $failCount); What is your overall rating for this page?
Thank you very much for your feedback. We will continue working to improve the documentation.See the reply and handling status in My Cloud VOC.
For any further questions, feel free to contact us through the chatbot.
Chatbot