Objects are listed alphabetically. The Python SDK provides a series of object listing methods.

Simple list

The following code provides an example on how to list 10 objects in a specified bucket:

# -*- coding: utf-8 -*-
import oss2
from itertools import islice

# Security risks may arise if you use the AccessKey pair of an Alibaba Cloud account to log on to OSS, because the account has permissions on all API operations. We recommend that you use a RAM user to call API operations or perform routine operations and maintenance. To create your RAM user, log on to https://ram.console.aliyun.com.
auth = oss2.Auth('<yourAccessKeyId>', '<yourAccessKeySecret>')
# This example uses the endpoint of the China (Hangzhou) region. Specify the actual endpoint based on your requirements.
bucket = oss2.Bucket(auth, 'http://oss-cn-hangzhou.aliyuncs.com', '<yourBucketName>')

for b in islice(oss2.ObjectIterator(bucket), 10):
    print(b.key)
			

List all objects in a bucket

Run the following code to list objects in a specified bucket:

# -*- coding: utf-8 -*-
import oss2

# Security risks may arise if you use the AccessKey pair of an Alibaba Cloud account to log on to OSS, because the account has permissions on all API operations. We recommend that you use a RAM user to call API operations or perform routine operations and maintenance. To create your RAM user, log on to https://ram.console.aliyun.com.
auth = oss2.Auth('<yourAccessKeyId>', '<yourAccessKeySecret>')
# This example uses the endpoint of the China (Hangzhou) region. Specify the actual endpoint based on your requirements.
bucket = oss2.Bucket(auth, 'http://oss-cn-hangzhou.aliyuncs.com', '<yourBucketName>')

# List all objects in a bucket.
for obj in oss2.ObjectIterator(bucket):
    print(obj.key)

List objects whose names contain a specified prefix

Assume that a bucket has four objects oss.jpg, fun/test.jpg, fun/movie/001.avi, and fun/movie/007.avi. The forward slash (/) is used as the folder delimiter.

The following code provides an example on how to list all objects whose names contain a specified prefix:

# -*- coding: utf-8 -*-
import oss2

# Security risks may arise if you use the AccessKey pair of an Alibaba Cloud account to log on to OSS, because the account has permissions on all API operations. We recommend that you use a RAM user to call API operations or perform routine operations and maintenance. To create your RAM user, log on to https://ram.console.aliyun.com.
auth = oss2.Auth('<yourAccessKeyId>', '<yourAccessKeySecret>')
# This example uses the endpoint of the China (Hangzhou) region. Specify the actual endpoint based on your requirements.
bucket = oss2.Bucket(auth, 'http://oss-cn-hangzhou.aliyuncs.com', '<yourBucketName>')

# List all objects in the fun folder, including objects from the subfolders.
for obj in oss2.ObjectIterator(bucket, prefix='fun/'):
    print(obj.key)

The expected result is as follows:

fun/movie/001.avi
fun/movie/007.avi
fun/test.jpg

List objects and subfolders in a specified folder

OSS does not use folders. All elements are stored as objects. To simulate a folder on the OSS console, you actually create a 0 MB object whose name ends with a forward slash (/). This object can be uploaded and downloaded. By default, the OSS console displays an object whose name ends with a forward slash (/) as a folder.

The delimiter and prefix parameters can be used to simulate folder functions.

  • If prefix is set to the name of a folder, objects and subfolders (subdirectories) whose names contain the prefix are listed.
  • If delimiter is set to a forward slash (/), only the objects and subfolders (subdirectories) are listed. The objects and folders in subfolders are not listed.

Use the following code to list objects and subfolders in a folder:

# -*- coding: utf-8 -*-
import oss2

# Security risks may arise if you use the AccessKey pair of an Alibaba Cloud account to log on to OSS, because the account has permissions on all API operations. We recommend that you use a RAM user to call API operations or perform routine operations and maintenance. To create your RAM user, log on to https://ram.console.aliyun.com.
auth = oss2.Auth('<yourAccessKeyId>', '<yourAccessKeySecret>')
# This example uses the endpoint of the China (Hangzhou) region. Specify the actual endpoint based on your requirements.
bucket = oss2.Bucket(auth, 'http://oss-cn-hangzhou.aliyuncs.com', '<yourBucketName>')

# List objects and subfolders in the fun folder. Objects in the subfolders are not listed.
for obj in oss2.ObjectIterator(bucket, prefix = 'fun/', delimiter = '/'):
    # Use is_prefix to determine whether obj is a folder.
    if obj.is_prefix():  # folder
        print('directory: ' + obj.key)
    else:                # object
        print('file: ' + obj.key)

The expected result is as follows:

directory: fun/movie/
file: fun/test.jpg

List the sizes of objects in a specified folder

The following code provides an example on how to query the sizes of objects in a specified folder:

# -*- coding: utf-8 -*-
import oss2

def CalculateFolderLength(bucket, folder):
    length = 0
    for obj in oss2.ObjectIterator(bucket, prefix=folder):
        length += obj.size
    return length

# Security risks may arise if you use the AccessKey pair of an Alibaba Cloud account to log on to OSS, because the account has permissions on all API operations. We recommend that you use a RAM user to call API operations or perform routine operations and maintenance. To create your RAM user, log on to https://ram.console.aliyun.com.
auth = oss2.Auth('<yourAccessKeyId>', '<yourAccessKeySecret>')
# This example uses the endpoint of the China (Hangzhou) region. Specify the actual endpoint based on your requirements.
bucket = oss2.Bucket(auth, 'http://oss-cn-hangzhou.aliyuncs.com', '<yourBucketName>')

for obj in oss2.ObjectIterator(bucket, delimiter='/'):
    if obj.is_prefix():  # folder
        length = CalculateFolderLength(bucket, obj.key)
        print('directory: ' + obj.key + '  length:' + str(length) + "Byte.")
    else: # object
        print('file:' + obj.key + '  length:' + str(obj.size) + "Byte.")