• Object Storage Service

obs
  1. Help Center
  2. Object Storage Service
  3. Developer Guide (Python 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

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.

max_keys

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

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 module.
from com.obs.client.obs_client import ObsClient
# Create an instance of ObsClient.
obsClient = ObsClient(
    access_key_id='*** Provide your Access Key ***',    
    secret_access_key='*** Provide your Secret Key ***',    
    server='yourdomainname'
)

resp = obsClient.listObjects('bucketname')

if resp.status < 300:
    print('requestId:', resp.requestId)
    index = 1    
    for content in resp.body.contents:        
        print('object [' + str(index) + ']')        
        print('key:', content.key)        
        print('owner_id:', content.owner.owner_id)        
        print('owner_name:', content.owner.owner_name)        
        index += 1
else:
    print('errorCode:', resp.errorCode)
    print('errorMessage:', resp.errorMessage)
NOTE:
  • A maximum of 1000 objects can be listed each time. If a bucket contains more than 1000 objects and body.is_truncated is True in the returned result, not all objects are listed. In such cases, you can use body.next_marker 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 module.
from com.obs.client.obs_client import ObsClient
# Create an instance of ObsClient.
obsClient = ObsClient(
    access_key_id='*** Provide your Access Key ***',    
    secret_access_key='*** Provide your Secret Key ***',    
    server='yourdomainname'
)

# Set the number of objects to be listed to 100.
resp = obsClient.listObjects('bucketname', max_keys=100)

if resp.status < 300:
    print('requestId:', resp.requestId)
    index = 1    
    for content in resp.body.contents:        
        print('object [' + str(index) + ']')        
        print('key:', content.key)        
        print('owner_id:', content.owner.owner_id)        
        print('owner_name:', content.owner.owner_name)        
        index += 1
else:
    print('errorCode:', resp.errorCode)
    print('errorMessage:', resp.errorMessage)

Listing Objects by Specifying a Prefix

Sample code:

# Import the module.
from com.obs.client.obs_client import ObsClient
# Create an instance of ObsClient.
obsClient = ObsClient(
    access_key_id='*** Provide your Access Key ***',    
    secret_access_key='*** Provide your Secret Key ***',    
    server='yourdomainname'
)

# Set the prefix to prefix and the number of objects to be listed to 100.
resp = obsClient.listObjects('bucketname', max_keys=100, prefix='prefix')

if resp.status < 300:
    print('requestId:', resp.requestId)
    index = 1    
    for content in resp.body.contents:        
        print('object [' + str(index) + ']')        
        print('key:', content.key)        
        print('owner_id:', content.owner.owner_id)        
        print('owner_name:', content.owner.owner_name)        
        index += 1
else:
    print('errorCode:', resp.errorCode)
    print('errorMessage:', resp.errorMessage)

Listing Objects by Specifying the Start Position

Sample code:

# Import the module.
from com.obs.client.obs_client import ObsClient
# Create an instance of ObsClient.
obsClient = ObsClient(
    access_key_id='*** Provide your Access Key ***',    
    secret_access_key='*** Provide your Secret Key ***',    
    server='yourdomainname'
)

# Set that 100 objects whose names are following "test" in lexicographical order will be listed.
resp = obsClient.listObjects('bucketname', max_keys=100, marker='test')

if resp.status < 300:
    print('requestId:', resp.requestId)
    index = 1    
    for content in resp.body.contents:        
        print('object [' + str(index) + ']')        
        print('key:', content.key)        
        print('owner_id:', content.owner.owner_id)        
        print('owner_name:', content.owner.owner_name)        
        index += 1
else:
    print('errorCode:', resp.errorCode)
    print('errorMessage:', resp.errorMessage)

Listing All Objects in Paging Mode

Sample code:

# Import the module.
from com.obs.client.obs_client import ObsClient
# Create an instance of ObsClient.
obsClient = ObsClient(
    access_key_id='*** Provide your Access Key ***',    
    secret_access_key='*** Provide your Secret Key ***',    
    server='yourdomainname'
)

# Set the number of objects displayed per page to 100.
max_keys = 100
index = 1   
marker = None
while True:
    resp = obsClient.listObjects('bucketname', max_keys=max_keys, marker=marker)
    if resp.status < 300:
        print('requestId:', resp.requestId)
        for content in resp.body.contents:        
            print('object [' + str(index) + ']')        
            print('key:', content.key)        
            print('owner_id:', content.owner.owner_id)        
            print('owner_name:', content.owner.owner_name)        
            index += 1
        if not resp.body.is_truncated:
            break
        marker = resp.body.next_marker
    else:
        print('errorCode:', resp.errorCode)
        print('errorMessage:', resp.errorMessage)
        break

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 module.
from com.obs.client.obs_client import ObsClient
# Create an instance of ObsClient.
obsClient = ObsClient(
    access_key_id='*** Provide your Access Key ***',    
    secret_access_key='*** Provide your Secret Key ***',    
    server='yourdomainname'
)

# Set the number of objects to be listed per page to 100.
max_keys = 100
index = 1   
marker = None
# Set the prefix to "dir/".
prefix = 'dir/'
while True:
    resp = obsClient.listObjects('bucketname', max_keys=max_keys, marker=marker, prefix=prefix)
    if resp.status < 300:
        print('requestId:', resp.requestId)
        for content in resp.body.contents:        
            print('object [' + str(index) + ']')        
            print('key:', content.key)        
            print('owner_id:', content.owner.owner_id)        
            print('owner_name:', content.owner.owner_name)        
            index += 1
        if not resp.body.is_truncated:
            break
        marker = resp.body.next_marker
    else:
        print('errorCode:', resp.errorCode)
        print('errorMessage:', resp.errorMessage)
        break

Listing All Objects According to Folders in a Bucket

Sample code:

# Import the module.
from com.obs.client.obs_client import ObsClient
# Create an instance of ObsClient.
obsClient = ObsClient(
    access_key_id='*** Provide your Access Key ***',    
    secret_access_key='*** Provide your Secret Key ***',    
    server='yourdomainname'
)

def listObjectsByPrefix(resp):
    for prefix in resp.body.commonPrefixs:
        subresp = obsClient.listObjects('bucketname', delimiter='/', prefix=prefix.prefix)
        if subresp .status < 300:
            print('Objects in folder [' + prefix.prefix + '] :')
            for content in subresp.body.contents:
                print('key:', content.key)        
                print('owner_id:', content.owner.owner_id)        
                print('owner_name:', content.owner.owner_name)       
            listObjectsByPrefix(subresp)
        else:
            print('errorCode:', resp.errorCode)
            print('errorMessage:', resp.errorMessage)

resp = obsClient.listObjects('bucketname', delimiter='/')
if resp.status < 300:
    print('Object in the root directory:')
    for content in resp.body.contents:
        print('key:', content.key)        
        print('owner_id:', content.owner.owner_id)        
        print('owner_name:', content.owner.owner_name)       
    listObjectsByPrefix(resp)
else:
    print('errorCode:', resp.errorCode)
    print('errorMessage:', resp.errorMessage)
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, body.contents includes the objects in the folder and body.commonPrefixs includes the sub-folders in the folder.