All Products
Search
Document Center

Object Storage Service:List files (Python SDK V2)

Last Updated:Aug 01, 2025

This topic describes how to list all objects in a bucket using Object Storage Service (OSS) SDK for Python.

Usage notes

  • The sample code in this topic uses the region ID cn-hangzhou of the China (Hangzhou) region as an example. A public endpoint is used by default. If you want to access OSS from other Alibaba Cloud products in the same region, use an internal endpoint. For more information about the mappings between OSS regions and endpoints, see Regions and endpoints.

  • To list files, you must have the oss:ListObjects permission. For more information, see Attach a custom policy to a RAM user.

Sample code

The following sample code provides an example on how to call the ListObjectsV2 operation to list all objects in a bucket:

import argparse import alibabacloud_oss_v2 as oss # Create a command line parameter parser. parser = argparse.ArgumentParser(description="list objects v2 sample") # Specify the --region parameter, which specifies the region in which the bucket is located. This command line parameter is required. parser.add_argument('--region', help='The region in which the bucket is located.', required=True) # Specify the --bucket parameter, which specifies the name of the bucket. This command line parameter is required. parser.add_argument('--bucket', help='The name of the bucket.', required=True) # Specify the --endpoint parameter, which specifies the endpoint that other services can use to access OSS. This command line parameter is optional. parser.add_argument('--endpoint', help='The domain names that other services can use to access OSS') def main(): args = parser.parse_args() # Parse the command line parameters. # Obtain access credentials from environment variables for authentication. credentials_provider = oss.credentials.EnvironmentVariableCredentialsProvider() # Load the default configurations of the SDK and specify the credential provider. cfg = oss.config.load_default() cfg.credentials_provider = credentials_provider # Set the region in the configuration to the one specified in the command line. cfg.region = args.region # If the endpoint parameter is provided, specify the endpoint. if args.endpoint is not None: cfg.endpoint = args.endpoint # Use the configurations to create an OSSClient instance. client = oss.Client(cfg) # Create a paginator to allow the ListObjectsV2 operation to list objects. paginator = client.list_objects_v2_paginator() # Traverse each page of the listed objects. for page in paginator.iter_page(oss.ListObjectsV2Request( bucket=args.bucket ) ): # Traverse each object on each page. for o in page.contents: # Display the name, size, and last modified time of the object. print(f'Object: {o.key}, {o.size}, {o.last_modified}') if __name__ == "__main__": main() # Specify the entry points in the main function of the script when the script is directly run.

Common scenarios

List all files in a specified folder

The following sample code provides an example on how to specify the prefix parameter to list information about all objects in a specific directory, including the object size, last modified time, and object name:

import argparse import alibabacloud_oss_v2 as oss # Create a command line parameter parser. parser = argparse.ArgumentParser(description="list objects v2 sample") # Specify the --region parameter, which specifies the region in which the bucket is located. This command line parameter is required. parser.add_argument('--region', help='The region in which the bucket is located.', required=True) # Specify the --bucket parameter, which specifies the name of the bucket. This command line parameter is required. parser.add_argument('--bucket', help='The name of the bucket.', required=True) # Specify the --endpoint parameter, which specifies the endpoint that other services can use to access OSS. This command line parameter is optional. parser.add_argument('--endpoint', help='The domain names that other services can use to access OSS') def main(): args = parser.parse_args() # Parse the command line parameters. # Obtain access credentials from environment variables for authentication. credentials_provider = oss.credentials.EnvironmentVariableCredentialsProvider() # Load the default configurations of the SDK and specify the credential provider. cfg = oss.config.load_default() cfg.credentials_provider = credentials_provider # Set the region in the configuration to the one specified in the command line. cfg.region = args.region # If the endpoint parameter is provided, specify the endpoint. if args.endpoint is not None: cfg.endpoint = args.endpoint # Use the configurations to create an OSSClient instance. client = oss.Client(cfg) # Create a paginator to allow the ListObjectsV2 operation to list objects. paginator = client.list_objects_v2_paginator() # Traverse each page of the listed objects. for page in paginator.iter_page(oss.ListObjectsV2Request( bucket=args.bucket, prefix="exampledir/", # Set the prefix parameter to exampledir/ to list all objects in the exampledir/ directory. ) ): # Traverse each object on each page. for o in page.contents: # Display the name, size, and last modified time of the object. print(f'Object: {o.key}, Size: {o.size}, Last_modified: {o.last_modified}') if __name__ == "__main__": main() # Specify the entry points in the main function of the script when the script is directly run.

