• Object Storage Service

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

Listing Objects

You can call ObsClient.listObjects to list objects in a bucket.

The following table describes the parameters involved in this API.

Parameter

Description

Prefix

Prefix that the object names to be listed must contain

Marker

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

MaxKeys

Maximum number of objects listed in the response body. The value ranges from 1 to 1000. If the value is not in this range, 1000 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.)

Listing Objects in Simple Mode

The following sample code shows how to list objects in simple mode. A maximum of 1000 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.listObjects({
       Bucket : 'bucketname'
}, (err, result) => {
       if(err){              
          console.error('Error-->' + err);       
       }else{              
          console.log('Status-->' + result.CommonMsg.Status);
                 if(result.CommonMsg.Status < 300 && result.InterfaceResult){
                        for(let j=0;j<result.InterfaceResult.Contents.length;j++){
                                  console.log('Contents[' + j +  ']:');
                                  console.log('Key-->' + result.InterfaceResult.Contents[j]['Key']);
                                  console.log('Owner[ID]-->' + result.InterfaceResult.Contents[j]['Owner']['ID']);
                                  console.log('Owner[Name]-->' + result.InterfaceResult.Contents[j]['Owner']['Name']);
                           }
                 }            
       }
});
NOTE:
  • A maximum of 1000 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 objects are returned. In such cases, you can use InterfaceResult.NextMarker to obtain the start position for next listing.
  • If you want to obtain all objects in a specified bucket, you can use the paging mode for listing objects.

Listing 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.listObjects({
       Bucket : 'bucketname',
// Set the number of objects to be listed to 100.
       MaxKeys : 100
}, (err, result) => {
       if(err){              
          console.error('Error-->' + err);       
       }else{              
          console.log('Status-->' + result.CommonMsg.Status);
                 if(result.CommonMsg.Status < 300 && result.InterfaceResult){
                        for(let j=0;j<result.InterfaceResult.Contents.length;j++){
                                  console.log('Contents[' + j +  ']:');
                                  console.log('Key-->' + result.InterfaceResult.Contents[j]['Key']);
                                  console.log('Owner[ID]-->' + result.InterfaceResult.Contents[j]['Owner']['ID']);
                                  console.log('Owner[Name]-->' + result.InterfaceResult.Contents[j]['Owner']['Name']);
                            }
                 }            
       }
});

Listing 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'
});

obsClient.listObjects({
       Bucket : 'bucketname',
// Set the number to 100 and the prefix to prefix.
       MaxKeys : 100,
       Prefix : 'prefix'
}, (err, result) => {
       if(err){              
          console.error('Error-->' + err);       
       }else{              
          console.log('Status-->' + result.CommonMsg.Status);
                 if(result.CommonMsg.Status < 300 && result.InterfaceResult){
                        for(let j=0;j<result.InterfaceResult.Contents.length;j++){
                                  console.log('Contents[' + j +  ']:');
                                  console.log('Key-->' + result.InterfaceResult.Contents[j]['Key']);
                                  console.log('Owner[ID]-->' + result.InterfaceResult.Contents[j]['Owner']['ID']);
                                  console.log('Owner[Name]-->' + result.InterfaceResult.Contents[j]['Owner']['Name']);
                           }
                 }            
       }
});

Listing 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'
});

obsClient.listObjects({
       Bucket : 'bucketname',
// List 100 objects following test in lexicographic order.
       MaxKeys : 100,
       Marker : 'test'
}, (err, result) => {
       if(err){              
          console.error('Error-->' + err);       
       }else{              
          console.log('Status-->' + result.CommonMsg.Status);
                 if(result.CommonMsg.Status < 300 && result.InterfaceResult){
                        for(let j=0;j<result.InterfaceResult.Contents.length;j++){
                                  console.log('Contents[' + j +  ']:');
                                  console.log('Key-->' + result.InterfaceResult.Contents[j]['Key']);
                                   console.log('Owner[ID]-->' + result.InterfaceResult.Contents[j]['Owner']['ID']);
                                  console.log('Owner[Name]-->' + result.InterfaceResult.Contents[j]['Owner']['Name']);
                           }
                 }            
       }
});

