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

Property in OBS .NET SDK

BucketName

Bucket name

ListObjectsRequest.BucketName

Prefix

Name prefix that the objects to be listed must contain

ListObjectsRequest.Prefix

Marker

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

ListObjectsRequest.Marker

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.

ListObjectsRequest.MaxKeys

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.)

ListObjectsRequest.Delimiter

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.

// Create an instance of ObsClient.
ObsClient client = new ObsClient("*** Provide your Access Key ***", "*** Provide your Secret Key ***", "https://your-endpoint");
// Simple listing
try
{
    ListObjectsRequest request = new ListObjectsRequest();
    request.BucketName = "bucketname";
    ListObjectsResponse response = client.ListObjects(request);
    foreach (ObsObject entry in response.ObsObjects)
    {
        Console.WriteLine("key = {0} size = {1}", entry.ObjectKey, entry.Size);
    }
}
catch (ObsException ex)
{
    Console.WriteLine("ErrorCode: {0}", ex.ErrorCode);
    Console.WriteLine("ErrorMessage: {0}", ex.ErrorMessage);
} 

Information about a maximum of 1000 objects can be listed each time. If a bucket contains more than 1000 objects and ListObjectsResponse.IsTruncated is true in the returned result, not all objects are listed. In such cases, you can use ListObjectsResponse.NextMarker to obtain the start position for next listing.

Listing Objects by Specifying the Number

Sample code:

// Create an instance of ObsClient.
ObsClient client = new ObsClient("*** Provide your Access Key ***", "*** Provide your Secret Key ***", "https://your-endpoint");
// Specify the number.
try
{
    ListObjectsRequest request = new ListObjectsRequest();
    request.BucketName = "bucketname";
       // Set the number of objects to be listed to 100.
    request.MaxKeys = 100;
    ListObjectsResponse response = client.ListObjects(request);
    foreach (ObsObject entry in response.ObsObjects)
    {
        Console.WriteLine("key = {0} size = {1}", entry.ObjectKey, entry.Size);
    }
}
catch (ObsException ex)
{
    Console.WriteLine("ErrorCode: {0}", ex.ErrorCode);
    Console.WriteLine("ErrorMessage: {0}", ex.ErrorMessage);
}

Listing Objects by Specifying a Prefix

Sample code:

// Create an instance of ObsClient.
ObsClient client = new ObsClient("*** Provide your Access Key ***", "*** Provide your Secret Key ***", "https://your-endpoint");
// List objects by specifying a prefix.
try
{
    ListObjectsRequest request = new ListObjectsRequest();
    request.BucketName = "bucketname";
    //Specify the prefix.
    request.Prefix = "prefix"; ;
    ListObjectsResponse response = client.ListObjects(request);
    foreach (ObsObject entry in response.ObsObjects)
    {
        Console.WriteLine("key = {0} size = {1}", entry.ObjectKey, entry.Size);
    }
}
catch (ObsException ex)
{
    Console.WriteLine("ErrorCode: {0}", ex.ErrorCode);
    Console.WriteLine("ErrorMessage: {0}", ex.ErrorMessage);
} 

Listing Objects by Specifying the Start Position

Sample code:

// Create an instance of ObsClient.
ObsClient client = new ObsClient("*** Provide your Access Key ***", "*** Provide your Secret Key ***", "https://your-endpoint");
// List objects by specifying the start position.
try
{
    ListObjectsRequest request = new ListObjectsRequest();
    request.BucketName = "bucketname";
    //Specify the start position for listing.
    request.Marker = "marker";
    ListObjectsResponse response = client.ListObjects(request);
    foreach (ObsObject entry in response.ObsObjects)
    {
        Console.WriteLine("key = {0} size = {1}", entry.ObjectKey, entry.Size);
    }
}
catch (ObsException ex)
{
    Console.WriteLine("ErrorCode: {0}", ex.ErrorCode);
    Console.WriteLine("ErrorMessage: {0}", ex.ErrorMessage);
}

Listing All Objects in Paging Mode

Sample code:

// Create an instance of ObsClient.
ObsClient client = new ObsClient("*** Provide your Access Key ***", "*** Provide your Secret Key ***", "https://your-endpoint");
//List all objects in paging mode.
try
{
    ListObjectsRequest request = new ListObjectsRequest();
    ListObjectsResponse response;
    request.BucketName = "bucketname";
    request.MaxKeys = 100;
    do
    {
        response = client.ListObjects(request);
        foreach (ObsObject entry in response.ObsObjects)
        {
            Console.WriteLine("key = {0} size = {1}", entry.ObjectKey, entry.Size);
        }
        request.Marker = response.NextMarker;
    }
    while (response.IsTruncated);
}
catch (ObsException ex)
{
    Console.WriteLine("ErrorCode: {0}", ex.ErrorCode);
    Console.WriteLine("ErrorMessage: {0}", ex.ErrorMessage);
} 

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:

// Create an instance of ObsClient.
ObsClient client = new ObsClient("*** Provide your Access Key ***", "*** Provide your Secret Key ***", "https://your-endpoint");
//List all objects in a folder.
try
{
    ListObjectsRequest request = new ListObjectsRequest();
    ListObjectsResponse response;
    request.BucketName = "bucketname";
    request.MaxKeys = 1000;
    // Set the prefix to dir/.
    request.Prefix = "dir/";
    do
    {
        response = client.ListObjects(request);
        foreach (ObsObject entry in response.ObsObjects)
        {
            Console.WriteLine("key = {0} size = {1}", entry.ObjectKey, entry.Size);
        }
        request.Marker = response.NextMarker;
    }
    while (response.IsTruncated);
}
catch (ObsException ex)
{
    Console.WriteLine("ErrorCode: {0}", ex.ErrorCode);
    Console.WriteLine("ErrorMessage: {0}", ex.ErrorMessage);
}