Updated on 2024-04-30 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 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

Name prefix that the objects 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. The value ranges from 1 to 1000. If the value is not in this range, 1000 objects are listed 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 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 1000 objects can be listed.

// Import the dependency library.
require 'vendor/autoload.php';
// Import the SDK code library during source code installation.
// require 'obs-autoloader.php';
// Declare the namespace.
use Obs\ObsClient;
// Create an instance of ObsClient.
$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.      'key' => getenv('ACCESS_KEY_ID'),
      'secret' => getenv('SECRET_ACCESS_KEY'),
      'endpoint' => 'https://your-endpoint',
      'signature' => 'obs'
] );

$resp = $obsClient->listObjects ( [ 
       'Bucket' => 'bucketname' 
] );

printf ( "RequestId:%s\n", $resp ['RequestId'] );
foreach ( $resp ['Contents'] as $index => $content ) {
       printf ( "Contents[%d]\n", $index + 1 );
       printf ( "Key:%s\n", $content ['Key'] );
       printf ( "LastModified:%s\n", $content ['LastModified'] );
       printf ( "ETag:%s\n", $content ['ETag'] );
       printf ( "Size:%s\n", $content ['Size'] );
       printf ( "Owner[ID]:%s\n", $content ['Owner'] ['ID'] );
       printf ( "StorageClass:%s\n", $content ['StorageClass'] );
}
  • A maximum of 1,000 objects can be listed each time. If a bucket contains more than 1,000 objects, IsTruncated in the response is true, indicating not all objects were listed. In such case, you can use 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 dependency library.
require 'vendor/autoload.php';
// Import the SDK code library during source code installation.
// require 'obs-autoloader.php';
// Declare the namespace.
use Obs\ObsClient;
// Create an instance of ObsClient.
$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.      'key' => getenv('ACCESS_KEY_ID'),
      'secret' => getenv('SECRET_ACCESS_KEY'),
      'endpoint' => 'https://your-endpoint',
      'signature' => 'obs'
] );

$resp = $obsClient->listObjects ( [ 
       'Bucket' => 'bucketname',
       // Specify the number of objects to be listed to 100.
       'MaxKeys' => 100
] );

printf ( "RequestId:%s\n", $resp ['RequestId'] );
foreach ( $resp ['Contents'] as $index => $content ) {
       printf ( "Contents[%d]\n", $index + 1 );
       printf ( "Key:%s\n", $content ['Key'] );
       printf ( "LastModified:%s\n", $content ['LastModified'] );
       printf ( "ETag:%s\n", $content ['ETag'] );
       printf ( "Size:%s\n", $content ['Size'] );
       printf ( "Owner[ID]:%s\n", $content ['Owner'] ['ID'] );
       printf ( "StorageClass:%s\n", $content ['StorageClass'] );
}

Listing Objects by Specifying a Prefix

Sample code:

// Import the dependency library.
require 'vendor/autoload.php';
// Import the SDK code library during source code installation.
// require 'obs-autoloader.php';
// Declare the namespace.
use Obs\ObsClient;
// Create an instance of ObsClient.
$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.      'key' => getenv('ACCESS_KEY_ID'),
      'secret' => getenv('SECRET_ACCESS_KEY'),
      'endpoint' => 'https://your-endpoint',
      'signature' => 'obs'
] );

$resp = $obsClient->listObjects ( [ 
       'Bucket' => 'bucketname',
       // Set the prefix to prefix and the number of objects to be listed to 100.
       'MaxKeys' => 100,
       'Prefix' => 'prefix'
] );

printf ( "RequestId:%s\n", $resp ['RequestId'] );
foreach ( $resp ['Contents'] as $index => $content ) {
       printf ( "Contents[%d]\n", $index + 1 );
       printf ( "Key:%s\n", $content ['Key'] );
       printf ( "LastModified:%s\n", $content ['LastModified'] );
       printf ( "ETag:%s\n", $content ['ETag'] );
       printf ( "Size:%s\n", $content ['Size'] );
       printf ( "Owner[ID]:%s\n", $content ['Owner'] ['ID'] );
       printf ( "StorageClass:%s\n", $content ['StorageClass'] );
}

Listing Objects by Specifying the Start Position

Sample code:

// Import the dependency library.
require 'vendor/autoload.php';
// Import the SDK code library during source code installation.
// require 'obs-autoloader.php';
// Declare the namespace.
use Obs\ObsClient;
// Create an instance of ObsClient.
$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.      'key' => getenv('ACCESS_KEY_ID'),
      'secret' => getenv('SECRET_ACCESS_KEY'),
      'endpoint' => 'https://your-endpoint',
      'signature' => 'obs'
] );

$resp = $obsClient->listObjects ( [ 
       'Bucket' => 'bucketname',
       // Set that 100 objects whose names follow test in lexicographical order will be listed.
       'MaxKeys' => 100,
       'Marker' => 'test'
] );

printf ( "RequestId:%s\n", $resp ['RequestId'] );
foreach ( $resp ['Contents'] as $index => $content ) {
       printf ( "Contents[%d]\n", $index + 1 );
       printf ( "Key:%s\n", $content ['Key'] );
       printf ( "LastModified:%s\n", $content ['LastModified'] );
       printf ( "ETag:%s\n", $content ['ETag'] );
       printf ( "Size:%s\n", $content ['Size'] );
       printf ( "Owner[ID]:%s\n", $content ['Owner'] ['ID'] );
       printf ( "StorageClass:%s\n", $content ['StorageClass'] );
}