Listing All 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 = (marker) => {
       obsClient.listObjects({
              Bucket : 'bucketname',
// Set the number of objects displayed per page to 100.
              MaxKeys : 100,
              Marker : marker
       }, (err, result) => {
              if(err){              
                     console.error('Error-->' + err);       
              }else{              
                     console.log('Status-->' + result.CommonMsg.Status);
                     if(result.CommonMsg.Status < 300 && result.InterfaceResult){
                           for(let j=0;j<result.InterfaceResult.Contents.length;j++){
                                  console.log('Contents[' + j +  ']:');
                                  console.log('Key-->' + result.InterfaceResult.Contents[j]['Key']);
                                  console.log('Owner[ID]-->' + result.InterfaceResult.Contents[j]['Owner']['ID']);
                                  console.log('Owner[Name]-->' + result.InterfaceResult.Contents[j]['Owner']['Name']);
                           }
                     if(result.InterfaceResult.IsTruncated === 'true'){
                                  listAll(result.InterfaceResult.NextMarker);
                           }
                     } 
              }
       });
};

listAll();

Listing All 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 = (marker) => {
       obsClient.listObjects({
              Bucket : 'bucketname',
              MaxKeys : 1000,
// Set the prefix of objects in the folder to dir/.
              Prefix : 'dir/'
       }, (err, result) => {
              if(err){              
                     console.error('Error-->' + err);       
              }else{              
                     console.log('Status-->' + result.CommonMsg.Status);
                     if(result.CommonMsg.Status < 300 && result.InterfaceResult){
                           for(let j=0;j<result.InterfaceResult.Contents.length;j++){
                                  console.log('Contents[' + j +  ']:');
                                  console.log('Key-->' + result.InterfaceResult.Contents[j]['Key']);
                                  console.log('Owner[ID]-->' + result.InterfaceResult.Contents[j]['Owner']['ID']);
                                  console.log('Owner[Name]-->' + result.InterfaceResult.Contents[j]['Owner']['Name']);
                           }
                     if(result.InterfaceResult.IsTruncated === 'true'){
                                  listAll(result.InterfaceResult.NextMarker);
                           }
                     } 
              }
       });
};

listAll();

Listing All 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.listObjects({
       Bucket: 'bucketname',
// Set folder isolators to slashes.
       Delimiter: '/'
}, (err, result) => {
       if(!err && result.CommonMsg.Status < 300){
              console.log('Objects in the root directory:');
              for(let j=0;j<result.InterfaceResult.Contents.length;j++){
                     console.log('\tKey-->' + result.InterfaceResult.Contents[j]['Key']);
                     console.log('Owner[ID]-->' + result.InterfaceResult.Contents[j]['Owner']['ID']);                                  
                     console.log('Owner[Name]-->' + result.InterfaceResult.Contents[j]['Owner']['Name']);
              }
              var listObjectsByPrefix = (commonPrefixes) => {
                     for(let i=0;i<commonPrefixes.length;i++){
                           obsClient.listObjects({
                                  Bucket: 'bucketname',
                                  Delimiter: '/',
                                  Prefix: commonPrefixes[i]['Prefix']
                           }, (err, result)=>{
                                  if(!err && result.CommonMsg.Status < 300){
                                         console.log('Objects in folder [' + commonPrefixes[i]['Prefix'] + ']:');
                                         for(let j=0;j<result.InterfaceResult.Contents.length;j++){
                                                console.log('\tKey-->' + result.InterfaceResult.Contents[j]['Key']);
                                                console.log('Owner[ID]-->' + result.InterfaceResult.Contents[j]['Owner']['ID']);                                  
                                                console.log('Owner[Name]-->' + result.InterfaceResult.Contents[j]['Owner']['Name']);
                                         }
                                         console.log('\n');
                                         if(result.InterfaceResult.CommonPrefixes && result.InterfaceResult.CommonPrefixes.length > 0){
                                                listObjectsByPrefix(result.InterfaceResult.CommonPrefixes);
                                         }
                                  }
                           });
                     }
              };
              listObjectsByPrefix(result.InterfaceResult.CommonPrefixes);
       }
});
NOTE:
  • The sample code does not apply to 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.Contents includes the objects in the folder and InterfaceResult.CommonPrefixes includes the sub-folders in the folder.