Listing Object Versions
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.
You can call ObsClient->listVersions to list object versions.
The following table describes the parameters involved in this API.
| Parameter | Description |
|---|---|
| Prefix | Name prefix that the objects to be listed must contain |
| KeyMarker | Object name to start with when listing object versions in a bucket. All object versions whose names follow this parameter are listed in the lexicographical order. |
| MaxKeys | Maximum number of listed object versions. The value ranges from 1 to 1000. If the specified value exceeds 1000, only 1,000 object versions are returned. |
| Delimiter | Character used to group object names. If the object name contains the Delimiter parameter, the character string from the first character to the first delimiter in the object name is grouped under a single result element, CommonPrefix. (If a prefix is specified in the request, the prefix must be removed from the object name.) |
| VersionIdMarker | Object name to start with when listing object versions in a bucket. All object versions are listed in the lexicographical order by object name and version ID. This parameter must be used together with KeyMarker. |
- If the value of VersionIdMarker is not a version ID specified by KeyMarker, VersionIdMarker is ineffective.
- The returned result of ObsClient->listVersions includes the object versions and delete markers.
Listing Object Versions in Simple Mode
The following sample code shows how to list object versions in simple mode. A maximum of 1,000 object versions can be returned.
<?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 Obs\ObsException;
// 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'),
'endpoint' => 'https://your-endpoint',
]);
// List object versions.
try {
$resp = $obsClient->listVersions([
'Bucket' => 'bucketname'
]);
printf("RequestId:%s\n", $resp['RequestId']);
// Obtain object versions.
printf("Versions:\n");
foreach ($resp['Versions'] as $index => $version) {
printf("Versions[%d]\n", $index + 1);
printf("Key:%s\n", $version['Key']);
printf("VersionId:%s\n", $version['VersionId']);
printf("IsLatest:%s\n", $version['IsLatest']);
printf("LastModified:%s\n", $version['LastModified']);
printf("ETag:%s\n", $version['ETag']);
printf("Size:%s\n", $version['Size']);
printf("Owner[ID]:%s\n", $version['Owner']['ID']);
printf("StorageClass:%s\n", $version['StorageClass']);
}
// Object delete markers
printf("DeleteMarkers:\n");
foreach ($resp['DeleteMarkers'] as $index => $deleteMarker) {
printf("DeleteMarkers[%d]\n", $index + 1);
printf("Key:%s\n", $deleteMarker['Key']);
printf("VersionId:%s\n", $deleteMarker['VersionId']);
printf("LastModified:%s\n", $deleteMarker['LastModified']);
printf("Owner[ID]:%s\n", $deleteMarker['Owner']['ID']);
}
} catch (ObsException $e) {
// If the operation fails, obtain the exception details.
printf("ListVersionsFailed: Status:%d, Code:%s, Message:%s\n",
$e->getStatusCode(), $e->getExceptionCode(), $e->getExceptionMessage());
} finally {
// Close ObsClient to release resources.
$obsClient->close();
}
- Information about a maximum of 1,000 object versions can be listed each time. If a bucket contains more than 1,000 objects and IsTruncated is true in the returned result, not all object versions are listed. In such cases, you can use NextKeyMarker and NextVersionIdMarker to obtain the start position for next listing.
- If you want to obtain all object versions in a specified bucket, you can use the paging mode for listing objects.
Listing Object Versions by Specifying the Number
Sample code:
<?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 Obs\ObsException;
// 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'),
'endpoint' => 'https://your-endpoint',
]);
// List 100 object versions.
try {
$resp = $obsClient->listVersions([
'Bucket' => 'bucketname',
'MaxKeys' => 100
]);
printf("RequestId:%s\n", $resp['RequestId']);
// Obtain object versions.
printf("Versions:\n");
foreach ($resp['Versions'] as $index => $version) {
printf("Versions[%d]\n", $index + 1);
printf("Key:%s\n", $version['Key']);
printf("VersionId:%s\n", $version['VersionId']);
printf("IsLatest:%s\n", $version['IsLatest']);
printf("LastModified:%s\n", $version['LastModified']);
printf("ETag:%s\n", $version['ETag']);
printf("Size:%s\n", $version['Size']);
printf("Owner[ID]:%s\n", $version['Owner']['ID']);
printf("StorageClass:%s\n", $version['StorageClass']);
}
// Obtain delete markers.
printf("DeleteMarkers:\n");
foreach ($resp['DeleteMarkers'] as $index => $deleteMarker) {
printf("DeleteMarkers[%d]\n", $index + 1);
printf("Key:%s\n", $deleteMarker['Key']);
printf("VersionId:%s\n", $deleteMarker['VersionId']);
printf("LastModified:%s\n", $deleteMarker['LastModified']);
printf("Owner[ID]:%s\n", $deleteMarker['Owner']['ID']);
}
} catch (ObsException $e) {
// If the operation fails, obtain the exception details.
printf("ListVersionsFailed: Status:%d, Code:%s, Message:%s\n",
$e->getStatusCode(), $e->getExceptionCode(), $e->getExceptionMessage());
} finally {
// Close ObsClient to release resources.
$obsClient->close();
}
Listing Object Versions by Specifying a Prefix
Sample code:
<?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 Obs\ObsException;
// 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'),
'endpoint' => 'https://your-endpoint',
]);
// Set the prefix to prefix and the number of object versions to be listed to 100.
try {
$resp = $obsClient->listVersions([
'Bucket' => 'bucketname',
'MaxKeys' => 100,
'Prefix' => 'prefix'
]);
printf("RequestId:%s\n", $resp['RequestId']);
// Obtain object versions.
printf("Versions:\n");
foreach ($resp['Versions'] as $index => $version) {
printf("Versions[%d]\n", $index + 1);
printf("Key:%s\n", $version['Key']);
printf("VersionId:%s\n", $version['VersionId']);
printf("IsLatest:%s\n", $version['IsLatest']);
printf("LastModified:%s\n", $version['LastModified']);
printf("ETag:%s\n", $version['ETag']);
printf("Size:%s\n", $version['Size']);
printf("Owner[ID]:%s\n", $version['Owner']['ID']);
printf("StorageClass:%s\n", $version['StorageClass']);
}
// Object delete markers
printf("DeleteMarkers:\n");
foreach ($resp['DeleteMarkers'] as $index => $deleteMarker) {
printf("DeleteMarkers[%d]\n", $index + 1);
printf("Key:%s\n", $deleteMarker['Key']);
printf("VersionId:%s\n", $deleteMarker['VersionId']);
printf("LastModified:%s\n", $deleteMarker['LastModified']);
printf("Owner[ID]:%s\n", $deleteMarker['Owner']['ID']);
}
} catch (ObsException $e) {
// If the operation fails, obtain the exception details.
printf("ListVersionsFailed: Status:%d, Code:%s, Message:%s\n",
$e->getStatusCode(), $e->getExceptionCode(), $e->getExceptionMessage());
} finally {
// Close ObsClient to release resources.
$obsClient->close();
}
Listing Object Versions by Specifying the Start Position
Sample code:
<?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 Obs\ObsException;
// 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'),
'endpoint' => 'https://your-endpoint',
]);
// Set that 100 object versions whose names follow test in lexicographical order will be listed.
try {
$resp = $obsClient->listVersions([
'Bucket' => 'bucketname',
'MaxKeys' => 100,
'Marker' => 'test'
]);
printf("RequestId:%s\n", $resp['RequestId']);
// Obtain object versions.
printf("Versions:\n");
foreach ($resp['Versions'] as $index => $version) {
printf("Versions[%d]\n", $index + 1);
printf("Key:%s\n", $version['Key']);
printf("VersionId:%s\n", $version['VersionId']);
printf("IsLatest:%s\n", $version['IsLatest']);
printf("LastModified:%s\n", $version['LastModified']);
printf("ETag:%s\n", $version['ETag']);
printf("Size:%s\n", $version['Size']);
printf("Owner[ID]:%s\n", $version['Owner']['ID']);
printf("StorageClass:%s\n", $version['StorageClass']);
}
// Object delete markers
printf("DeleteMarkers:\n");
foreach ($resp['DeleteMarkers'] as $index => $deleteMarker) {
printf("DeleteMarkers[%d]\n", $index + 1);
printf("Key:%s\n", $deleteMarker['Key']);
printf("VersionId:%s\n", $deleteMarker['VersionId']);
printf("LastModified:%s\n", $deleteMarker['LastModified']);
printf("Owner[ID]:%s\n", $deleteMarker['Owner']['ID']);
}
} catch (ObsException $e) {
// If the operation fails, obtain the exception details.
printf("ListVersionsFailed: Status:%d, Code:%s, Message:%s\n",
$e->getStatusCode(), $e->getExceptionCode(), $e->getExceptionMessage());
} finally {
// Close ObsClient to release resources.
$obsClient->close();
}
Listing All Object Versions in Paging Mode
Sample code:
<?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 Obs\ObsException;
// 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'),
'endpoint' => 'https://your-endpoint',
]);
// List object versions.
try {
$keyMarker = null;
$versionIdMarker = null;
do {
$resp = $obsClient->listVersions([
'Bucket' => 'bucketname',
'MaxKeys' => 100,
'Marker' => $keyMarker,
'VersionIdMarker' => $versionIdMarker
]);
printf( "RequestId:%s\n", $resp['RequestId']);
// Obtain object versions.
printf( "Versions:\n" );
foreach ($resp['Versions'] as $index => $version) {
printf( "Versions[%d]\n", $index + 1 );
printf( "Key:%s\n", $version['Key']);
printf( "VersionId:%s\n", $version['VersionId']);
printf( "IsLatest:%s\n", $version['IsLatest']);
printf( "LastModified:%s\n", $version['LastModified']);
printf( "ETag:%s\n", $version['ETag']);
printf( "Size:%s\n", $version['Size']);
printf( "Owner[ID]:%s\n", $version['Owner']['ID']); }
// Obtain delete markers.
printf("DeleteMarkers:\n" );
foreach ($resp['DeleteMarkers'] as $index => $deleteMarker) {
printf("DeleteMarkers[%d]\n", $index + 1 );
printf("Key:%s\n", $deleteMarker['Key']);
printf("VersionId:%s\n", $deleteMarker['VersionId']);
printf("LastModified:%s\n", $deleteMarker['LastMod]);']);
printf("Owner[ID]:%s\n", $deleteMarker['Owner']['ID']);
}
$keyMarker = $resp['NextKeyMarker'];
$versionIdMarker = $resp['NextVersionIdMarker'];
}while($resp['IsTruncated']);
} catch (ObsException $e) {
// If the operation fails, obtain the exception details.
printf("ListVersionsFailed: Status:%d, Code:%s, Message:%s\n",
$e->getStatusCode(), $e->getExceptionCode(), $e->getExceptionMessage());
} finally {
// Close ObsClient to release resources.
$obsClient->close();
}
Listing All Object Versions in a Folder
There is no folder concept in OBS. All elements in buckets are objects. Folders are actually objects whose sizes are 0 and whose names end with a slash (/). When you set a folder name as the prefix, objects in this folder will be listed. Sample code is as follows:
<?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 Obs\ObsException;
// 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'),
'endpoint' => 'https://your-endpoint',
]);
// List all object versions in paging mode.
try {
$keyMarker = null;
$versionIdMarker = null;
do {
$resp = $obsClient->listVersions([
'Bucket' => 'bucketname',
'MaxKeys' => 100,
'Marker' => $keyMarker,
'VersionIdMarker' => $versionIdMarker,
// Set folder name dir/ as the prefix.
'Prefix' => 'dir/'
]);
printf("RequestId:%s\n", $resp['RequestId']);
// Obtain object versions.
printf("Versions:\n");
foreach ($resp['Versions'] as $index => $version) {
printf("Versions[%d]\n", $index + 1);
printf("Key:%s\n", $version['Key']);
printf("VersionId:%s\n", $version['VersionId']);
printf("IsLatest:%s\n", $version['IsLatest']);
printf("LastModified:%s\n", $version['LastModified']);
printf("ETag:%s\n", $version['ETag']);
printf("Size:%s\n", $version['Size']);
printf("Owner[ID]:%s\n", $version['Owner']['ID']);
printf("StorageClass:%s\n", $version['StorageClass']);
}
// Obtain delete markers.
printf("DeleteMarkers:\n");
foreach ($resp['DeleteMarkers'] as $index => $deleteMarker) {
printf("DeleteMarkers[%d]\n", $index + 1);
printf("Key:%s\n", $deleteMarker['Key']);
printf("VersionId:%s\n", $deleteMarker['VersionId']);
printf("LastModified:%s\n", $deleteMarker['LastModified']);
printf("Owner[ID]:%s\n", $deleteMarker['Owner']['ID']);
}
$keyMarker = $resp['NextKeyMarker'];
$versionIdMarker = $resp['NextVersionIdMarker'];
} while ($resp['IsTruncated']);
} catch (ObsException $e) {
// If the operation fails, obtain the exception details.
printf("ListVersionsFailed: Status:%d, Code:%s, Message:%s\n",
$e->getStatusCode(), $e->getExceptionCode(), $e->getExceptionMessage());
} finally {
// Close ObsClient to release resources.
$obsClient->close();
}
Listing All Object Versions According to Folders in a Bucket
Sample code:
<?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 Obs\ObsException;
// 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'),
'endpoint' => 'https://your-endpoint',
]);
// List object versions.
try {
function listVersionsByPrefix($commonPrefixes){
global $obsClient;
foreach ($commonPrefixes as $commonPrefix){
$resp = $obsClient->listVersions([
'Bucket' => 'bucketname',
// Set the folder delimiter to a slash (/).
'Delimiter' => '/',
'Prefix' => $commonPrefix['Prefix']
]);
printf( "Objects in folder [%s]:\n ", $commonPrefix['Prefix']);
// Obtain object versions.
printf( "Versions:\n" );
foreach ($resp['Versions'] as $index => $version) {
printf( "Versions[%d]\n", $index + 1 );
printf( "Key:%s\n", $version['Key']);
printf( "VersionId:%s\n", $version['VersionId']);
}
// Obtain delete markers.
printf( "DeleteMarkers:\n" );
foreach ($resp['DeleteMarkers'] as $index => $deleteMarker) {
printf( "DeleteMarkers[%d]\n", $index + 1 );
printf( "Key:%s\n", $deleteMarker['Key']);
printf( "VersionId:%s\n", $deleteMarker['VersionId']);
}
printf("\n");
listVersionsByPrefix($resp['CommonPrefixes']);
}
}
$resp = $obsClient->listVersions([
'Bucket' => 'bucketname',
// Set the folder delimiter to a slash (/).
'Delimiter' => '/'
]);printf( "Objects in the root directory:\n ");
// Obtain object versions.
printf( "Versions:\n" );
foreach ($resp['Versions'] as $index => $version) {
printf( "Versions[%d]\n", $index + 1 );
printf( "Key:%s\n", $version['Key']);
printf( "VersionId:%s\n", $version['VersionId']);
}
// Obtain delete markers.
printf( "DeleteMarkers:\n" );
foreach ($resp['DeleteMarkers'] as $index => $deleteMarker) {
printf( "DeleteMarkers[%d]\n", $index + 1 );
printf( "Key:%s\n", $deleteMarker['Key']);
printf( "VersionId:%s\n", $deleteMarker['VersionId']);
}
listVersionsByPrefix($resp['CommonPrefixes']);
} catch (ObsException $e) {
// If the operation fails, obtain the exception details.
printf("ListVersionsFailed: Status:%d, Code:%s, Message:%s\n",
$e->getStatusCode(), $e->getExceptionCode(), $e->getExceptionMessage());
} finally {
// Close ObsClient to release resources.
$obsClient->close();
}
- The previous sample code does not include scenarios where the number of objects in a folder exceeds 1000.
- Because objects and sub-folders in a folder are to be listed and all the objects end with a slash (/), Delimiter is always a slash (/).
- In the returned result of each recursion, Versions includes the object versions in the folder, DeleteMarkers includes the delete markers in the folder, and CommonPrefixes includes the sub-folders in the folder.
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