List objects whose names contain a specific prefix

The following sample code provides an example on how to specify the prefix parameter to list information about objects whose names contain the specified prefix, including the object size, last modified time, and object name:

import argparse import alibabacloud_oss_v2 as oss # Create a command line parameter parser. parser = argparse.ArgumentParser(description="list objects v2 sample") # Specify the --region parameter, which specifies the region in which the bucket is located. This command line parameter is required. parser.add_argument('--region', help='The region in which the bucket is located.', required=True) # Specify the --bucket parameter, which specifies the name of the bucket. This command line parameter is required. parser.add_argument('--bucket', help='The name of the bucket.', required=True) # Specify the --endpoint parameter, which specifies the endpoint that other services can use to access OSS. This command line parameter is optional. parser.add_argument('--endpoint', help='The domain names that other services can use to access OSS') def main(): args = parser.parse_args() # Parse the command line parameters. # Obtain access credentials from environment variables for authentication. credentials_provider = oss.credentials.EnvironmentVariableCredentialsProvider() # Load the default configurations of the SDK and specify the credential provider. cfg = oss.config.load_default() cfg.credentials_provider = credentials_provider # Set the region in the configuration to the one specified in the command line. cfg.region = args.region # If the endpoint parameter is provided, specify the endpoint. if args.endpoint is not None: cfg.endpoint = args.endpoint # Use the configurations to create an OSSClient instance. client = oss.Client(cfg) # Create a paginator to allow the ListObjectsV2 operation to list objects. paginator = client.list_objects_v2_paginator() # Traverse each page of the listed objects. for page in paginator.iter_page(oss.ListObjectsV2Request( bucket=args.bucket, prefix="my-object-", # Set the prefix parameter to my-object-, which means only objects whose names contain the my-object- prefix are listed. ) ): # Traverse each object on each page. for o in page.contents: # Display the name, size, and last modified time of the object. print(f'Object: {o.key}, Size: {o.size}, Last_modified: {o.last_modified}') if __name__ == "__main__": main() # Specify the entry points in the main function of the script when the script is directly run.

List a specific number of objects

The following sample code provides an example on how to specify the MaxKeys parameter to list information about a specific number of objects, including the object size, last modified time, and object name:

import argparse import alibabacloud_oss_v2 as oss # Create a command line parameter parser. parser = argparse.ArgumentParser(description="list objects v2 sample") # Specify the --region parameter, which specifies the region in which the bucket is located. This command line parameter is required. parser.add_argument('--region', help='The region in which the bucket is located.', required=True) # Specify the --bucket parameter, which specifies the name of the bucket. This command line parameter is required. parser.add_argument('--bucket', help='The name of the bucket.', required=True) # Specify the --endpoint parameter, which specifies the endpoint that other services can use to access OSS. This command line parameter is optional. parser.add_argument('--endpoint', help='The domain names that other services can use to access OSS') def main(): args = parser.parse_args() # Parse the command line parameters. # Obtain access credentials from environment variables for authentication. credentials_provider = oss.credentials.EnvironmentVariableCredentialsProvider() # Load the default configurations of the SDK and specify the credential provider. cfg = oss.config.load_default() cfg.credentials_provider = credentials_provider # Set the region in the configuration to the one specified in the command line. cfg.region = args.region # If the endpoint parameter is provided, specify the endpoint. if args.endpoint is not None: cfg.endpoint = args.endpoint # Use the configurations to create an OSSClient instance. client = oss.Client(cfg) # Create a paginator to allow the ListObjectsV2 operation to list objects. paginator = client.list_objects_v2_paginator() # Traverse each page of the listed objects. for page in paginator.iter_page(oss.ListObjectsV2Request( bucket=args.bucket, max_keys=10, # Specify that up to 10 objects can be returned per page. ) ): # Traverse each object on each page. for o in page.contents: # Display the name, size, and last modified time of the object. print(f'Object: {o.key}, Size: {o.size}, Last_modified: {o.last_modified}') print('-' * 30) if __name__ == "__main__": main() # Specify the entry points in the main function of the script when the script is directly run.

