• Object Storage Service

obs
  1. Help Center
  2. Object Storage Service
  3. Developer Guide (Node.js SDK)
  4. Versioning Management
  5. Listing Versioning Objects

Listing Versioning Objects

You can call ObsClient.listVersions to list versioning objects.

The following table describes the parameters involved in this API.

Parameter

Description

Prefix

Prefix that the object names to be listed must contain.

KeyMarker

Versioning object name to start with when listing versioning objects in a bucket. All versioning objects are listed in the lexicographical order.

MaxKeys

Maximum number of versioning object names listed in the response body. The value ranges from 1 to 1000. If the value is not in this range, 1000 versioning objects are returned by default.

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 parameter 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

All versioning objects are listed in the lexicographical order by object name and version ID. This parameter must be used together with KeyMarker.

NOTE:
  • 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 1000 versioning objects can be returned.

// Import the OBS library.
var ObsClient = require('./lib/obs'); 
// Create an instance of ObsClient.
var obsClient = new ObsClient({ 
    access_key_id: '*** Provide your Access Key ***',        
    secret_access_key: '*** Provide your Secret Key ***',        
    server : 'yourdomainname' 
}); 
obsClient.listVersions({ 
       Bucket : 'bucketname' 
}, (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(let j=0;j<result.InterfaceResult.Versions.length;j++){ 
                            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']); 
                            console.log('Owner[Name]-->' + result.InterfaceResult.Versions[j]['Owner']['Name']); 
                     } 
                      // Obtain delete markers.
                     for(let i=0;i<result.InterfaceResult.DeleteMarkers.length;i++){ 
                            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('Owner[Name]-->' + result.InterfaceResult.DeleteMarkers[i]['Owner']['Name']); 
                     } 
              }
       } 
});
NOTE:
  • A maximum of 1000 versioning objects can be returned each time. If a bucket contains more than 1000 objects and InterfaceResult.IsTruncated is true in the returned result, not all versioning objects are returned. In such cases, you can use InterfaceResult.NextKeyMarker and InterfaceResult.NextVersionIdMarker to obtain the start position for 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:

// Import the OBS library.
var ObsClient = require('./lib/obs');

// Create an instance of ObsClient.
var obsClient = new ObsClient({
    access_key_id: '*** Provide your Access Key ***',       
    secret_access_key: '*** Provide your Secret Key ***',       
    server : 'yourdomainname'
});

obsClient.listVersions({
       Bucket : 'bucketname',
       MaxKeys : 100
}, (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(let j=0;j<result.InterfaceResult.Versions.length;j++){ 
                            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']); 
                            console.log('Owner[Name]-->' + result.InterfaceResult.Versions[j]['Owner']['Name']); 
                     } 
                      // Obtain delete markers.
                     for(let i=0;i<result.InterfaceResult.DeleteMarkers.length;i++){ 
                            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('Owner[Name]-->' + result.InterfaceResult.DeleteMarkers[i]['Owner']['Name']); 
                     } 
              }
       }
});

Listing Versioning Objects by Specifying a Prefix

Sample code:

// Import the OBS library.
var ObsClient = require('./lib/obs');

// Create an instance of ObsClient.
var obsClient = new ObsClient({
    access_key_id: '*** Provide your Access Key ***',       
    secret_access_key: '*** Provide your Secret Key ***',       
    server : 'yourdomainname'
});

// Set the prefix to prefix and the number to 100.
obsClient.listVersions({
       Bucket : 'bucketname',
       MaxKeys : 100,
       Prefix : 'prefix'
}, (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(let j=0;j<result.InterfaceResult.Versions.length;j++){ 
                            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']); 
                            console.log('Owner[Name]-->' + result.InterfaceResult.Versions[j]['Owner']['Name']); 
                     } 
                     // Obtain delete markers.
                     for(let i=0;i<result.InterfaceResult.DeleteMarkers.length;i++){ 
                            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('Owner[Name]-->' + result.InterfaceResult.DeleteMarkers[i]['Owner']['Name']); 
                     } 
              }
       }
});

Listing Versioning Objects by Specifying the Start Position

Sample code:

// Import the OBS library.
var ObsClient = require('./lib/obs');

// Create an instance of ObsClient.
var obsClient = new ObsClient({
    access_key_id: '*** Provide your Access Key ***',       
    secret_access_key: '*** Provide your Secret Key ***',       
    server : 'yourdomainname'
});

// List 100 versioning objects whose names following test in lexicographic order.
obsClient.listVersions({
       Bucket : 'bucketname',
       MaxKeys : 100,
       KeyMarker : 'test'
}, (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(let j=0;j<result.InterfaceResult.Versions.length;j++){ 
                            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']); 
                            console.log('Owner[Name]-->' + result.InterfaceResult.Versions[j]['Owner']['Name']); 
                     } 
                     // Obtain delete markers.
                     for(let i=0;i<result.InterfaceResult.DeleteMarkers.length;i++){ 
                            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('Owner[Name]-->' + result.InterfaceResult.DeleteMarkers[i]['Owner']['Name']); 
                     } 
              }
       }
});

