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

Method in OBS Android SDK

bucketName

Bucket name

ListObjectsRequest.setBucketName

prefix

Name prefix that the objects to be listed must contain

ListObjectsRequest.setPrefix

marker

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

ListObjectsRequest.setMarker

maxKeys

Maximum number of objects listed in the response body. The value ranges from 1 to 1000. If the value exceeds 1000, only 1,000 objects are returned.

ListObjectsRequest.setMaxKeys

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.

ListObjectsRequest.setDelimiter

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.

// 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 ACCESS_KEY_ID and SECRET_ACCESS_KEY_ID.
// 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.
String ak = System.getenv("ACCESS_KEY_ID");
String sk = System.getenv("SECRET_ACCESS_KEY_ID");
String endPoint = "https://your-endpoint";
// Create an instance of ObsClient.
final ObsClient obsClient = new ObsClient(ak, sk, endPoint);

ObjectListing result = obsClient.listObjects("bucketname");
for(ObsObject obsObject : result.getObjects()){
    Log.i("ListObjects", "\t" + obsObject.getObjectKey());
    Log.i("ListObjects","\t" + obsObject.getOwner());
}
  • A maximum of 1000 objects can be listed each time. If a bucket contains more than 1000 objects and ObjectListing.isTruncated is true in the returned result, not all objects are listed. In such cases, you can use ObjectListing.getNextMarker 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:

// 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 ACCESS_KEY_ID and SECRET_ACCESS_KEY_ID.
// 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.
String ak = System.getenv("ACCESS_KEY_ID");
String sk = System.getenv("SECRET_ACCESS_KEY_ID");
String endPoint = "https://your-endpoint";
// Create an instance of ObsClient.
final ObsClient obsClient = new ObsClient(ak, sk, endPoint);

ListObjectsRequest request = new ListObjectsRequest("bucketname");
// Specify the number of objects to be listed to 100.
request.setMaxKeys(100);
ObjectListing result = obsClient.listObjects(request);
for(ObsObject obsObject : result.getObjects()){
    Log.i("ListObjects","\t" + obsObject.getObjectKey());
    Log.i("ListObjects","\t" + obsObject.getOwner());
}

Listing Objects by Specifying a Prefix

Sample code:

// 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 ACCESS_KEY_ID and SECRET_ACCESS_KEY_ID.
// 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.
String ak = System.getenv("ACCESS_KEY_ID");
String sk = System.getenv("SECRET_ACCESS_KEY_ID");
String endPoint = "https://your-endpoint";
// Create an instance of ObsClient.
final ObsClient obsClient = new ObsClient(ak, sk, endPoint);

ListObjectsRequest request = new ListObjectsRequest("bucketname");
// Set the number to 100 and the prefix to prefix. 
request.setMaxKeys(100);
request.setPrefix("prefix");
ObjectListing result = obsClient.listObjects(request);
for(ObsObject obsObject : result.getObjects()){
    Log.i("ListObjects","\t" + obsObject.getObjectKey());
    Log.i("ListObjects","\t" + obsObject.getOwner());
}

Listing Objects by Specifying the Start Position

Sample code:

// 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 ACCESS_KEY_ID and SECRET_ACCESS_KEY_ID.
// 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.
String ak = System.getenv("ACCESS_KEY_ID");
String sk = System.getenv("SECRET_ACCESS_KEY_ID");
String endPoint = "https://your-endpoint";
// Create an instance of ObsClient.
final ObsClient obsClient = new ObsClient(ak, sk, endPoint);

ListObjectsRequest request = new ListObjectsRequest("bucketname");
// List 100 objects following test in lexicographic order.
request.setMaxKeys(100);
request.setMarker("test");
ObjectListing result = obsClient.listObjects(request);
for(ObsObject obsObject : result.getObjects()){
    Log.i("ListObjects","\t" + obsObject.getObjectKey());
    Log.i("ListObjects","\t" + obsObject.getOwner());
}

Listing All Objects in Paging Mode

Sample code:

// 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 ACCESS_KEY_ID and SECRET_ACCESS_KEY_ID.
// 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.
String ak = System.getenv("ACCESS_KEY_ID");
String sk = System.getenv("SECRET_ACCESS_KEY_ID");
String endPoint = "https://your-endpoint";
// Create an instance of ObsClient.
final ObsClient obsClient = new ObsClient(ak, sk, endPoint);

ListObjectsRequest request = new ListObjectsRequest("bucketname");
// Set the number of objects displayed per page to 100.
request.setMaxKeys(100);

ObjectListing result;
do{
    result = obsClient.listObjects(request);
    for(ObsObject obsObject : result.getObjects()){
        Log.i("ListObjects","\t" + obsObject.getObjectKey());
        Log.i("ListObjects","\t" + obsObject.getOwner());
    }
    
    request.setMarker(result.getNextMarker());
}while(result.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:

// 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 ACCESS_KEY_ID and SECRET_ACCESS_KEY_ID.
// 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.
String ak = System.getenv("ACCESS_KEY_ID");
String sk = System.getenv("SECRET_ACCESS_KEY_ID");
String endPoint = "https://your-endpoint";
// Create an instance of ObsClient.
ObsClient obsClient = new ObsClient(ak, sk, endPoint);

ListObjectsRequest request = new ListObjectsRequest("bucketname");
// Set the prefix of objects in the folder to dir/.
request.setPrefix("dir/");
request.setMaxKeys(1000);

ObjectListing result;

do{
    result = obsClient.listObjects(request);
    for (ObsObject obsObject : result.getObjects())
    {
        Log.i("ListObjects","\t" + obsObject.getObjectKey());
        Log.i("ListObjects","\t" + obsObject.getOwner());
    }
    request.setMarker(result.getNextMarker());
}while(result.isTruncated());

Listing All Objects According to Folders in a Bucket

Sample code:

// 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 ACCESS_KEY_ID and SECRET_ACCESS_KEY_ID.
// 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.
String ak = System.getenv("ACCESS_KEY_ID");
String sk = System.getenv("SECRET_ACCESS_KEY_ID");
String endPoint = "https://your-endpoint";
// Create an instance of ObsClient.
final ObsClient obsClient = new ObsClient(ak, sk, endPoint);

ListObjectsRequest request = new ListObjectsRequest("bucketname");
request.setMaxKeys(1000);
// Set folder isolators to slashes.
request.setDelimiter("/");
ObjectListing result = obsClient.listObjects(request);
Log.i("ListObjects", "Objects in the root directory:");
for(ObsObject obsObject : result.getObjects()){
    Log.i("ListObjects","\t" + obsObject.getObjectKey());
    Log.i("ListObjects","\t" + obsObject.getOwner());
}
listObjectsByPrefix(obsClient, request, result);

The following is the sample code of the listObjectsByPrefix function, which is used to recursively list objects in sub-folders.

static void listObjectsByPrefix(ObsClient obsClient, ListObjectsRequest request, ObjectListing result) throws ObsException
{
      for(String prefix : result.getCommonPrefixes()){
          Log.i("ListObjects", "Objects in folder [" + prefix + "]:");
          request.setPrefix(prefix);
          result  = obsClient.listObjects(request);
          for(ObsObject obsObject : result.getObjects()){
              Log.i("ListObjects","\t" + obsObject.getObjectKey());
              Log.i("ListObjects","\t" + obsObject.getOwner());
          }
          listObjectsByPrefix(obsClient, request, result);
      }
}
  • 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, ObjectListing.getObjects includes the objects in the folder and ObjectListing.getCommonPrefixes includes the sub-folders in the folder.