Updated on 2024-05-09 GMT+08:00

Listing 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.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. Objects are listed in the lexicographical order.

MaxKeys

Maximum number of objects to be listed in the response body. The value ranges from 1 to 1000. If the specified value exceeds 1000, only 1,000 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 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.)

For a parallel file system, if this parameter is not specified, all the content in the directory is recursively listed by default, and subdirectories are also listed. In big data scenarios, parallel file systems usually have deep directory levels and each directory has a large number of files. In such case, you are advised to configure [delimiter='/'] to list the content in the current directory, but not list subdirectories, thereby improving the listing efficiency.

Listing Objects in Simple Mode

The following sample code shows how to list objects in simple mode. A maximum of 1,000 objects can be returned.

// Import the OBS library.
// Use npm to install the client.
var ObsClient = require('esdk-obs-nodejs');
// Use the source code to install the client.
// var ObsClient = require('./lib/obs');

// Create an instance of ObsClient.
var 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.
       access_key_id: process.env.ACCESS_KEY_ID,
       secret_access_key: process.env.SECRET_ACCESS_KEY,
       server : 'https://your-endpoint'
});

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']);
                           }
                 }            
       }
});
  • A maximum of 1,000 objects can be listed each time. If a bucket contains more than 1,000 objects, InterfaceResult.IsTruncated in the response is true, indicating not all objects were listed. In such case, 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.
// Use npm to install the client.
var ObsClient = require('esdk-obs-nodejs');
// Use the source code to install the client.
// var ObsClient = require('./lib/obs');

// Create an instance of ObsClient.
var 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.
       access_key_id: process.env.ACCESS_KEY_ID,
       secret_access_key: process.env.SECRET_ACCESS_KEY,
       server : 'https://your-endpoint'
});

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

Listing Objects by Specifying a Prefix

Sample code:

// Import the OBS library.
// Use npm to install the client.
var ObsClient = require('esdk-obs-nodejs');
// Use the source code to install the client.
// var ObsClient = require('./lib/obs');

// Create an instance of ObsClient.
var 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.
       access_key_id: process.env.ACCESS_KEY_ID,
       secret_access_key: process.env.SECRET_ACCESS_KEY,
       server : 'https://your-endpoint'
});

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

Listing Objects by Specifying the Start Position

Sample code:

// Import the OBS library.
// Use npm to install the client.
var ObsClient = require('esdk-obs-nodejs');
// Use the source code to install the client.
// var ObsClient = require('./lib/obs');

// Create an instance of ObsClient.
var 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.
       access_key_id: process.env.ACCESS_KEY_ID,
       secret_access_key: process.env.SECRET_ACCESS_KEY,
       server : 'https://your-endpoint'
});

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

Listing All Objects in Paging Mode

Sample code:

// Import the OBS library.
// Use npm to install the client.
var ObsClient = require('esdk-obs-nodejs');
// Use the source code to install the client.
// var ObsClient = require('./lib/obs');

// Create an instance of ObsClient.
var 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.
       access_key_id: process.env.ACCESS_KEY_ID,
       secret_access_key: process.env.SECRET_ACCESS_KEY,
       server : 'https://your-endpoint'
});

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']);
                                  
                           }
                           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.
// Use npm to install the client.
var ObsClient = require('esdk-obs-nodejs');
// Use the source code to install the client.
// var ObsClient = require('./lib/obs');

// Create an instance of ObsClient.
var 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.
       access_key_id: process.env.ACCESS_KEY_ID,
       secret_access_key: process.env.SECRET_ACCESS_KEY,
       server : 'https://your-endpoint'
});

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']);
                                  
                           }
                           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.
// Use npm to install the client.
var ObsClient = require('esdk-obs-nodejs');
// Use the source code to install the client.
// var ObsClient = require('./lib/obs');

// Create an instance of ObsClient.
var 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.
       access_key_id: process.env.ACCESS_KEY_ID,
       secret_access_key: process.env.SECRET_ACCESS_KEY,
       server : 'https://your-endpoint'
});

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('\n');
                                         if(result.InterfaceResult.CommonPrefixes && result.InterfaceResult.CommonPrefixes.length > 0){
                                                listObjectsByPrefix(result.InterfaceResult.CommonPrefixes);
                                         }
                                  }
                           });
                     }
              };
              listObjectsByPrefix(result.InterfaceResult.CommonPrefixes);
       }
});
  • 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.