List all objects from a specific position

The following sample code provides an example on how to configure the StartAfter parameter to specify the start position from which the list operation starts. All objects whose names are alphabetically after the value of the StartAfter parameter are returned.

import argparse import alibabacloud_oss_v2 as oss # Create a command line parameter parser. parser = argparse.ArgumentParser(description="list objects v2 sample") # Specify the --region parameter, which specifies the region in which the bucket is located. This command line parameter is required. parser.add_argument('--region', help='The region in which the bucket is located.', required=True) # Specify the --bucket parameter, which specifies the name of the bucket. This command line parameter is required. parser.add_argument('--bucket', help='The name of the bucket.', required=True) # Specify the --endpoint parameter, which specifies the endpoint that other services can use to access OSS. This command line parameter is optional. parser.add_argument('--endpoint', help='The domain names that other services can use to access OSS') def main(): args = parser.parse_args() # Parse the command line parameters. # Obtain access credentials from environment variables for authentication. credentials_provider = oss.credentials.EnvironmentVariableCredentialsProvider() # Load the default configurations of the SDK and specify the credential provider. cfg = oss.config.load_default() cfg.credentials_provider = credentials_provider # Set the region in the configuration to the one specified in the command line. cfg.region = args.region # If the endpoint parameter is provided, specify the endpoint. if args.endpoint is not None: cfg.endpoint = args.endpoint # Use the configurations to create an OSSClient instance. client = oss.Client(cfg) # Create a paginator to allow the ListObjectsV2 operation to list objects. paginator = client.list_objects_v2_paginator() # Traverse each page of the listed objects. for page in paginator.iter_page(oss.ListObjectsV2Request( bucket=args.bucket, start_after="my-object", # Specify that objects whose names are alphabetically after my-object are listed. ) ): # Traverse each object on each page. for o in page.contents: # Display the name, size, and last modified time of the object. print(f'Object: {o.key}, Size: {o.size}, Last_modified: {o.last_modified}') print('-' * 30) if __name__ == "__main__": main() # Specify the entry points in the main function of the script when the script is directly run.

List all subdirectories in the root directory

import argparse import alibabacloud_oss_v2 as oss # Create a command-line argument parser. parser = argparse.ArgumentParser(description="list objects v2 sample") # Add the --region command-line argument, which specifies the region where the bucket is located. This argument is required. parser.add_argument('--region', help='The region in which the bucket is located.', required=True) # Add the --bucket command-line argument, which specifies the name of the bucket. This argument is required. parser.add_argument('--bucket', help='The name of the bucket.', required=True) # Add the --endpoint command-line argument, which specifies the domain name that other services can use to access OSS. This argument is not required. parser.add_argument('--endpoint', help='The domain names that other services can use to access OSS') def main(): args = parser.parse_args() # Parse the command-line arguments. # Load credential information from environment variables for identity verification. credentials_provider = oss.credentials.EnvironmentVariableCredentialsProvider() # Load the default configurations of the SDK and set the credential provider. cfg = oss.config.load_default() cfg.credentials_provider = credentials_provider # Set the region in the configuration. cfg.region = args.region # If the endpoint argument is provided, set the endpoint in the configuration. if args.endpoint is not None: cfg.endpoint = args.endpoint # Create an OSS client using the configured information. client = oss.Client(cfg) # Create a paginator for the ListObjectsV2 operation. paginator = client.list_objects_v2_paginator() # Traverse each page of the object list. for page in paginator.iter_page(oss.ListObjectsV2Request( bucket=args.bucket, prefix="", delimiter="/", ) ): # Print the subdirectories (common prefixes) on each page. for prefix in page.common_prefixes: print(f'Subdirectory: {prefix.prefix}') if __name__ == "__main__": main() # The entry point of the script. The main function is called when the file is run directly.

References