Clearing Fragments of a Multipart Upload Task
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.
Deleting fragments refers to deleting the parts generated by incomplete multipart uploads. If a multipart upload task is interrupted or fails, unassembled parts are left in the bucket, occupying storage space and incurring fees. You can use the fragment deletion function to delete these unnecessary parts. You are advised to periodically clear multipart upload tasks that have not been completed for more than seven days to avoid unnecessary storage fees.
OBS SDKs provide the following methods for deleting fragments:
- Single deletion: You can call Aborting a Multipart Upload to delete the fragments of a single incomplete multipart upload.
- Batch deletion: You can call Listing Multipart Uploads to list all incomplete multipart uploads and then delete their fragments in batches.
This example shows how to stop incomplete multipart uploads in batches, list all such tasks in a bucket, and cancel them one by one to clear fragments.
<?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 ObsClient instance.
$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';
$concurrency = 5;
// List all multipart uploads using pagination.
// To clear only fragments with a specified prefix, set the Prefix parameter in listMultipartUploads.
$isTruncated = true;
$keyMarker = null;
$uploadIdMarker = null;
$uploads = [];
while ($isTruncated) {
$listResp = $obsClient->listMultipartUploads ( [
'Bucket' => $bucketName,
'KeyMarker' => $keyMarker,
'UploadIdMarker' => $uploadIdMarker
] );
if (empty($listResp['Uploads'])) {
break;
}
foreach ($listResp['Uploads'] as $upload) {
// To clear only fragments generated seven days ago, add the following filter criteria:
// if (strtotime($upload['Initiated']) > time() - 7*86400) continue;
$uploads[] = ['Key' => $upload['Key'], 'UploadId' => $upload['UploadId'], 'Initiated' => $upload['Initiated']];
}
$isTruncated = $listResp['IsTruncated'];
$keyMarker = $listResp['NextKeyMarker'];
$uploadIdMarker = $listResp['NextUploadIdMarker'];
}
printf("Found %d uploads to abort\n\n", count($uploads));
// Stop multipart upload tasks concurrently.
$successCount = 0;
$failCount = 0;
$promiseGenerator = function () use ($obsClient, $bucketName, $uploads, &$successCount, &$failCount) {
foreach ($uploads as $upload) {
printf("Key:%s, UploadId:%s, Initiated:%s\n", $upload['Key'], $upload['UploadId'], $upload['Initiated']);
yield $obsClient->abortMultipartUploadAsync ( [
'Bucket' => $bucketName,
'Key' => $upload['Key'],
'UploadId' => $upload['UploadId']
], function ($exception, $resp) use ($upload, &$successCount, &$failCount) {
if ($exception === null) {
printf("AbortSuccess: Key:%s, RequestId:%s\n", $upload['Key'], $resp['RequestId']);
$successCount++;
} else {
printf("AbortFailed: Key:%s, Status:%d, Message:%s\n", $upload['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