Listing All Objects in Paging Mode

Sample code:

// Import the dependency library.
require 'vendor/autoload.php';
// Import the SDK code library during source code installation.
// require 'obs-autoloader.php';
// Declare the namespace.
use Obs\ObsClient;
// Create an instance of ObsClient.
$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.
      'key' => getenv('ACCESS_KEY_ID'),
      'secret' => getenv('SECRET_ACCESS_KEY'),
      'endpoint' => 'https://your-endpoint',
      'signature' => 'obs'
] );

$marker = null;
$index = 1;
do {
              
       $resp = $obsClient->listObjects ( [ 
              'Bucket' => 'bucketname',
              // Set the number of parts displayed per page to 100.
              'MaxKeys' => 100,
              'Marker' => $marker
       ] );
       
       printf ( "RequestId:%s\n", $resp ['RequestId'] );
       foreach ( $resp ['Contents'] as $content ) {
              printf ( "Contents[%d]\n", $index );
              printf ( "Key:%s\n", $content ['Key'] );
              printf ( "LastModified:%s\n", $content ['LastModified'] );
              printf ( "ETag:%s\n", $content ['ETag'] );
              printf ( "Size:%s\n", $content ['Size'] );
              printf ( "Owner[ID]:%s\n", $content ['Owner'] ['ID'] );
              printf ( "StorageClass:%s\n", $content ['StorageClass'] );
              $index ++;
       }
       $marker = $resp['NextMarker'];    
}while($resp['IsTruncated']);

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 dependency library.
require 'vendor/autoload.php';
// Import the SDK code library during source code installation.
// require 'obs-autoloader.php';
// Declare the namespace.
use Obs\ObsClient;
// Create an instance of ObsClient.
$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.
      'key' => getenv('ACCESS_KEY_ID'),
      'secret' => getenv('SECRET_ACCESS_KEY'),
      'endpoint' => 'https://your-endpoint',
      'signature' => 'obs'
] );

$marker = null;
$index = 1;
do {
              
       $resp = $obsClient->listObjects ( [ 
              'Bucket' => 'bucketname',
              'MaxKeys' => 1000,
              // Set the prefix of the folders to dir/.
              'Prefix' => 'dir/',
              'Marker' => $marker
       ] );
       
       printf ( "RequestId:%s\n", $resp ['RequestId'] );
       foreach ( $resp ['Contents'] as $content ) {
              printf ( "Contents[%d]\n", $index );
              printf ( "Key:%s\n", $content ['Key'] );
              printf ( "LastModified:%s\n", $content ['LastModified'] );
              printf ( "ETag:%s\n", $content ['ETag'] );
              printf ( "Size:%s\n", $content ['Size'] );
              printf ( "Owner[ID]:%s\n", $content ['Owner'] ['ID'] );
              printf ( "StorageClass:%s\n", $content ['StorageClass'] );
              $index ++;
       }
       $marker = $resp['NextMarker'];    
}while($resp['IsTruncated']);

Listing All Objects According to Folders in a Bucket

Sample code:

// Import the dependency library.
require 'vendor/autoload.php';
// Import the SDK code library during source code installation.
// require 'obs-autoloader.php';
// Declare the namespace.
use Obs\ObsClient;
// Create an instance of ObsClient.
$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.      'key' => getenv('ACCESS_KEY_ID'),
      'secret' => getenv('SECRET_ACCESS_KEY'),
      'endpoint' => 'https://your-endpoint',
      'signature' => 'obs'
] );

function listObjectsByPrefix($commonPrefiexes){
       global $obsClient;
       foreach ($commonPrefiexes as $commonPrefiex){
              $resp = $obsClient->listObjects ( [
                           'Bucket' => 'bucketname',
                           // Set folder isolators to slashes (/).
                           'Delimiter' => '/',
                           'Prefix' => $commonPrefiex['Prefix']
              ] );
              printf("Objects in folder [%s]:\n", $commonPrefiex['Prefix']);
              foreach ( $resp ['Contents'] as $index => $content ) {
                     printf ( "Contents[%d]\n", $index );
                     printf ( "Key:%s\n", $content ['Key'] );
                     printf ( "LastModified:%s\n", $content ['LastModified'] );
                     printf ( "ETag:%s\n", $content ['ETag'] );
                     printf ( "Size:%s\n", $content ['Size'] );
                     printf ( "Owner[ID]:%s\n", $content ['Owner'] ['ID'] );
                     printf ( "StorageClass:%s\n", $content ['StorageClass'] );
              }
              printf("\n");
              listObjectsByPrefix($resp['CommonPrefixes']);
       }
}

$resp = $obsClient->listObjects ( [
       'Bucket' => 'bucketname',
       // Set folder isolators to slashes (/).
       'Delimiter' => '/'
] );

printf( "Objects in the root directory:\n");
foreach ( $resp ['Contents'] as $index => $content ) {
       printf ( "Contents[%d]\n", $index );
       printf ( "Key:%s\n", $content ['Key'] );
       printf ( "LastModified:%s\n", $content ['LastModified'] );
       printf ( "ETag:%s\n", $content ['ETag'] );
       printf ( "Size:%s\n", $content ['Size'] );
       printf ( "Owner[ID]:%s\n", $content ['Owner'] ['ID'] );
       printf ( "StorageClass:%s\n", $content ['StorageClass'] );
}
printf("\n");
listObjectsByPrefix($resp['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, Contents includes the objects in the folder and CommonPrefixes includes the sub-folders in the folder.