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.
Batch upload is based on the following three single upload methods. You can select a proper upload method based on the file type and size and use the sample code below to implement batch upload.
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 source code installation.
// require 'obs-autoloader.php';
// Declare the namespace.
use Obs\ObsClient;
// Create an instance of ObsClient.
$obsClient = new ObsClient ( [
// Obtain an AK/SK pair using environment variables or import the 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 {
printf ( "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); Feedback
Was this page helpful?
Provide feedbackThank 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