Listing All Versioning Objects in Paging Mode

Sample code:

// Import the OBS library.
var ObsClient = require('./lib/obs'); 
// Create an instance of ObsClient.
var obsClient = new ObsClient({ 
    access_key_id: '*** Provide your Access Key ***',        
    secret_access_key: '*** Provide your Secret Key ***',        
    server : 'yourdomainname' 
}); 
var listAll = (keyMarker, versionIdMarker) => { 
       obsClient.listVersions({ 
              Bucket : 'bucketname', 
              MaxKeys : 100, 
              KeyMarker : keyMarker, 
              VersionIdMarker : versionIdMarker 
       }, (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(let j=0;j<result.InterfaceResult.Versions.length;j++){ 
                                  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']); 
                                   console.log('Owner[Name]-->' + result.InterfaceResult.Versions[j]['Owner']['Name']); 
                            } 
                            // Obtain delete markers.
                            for(let i=0;i<result.InterfaceResult.DeleteMarkers.length;i++){ 
                                  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('Owner[Name]-->' + result.InterfaceResult.DeleteMarkers[i]['Owner']['Name']); 
                            } 
                            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:

// Import the OBS library.
var ObsClient = require('./lib/obs');

// Create an instance of ObsClient.
var obsClient = new ObsClient({
    access_key_id: '*** Provide your Access Key ***',       
    secret_access_key: '*** Provide your Secret Key ***',       
    server : 'yourdomainname'
});

var listAll = (keyMarker, versionIdMarker) => {
       obsClient.listVersions({
              Bucket : 'bucketname',
              MaxKeys : 100,
              KeyMarker : keyMarker,
              VersionIdMarker : versionIdMarker,
              // Set the prefix of objects in the folder to dir/.
              Prefix : 'dir/'
       }, (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(let j=0;j<result.InterfaceResult.Versions.length;j++){ 
                                  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']); 
                                   console.log('Owner[Name]-->' + result.InterfaceResult.Versions[j]['Owner']['Name']); 
                            } 
                            // Obtain delete markers.
                            for(let i=0;i<result.InterfaceResult.DeleteMarkers.length;i++){ 
                                  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('Owner[Name]-->' + result.InterfaceResult.DeleteMarkers[i]['Owner']['Name']); 
                            } 
                            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:

// Import the OBS library.
var ObsClient = require('./lib/obs'); 
// Create an instance of ObsClient.
var obsClient = new ObsClient({ 
    access_key_id: '*** Provide your Access Key ***',        
    secret_access_key: '*** Provide your Secret Key ***',        
    server : 'yourdomainname' 
}); 
obsClient.listVersions({ 
       Bucket : 'bucketname', 
       // Set the folder isolators to slashes.
       Delimiter : '/' 
}, (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(let j=0;j<result.InterfaceResult.Versions.length;j++){ 
                         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']); 
                         console.log('Owner[Name]-->' + result.InterfaceResult.Versions[j]['Owner']['Name']); 
                  } 
// Obtain delete markers.
                  for(let i=0;i<result.InterfaceResult.DeleteMarkers.length;i++){ 
                         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('Owner[Name]-->' + result.InterfaceResult.DeleteMarkers[i]['Owner']['Name']); 
                  } 
              }
              var listVersionsByPrefix = (commonPrefixes) => { 
                     for(let n=0;n<commonPrefixes.length;n++){ 
                           obsClient.listVersions({ 
                                  Bucket : 'bucketname', 
                                  Delimiter : '/', 
                                  Prefix: commonPrefixes[n]['Prefix'] 
                           }, (err, result) => { 
                                  console.log('Objects in folder [' + commonPrefixes[n]['Prefix'] + ']:'); 
                                  if(result.CommonMsg.Status < 300 && result.InterfaceResult){
                                    // Obtain versioning objects.
                                    for(let j=0;j<result.InterfaceResult.Versions.length;j++){ 
                                           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']); 
                                           console.log('Owner[Name]-->' + result.InterfaceResult.Versions[j]['Owner']['Name']); 
                                    } 
                                    // Obtain delete markers.
                                    for(let i=0;i<result.InterfaceResult.DeleteMarkers.length;i++){ 
                                           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('Owner[Name]-->' + result.InterfaceResult.DeleteMarkers[i]['Owner']['Name']); 
                                    } 
                                    console.log('\n'); 
                                    if(result.InterfaceResult.CommonPrefixes && result.InterfaceResult.CommonPrefixes.length > 0){ 
                                           listVersionsByPrefix(result.InterfaceResult.CommonPrefixes); 
                                    } 
                                  }
                           }); 
                     } 
              }; 
              listVersionsByPrefix(result.InterfaceResult.CommonPrefixes); 
       } 
});
NOTE:
  • 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, InterfaceResult.Versions includes the versioning objects in the folder, InterfaceResult.DeleteMarkers includes the delete markers involved in the folder, and InterfaceResult.CommonPrefixes includes the sub-folders in the folder.