This article explains how to use listObjectsType2 to enumerate all objects within a specified bucket, files with a designated prefix, as well as files and subdirectories within a specified directory.
Precautions
- The ListObjects interface has been updated to ListObjectsType2. Developers are strongly encouraged to use ListObjectsType2. To ensure backward compatibility, TOS continues to support the ListObjects interface.
- To use List Objects V2, you must have the tos:ListBucket permission. For detailed instructions, see the IAM Policy Overview.
Parameter Description
The supported parameters for listObjectsType2 are as follows.
Sample Code
Brief Enumeration
The following code is used to list up to 100 objects in the bucket examplebucket.
Paginated Listing
The following code is used to list objects in the examplebucket bucket with pagination. By setting maxKeys, you can specify the maximum number of objects returned in a single request. In the sample code, 100 objects are listed from the bucket examplebucket. To continue listing objects, set continuationToken to the nextContinuationToken returned from the initial listing.
List all objects with the specified prefix
The following code is used to list all objects with the specified prefix in the bucket examplebucket.
Enumerate all objects
The following code is used to list all objects in the bucket examplebucket.
List the files and subdirectories in the specified directory.
TOS supports only the concept of objects and allows the creation of an object with a size of zero and a slash in its name.../The object at the end simulates the functionality of a directory.
The functionality of a directory can be simulated using the delimiter and prefix parameters:
- First, set the delimiter to/When prefix is set to empty, objects and subdirectory information in the root directory can be returned.
- Set the delimiter again to/Simultaneously, when prefix is set to a subdirectory (subfiledir), objects within the subdirectory and its subdirectories can be returned.
The following code is used to enumerate objects and subdirectories in the root directory.