Listing Versioning Objects
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 the API Reference.
You can call ObsClient.listVersions to list versioning objects.
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 versioning objects in a bucket. All versioning objects whose names follow this parameter are listed in the lexicographical order. |
MaxKeys |
Maximum number of listed versioning objects. The value ranges from 1 to 1000. If the specified value exceeds 1000, only 1,000 versioning objects 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 versioning objects in a bucket. All versioning objects 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 versioning objects and delete markers.
Listing Versioning Objects in Simple Mode
The following sample code shows how to list versioning objects in simple mode. A maximum of 1,000 versioning objects can be returned.
// Create an instance of ObsClient. var obsClient = new ObsClient({ // Hard-coded or plaintext AK/SK are risky. For security purposes, encrypt your AK/SK and store them in the configuration file or environment variables. In this example, the AK/SK are stored in environment variables for identity authentication. Before running this example, configure environment variables AccessKeyID and SecretAccessKey. // The front-end code does not have the process environment variable, so you need to use a module bundler like webpack to define the process variable. // 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. access_key_id: process.env.AccessKeyID, secret_access_key: process.env.SecretAccessKey, // Replace the example endpoint with the actual one in your case. server: 'https://obs.ap-southeast-1.myhuaweicloud.com' }); obsClient.listVersions({ Bucket : 'bucketname' }, function (err, result) { if(err){ console.log('Error-->' + err); }else{ console.log('Status-->' + result.CommonMsg.Status); if(result.CommonMsg.Status < 300 && result.InterfaceResult){ // Obtain versioning objects. for(var j in result.InterfaceResult.Versions){ console.log('Version[' + j + ']:'); console.log('Key-->' + result.InterfaceResult.Versions[j]['Key']); console.log('VersionId-->' + result.InterfaceResult.Versions[j]['VersionId']); console.log('Owner[ID]-->' + result.InterfaceResult.Versions[j]['Owner']['ID']); } // Obtain delete markers. for(var i in result.InterfaceResult.DeleteMarkers){ console.log('DeleteMarker[' + i + ']:'); console.log('Key-->' + result.InterfaceResult.DeleteMarkers[i]['Key']); console.log('VersionId-->' + result.InterfaceResult.DeleteMarkers[i]['VersionId']); console.log('Owner[ID]-->' + result.InterfaceResult.DeleteMarkers[i]['Owner']['ID']); } } } });
- A maximum of 1,000 object versions can be listed each time. If a bucket contains more than 1,000 object versions, InterfaceResult.IsTruncated in the response is true, indicating not all object versions were listed. In such case, you can use InterfaceResult.NextKeyMarker and InterfaceResult.NextVersionIdMarker to obtain the start position for the next listing.
- If you want to obtain all versioning objects in a specified bucket, you can use the paging mode for listing objects.
Listing Versioning Objects by Specifying the Number
Sample code:
// Create an instance of ObsClient. var obsClient = new ObsClient({ // Hard-coded or plaintext AK/SK are risky. For security purposes, encrypt your AK/SK and store them in the configuration file or environment variables. In this example, the AK/SK are stored in environment variables for identity authentication. Before running this example, configure environment variables AccessKeyID and SecretAccessKey. // The front-end code does not have the process environment variable, so you need to use a module bundler like webpack to define the process variable. // 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. access_key_id: process.env.AccessKeyID, secret_access_key: process.env.SecretAccessKey, // Replace the example endpoint with the actual one in your case. server: 'https://obs.ap-southeast-1.myhuaweicloud.com' }); obsClient.listVersions({ Bucket : 'bucketname', MaxKeys : 100 }, function (err, result) { if(err){ console.log('Error-->' + err); }else{ console.log('Status-->' + result.CommonMsg.Status); if(result.CommonMsg.Status < 300 && result.InterfaceResult){ // Obtain versioning objects. for(var j in result.InterfaceResult.Versions){ console.log('Version[' + j + ']:'); console.log('Key-->' + result.InterfaceResult.Versions[j]['Key']); console.log('VersionId-->' + result.InterfaceResult.Versions[j]['VersionId']); console.log('Owner[ID]-->' + result.InterfaceResult.Versions[j]['Owner']['ID']); } // Obtain delete markers. for(var i in result.InterfaceResult.DeleteMarkers){ console.log('DeleteMarker[' + i + ']:'); console.log('Key-->' + result.InterfaceResult.DeleteMarkers[i]['Key']); console.log('VersionId-->' + result.InterfaceResult.DeleteMarkers[i]['VersionId']); console.log('Owner[ID]-->' + result.InterfaceResult.DeleteMarkers[i]['Owner']['ID']); } } } });
Listing Versioning Objects by Specifying a Prefix
Sample code:
// Create an instance of ObsClient. var obsClient = new ObsClient({ // Hard-coded or plaintext AK/SK are risky. For security purposes, encrypt your AK/SK and store them in the configuration file or environment variables. In this example, the AK/SK are stored in environment variables for identity authentication. Before running this example, configure environment variables AccessKeyID and SecretAccessKey. // The front-end code does not have the process environment variable, so you need to use a module bundler like webpack to define the process variable. // 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. access_key_id: process.env.AccessKeyID, secret_access_key: process.env.SecretAccessKey, // Replace the example endpoint with the actual one in your case. server: 'https://obs.ap-southeast-1.myhuaweicloud.com' }); // Set the prefix to prefix and the number to 100. obsClient.listVersions({ Bucket : 'bucketname', MaxKeys : 100, Prefix : 'prefix' }, function (err, result) { if(err){ console.log('Error-->' + err); }else{ console.log('Status-->' + result.CommonMsg.Status); if(result.CommonMsg.Status < 300 && result.InterfaceResult){ // Obtain versioning objects. for(var j in result.InterfaceResult.Versions){ console.log('Version[' + j + ']:'); console.log('Key-->' + result.InterfaceResult.Versions[j]['Key']); console.log('VersionId-->' + result.InterfaceResult.Versions[j]['VersionId']); console.log('Owner[ID]-->' + result.InterfaceResult.Versions[j]['Owner']['ID']); } // Obtain delete markers. for(var i in result.InterfaceResult.DeleteMarkers){ console.log('DeleteMarker[' + i + ']:'); console.log('Key-->' + result.InterfaceResult.DeleteMarkers[i]['Key']); console.log('VersionId-->' + result.InterfaceResult.DeleteMarkers[i]['VersionId']); console.log('Owner[ID]-->' + result.InterfaceResult.DeleteMarkers[i]['Owner']['ID']); } } } });
Listing Versioning Objects by Specifying the Start Position
Sample code:
// Create an instance of ObsClient. var obsClient = new ObsClient({ // Hard-coded or plaintext AK/SK are risky. For security purposes, encrypt your AK/SK and store them in the configuration file or environment variables. In this example, the AK/SK are stored in environment variables for identity authentication. Before running this example, configure environment variables AccessKeyID and SecretAccessKey. // The front-end code does not have the process environment variable, so you need to use a module bundler like webpack to define the process variable. // 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. access_key_id: process.env.AccessKeyID, secret_access_key: process.env.SecretAccessKey, // Replace the example endpoint with the actual one in your case. server: 'https://obs.ap-southeast-1.myhuaweicloud.com' }); // List 100 versioning objects whose names are following test in the lexicographical order. obsClient.listVersions({ Bucket : 'bucketname', MaxKeys : 100, KeyMarker : 'test' }, function (err, result) { if(err){ console.log('Error-->' + err); }else{ console.log('Status-->' + result.CommonMsg.Status); if(result.CommonMsg.Status < 300 && result.InterfaceResult){ // Obtain versioning objects. for(var j in result.InterfaceResult.Versions){ console.log('Version[' + j + ']:'); console.log('Key-->' + result.InterfaceResult.Versions[j]['Key']); console.log('VersionId-->' + result.InterfaceResult.Versions[j]['VersionId']); console.log('Owner[ID]-->' + result.InterfaceResult.Versions[j]['Owner']['ID']); } // Obtain delete markers. for(var i in result.InterfaceResult.DeleteMarkers){ console.log('DeleteMarker[' + i + ']:'); console.log('Key-->' + result.InterfaceResult.DeleteMarkers[i]['Key']); console.log('VersionId-->' + result.InterfaceResult.DeleteMarkers[i]['VersionId']); console.log('Owner[ID]-->' + result.InterfaceResult.DeleteMarkers[i]['Owner']['ID']); } } } });
Listing All Versioning Objects in Paging Mode
Sample code:
// Create an instance of ObsClient. var obsClient = new ObsClient({ // Hard-coded or plaintext AK/SK are risky. For security purposes, encrypt your AK/SK and store them in the configuration file or environment variables. In this example, the AK/SK are stored in environment variables for identity authentication. Before running this example, configure environment variables AccessKeyID and SecretAccessKey. // The front-end code does not have the process environment variable, so you need to use a module bundler like webpack to define the process variable. // 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. access_key_id: process.env.AccessKeyID, secret_access_key: process.env.SecretAccessKey, // Replace the example endpoint with the actual one in your case. server: 'https://obs.ap-southeast-1.myhuaweicloud.com' }); var listAll = function (keyMarker, versionIdMarker) { obsClient.listVersions({ Bucket : 'bucketname', MaxKeys : 100, KeyMarker : keyMarker, VersionIdMarker : versionIdMarker }, function (err, result) { if(err){ console.log('Error-->' + err); }else{ console.log('Status-->' + result.CommonMsg.Status); if(result.CommonMsg.Status < 300 && result.InterfaceResult){ // Obtain versioning objects. for(var j in result.InterfaceResult.Versions){ console.log('Version[' + j + ']:'); console.log('Key-->' + result.InterfaceResult.Versions[j]['Key']); console.log('VersionId-->' + result.InterfaceResult.Versions[j]['VersionId']); console.log('Owner[ID]-->' + result.InterfaceResult.Versions[j]['Owner']['ID']); } // Obtain delete markers. for(var i in result.InterfaceResult.DeleteMarkers){ console.log('DeleteMarker[' + i + ']:'); console.log('Key-->' + result.InterfaceResult.DeleteMarkers[i]['Key']); console.log('VersionId-->' + result.InterfaceResult.DeleteMarkers[i]['VersionId']); console.log('Owner[ID]-->' + result.InterfaceResult.DeleteMarkers[i]['Owner']['ID']); } if(result.InterfaceResult.IsTruncated === 'true'){ listAll(result.InterfaceResult.NextKeyMarker, result.InterfaceResult.NextVersionIdMarker); } } } }); }; listAll();
Listing All Versioning Objects 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:
// Create an instance of ObsClient. var obsClient = new ObsClient({ // Hard-coded or plaintext AK/SK are risky. For security purposes, encrypt your AK/SK and store them in the configuration file or environment variables. In this example, the AK/SK are stored in environment variables for identity authentication. Before running this example, configure environment variables AccessKeyID and SecretAccessKey. // The front-end code does not have the process environment variable, so you need to use a module bundler like webpack to define the process variable. // 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. access_key_id: process.env.AccessKeyID, secret_access_key: process.env.SecretAccessKey, // Replace the example endpoint with the actual one in your case. server: 'https://obs.ap-southeast-1.myhuaweicloud.com' }); var listAll = function (keyMarker, versionIdMarker) { obsClient.listVersions({ Bucket : 'bucketname', MaxKeys : 100, KeyMarker : keyMarker, VersionIdMarker : versionIdMarker, // Set the prefix of the folders to dir/. Prefix : 'dir/' }, function (err, result) { if(err){ console.log('Error-->' + err); }else{ console.log('Status-->' + result.CommonMsg.Status); if(result.CommonMsg.Status < 300 && result.InterfaceResult){ // Obtain versioning objects. for(var j in result.InterfaceResult.Versions){ console.log('Version[' + j + ']:'); console.log('Key-->' + result.InterfaceResult.Versions[j]['Key']); console.log('VersionId-->' + result.InterfaceResult.Versions[j]['VersionId']); console.log('Owner[ID]-->' + result.InterfaceResult.Versions[j]['Owner']['ID']); } // Obtain delete markers. for(var i in result.InterfaceResult.DeleteMarkers){ console.log('DeleteMarker[' + i + ']:'); console.log('Key-->' + result.InterfaceResult.DeleteMarkers[i]['Key']); console.log('VersionId-->' + result.InterfaceResult.DeleteMarkers[i]['VersionId']); console.log('Owner[ID]-->' + result.InterfaceResult.DeleteMarkers[i]['Owner']['ID']); } if(result.InterfaceResult.IsTruncated === 'true'){ listAll(result.InterfaceResult.NextKeyMarker, result.InterfaceResult.NextVersionIdMarker); } } } }); }; listAll();
Listing All Versioning Objects According to Folders in a Bucket
Sample code:
// Create an instance of ObsClient. var obsClient = new ObsClient({ // Hard-coded or plaintext AK/SK are risky. For security purposes, encrypt your AK/SK and store them in the configuration file or environment variables. In this example, the AK/SK are stored in environment variables for identity authentication. Before running this example, configure environment variables AccessKeyID and SecretAccessKey. // The front-end code does not have the process environment variable, so you need to use a module bundler like webpack to define the process variable. // 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. access_key_id: process.env.AccessKeyID, secret_access_key: process.env.SecretAccessKey, // Replace the example endpoint with the actual one in your case. server: 'https://obs.ap-southeast-1.myhuaweicloud.com' }); obsClient.listVersions({ Bucket : 'bucketname', // Set folder isolators to slashes (/). Delimiter : '/' }, function (err, result) { if(err){ console.log('Error-->' + err); }else{ console.log('Objects in the root directory:'); if(result.CommonMsg.Status < 300 && result.InterfaceResult){ // Obtain versioning objects. for(var j in result.InterfaceResult.Versions){ console.log('Version[' + j + ']:'); console.log('Key-->' + result.InterfaceResult.Versions[j]['Key']); console.log('VersionId-->' + result.InterfaceResult.Versions[j]['VersionId']); console.log('Owner[ID]-->' + result.InterfaceResult.Versions[j]['Owner']['ID']); } // Obtain delete markers. for(var i in result.InterfaceResult.DeleteMarkers){ console.log('DeleteMarker[' + i + ']:'); console.log('Key-->' + result.InterfaceResult.DeleteMarkers[i]['Key']); console.log('VersionId-->'+ result.InterfaceResult.DeleteMarkers[i]['VersionId']); console.log('Owner[ID]-->' + result.InterfaceResult.DeleteMarkers[i]['Owner']['ID']); } } var listVersionsByPrefix = function (commonPrefixes) { for(var n in commonPrefixes){ obsClient.listVersions({ Bucket : 'bucketname', Delimiter : '/', Prefix: commonPrefixes[n]['Prefix'] }, function (err, result) { console.log('Objects in folder:'); if(result.CommonMsg.Status < 300 && result.InterfaceResult){ // Obtain versioning objects. for(var j in result.InterfaceResult.Versions){ console.log('Version[' + j + ']:'); console.log('Key-->' + result.InterfaceResult.Versions[j]['Key']); console.log('VersionId-->' + result.InterfaceResult.Versions[j]['VersionId']); console.log('Owner[ID]-->' + result.InterfaceResult.Versions[j]['Owner']['ID']); } // Obtain delete markers. for(var i in result.InterfaceResult.DeleteMarkers){ console.log('DeleteMarker[' + i + ']:'); console.log('Key-->' + result.InterfaceResult.DeleteMarkers[i]['Key']); console.log('VersionId-->' + result.InterfaceResult.DeleteMarkers[i]['VersionId']); console.log('Owner[ID]-->' + result.InterfaceResult.DeleteMarkers[i]['Owner']['ID']); } console.log('\n'); if(result.InterfaceResult.CommonPrefixes && result.InterfaceResult.CommonPrefixes.length > 0){ listVersionsByPrefix(result.InterfaceResult.CommonPrefixes); } } }); } }; listVersionsByPrefix(result.InterfaceResult.CommonPrefixes); } });
- The previous sample code does not include scenarios where the number of versioning 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, InterfaceResult.Versions includes the versioning objects in the folder, InterfaceResult.DeleteMarkers includes the delete markers in the folder, and InterfaceResult.CommonPrefixes includes the sub-folders in the folder.
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