This topic describes how to list all objects, objects whose names contain the specified prefix, and objects and subdirectories in the specified directory in a bucket.
Background information
You can call the GetBucket (ListObjects) or GetBucketV2 (ListObjectsV2) operation to list up to 1,000 objects in a bucket at a time. You can configure the parameters to use different methods to list objects. For example, you can configure the parameters to list objects from the specified start position, list objects and subdirectories in the specified directory, and list objects by page. The GetBucket (ListObjects) and GetBucketV2 (ListObjectsV2) operations are different in the following aspects:
- By default, if you call the GetBucket (ListObjects) operation to list objects, the owner information is included in the response.
- If you call the GetBucketV2 (ListObjectsV2) operation to list objects, you can configure
the fetchOwner parameter to specify whether to include the owner information in the
response.
Note To list objects in a versioned bucket, we recommend that you call the GetBucketV2 (ListObjectsV2) operation.
The following tables describe the parameters that you can configure when you call the GetBucket (ListObjects) or GetBucketV2 (ListObjectsV2) operation to list objects.
- Call the GetBucket (ListObjects) operation to list objects
The following table describes the parameters that you can configure when you call the GetBucket (ListObjects) operation to list objects.
Parameter Description delimiter The character that is used to group objects by name. Objects whose names contain the same string that ranges from the specified prefix to the delimiter that appears for the first time are grouped as a commonPrefixes element. prefix The prefix that the names of returned objects must contain. maxKeys The maximum number of objects that can be listed at a time. The default value is 100, and the maximum value is 1000. marker The position from which the list operation starts. For more information, see GetBucket (ListObjects).
- Call the GetBucketV2 (ListObjectsV2) operation to list objects
The following table describes the parameters that you can configure when you call the GetBucketV2 (ListObjectsV2) operation to list objects.
Parameter Description prefix The prefix that the names of returned objects must contain. delimiter The character that is used to group objects by name. Objects whose names contain the same string that ranges from the specified prefix to the delimiter that appears for the first time are grouped as a commonPrefixes element. startAfter The position from which the list operation starts. fetchOwner Specifies whether to include the owner information in the response. - true: The response includes the owner information.
- false: The response does not include the owner information.
For more information, see GetBucketV2 (ListObjectsV2).
Simple list
- Call the GetBucket (ListObjects) operation to list objects
The following code provides an example on how to call the GetBucket (ListObjects) operation to list 100 objects in the specified bucket:
package main import ( "fmt" "os" "github.com/aliyun/aliyun-oss-go-sdk/oss" ) func HandleError(err error) { fmt.Println("Error:", err) os.Exit(-1) } func main() { // Create an OSSClient instance. // Set yourEndpoint to the endpoint of the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set yourEndpoint to https://oss-cn-hangzhou.aliyuncs.com. Specify the endpoint based on your business requirements. // Security risks may arise if you use the AccessKey pair of an Alibaba Cloud account to access Object Storage Service (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 a RAM user, log on to the RAM console. client, err := oss.New("yourEndpoint", "yourAccessKeyId", "yourAccessKeySecret") if err != nil { HandleError(err) } // Specify the bucket name. bucketName := "yourBucketName" bucket, err := client.Bucket(bucketName) if err != nil { HandleError(err) } // List objects. marker := "" for { lsRes, err := bucket.ListObjects(oss.Marker(marker)) if err != nil { HandleError(err) } // Display the listed objects. By default, a maximum of 100 objects are returned at a time. for _, object := range lsRes.Objects { fmt.Println("Bucket: ", object.Key) } if lsRes.IsTruncated { marker = lsRes.NextMarker } else { break } } }
- Call the GetBucketV2 (ListObjectsV2) operation to list objects
The following code provides an example on how to call the GetBucketV2 (ListObjectsV2) operation to list 100 objects in the specified bucket:
package main import ( "fmt" "os" "github.com/aliyun/aliyun-oss-go-sdk/oss" ) func HandleError(err error) { fmt.Println("Error:", err) os.Exit(-1) } func main() { // Create an OSSClient instance. // Set yourEndpoint to the endpoint of the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set yourEndpoint to https://oss-cn-hangzhou.aliyuncs.com. Specify the endpoint based on your business requirements. // Security risks may arise if you use the AccessKey pair of an Alibaba Cloud account to access 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 a RAM user, log on to the RAM console. client, err := oss.New("yourEndpoint", "yourAccessKeyId", "yourAccessKeySecret") if err != nil { HandleError(err) } // Specify the bucket name. bucketName := "yourBucketName" bucket, err := client.Bucket(bucketName) if err != nil { HandleError(err) } continueToken := "" for { lsRes, err := bucket.ListObjectsV2(oss.ContinuationToken(continueToken)) if err != nil { HandleError(err) } // Display the listed objects. By default, a maximum of 100 objects are returned at a time. for _, object := range lsRes.Objects { fmt.Println(object.Key, object.Type, object.Size, object.ETag, object.LastModified, object.StorageClass) } if lsRes.IsTruncated { continueToken = lsRes.NextContinuationToken } else { break } } }
List the specified number of objects
- Call the GetBucket (ListObjects) operation to list objects
The following code provides an example on how to call the GetBucket (ListObjects) operation to list the specified number of objects:
package main import ( "fmt" "os" "github.com/aliyun/aliyun-oss-go-sdk/oss" ) func main() { // Create an OSSClient instance. // Set yourEndpoint to the endpoint of the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set yourEndpoint to https://oss-cn-hangzhou.aliyuncs.com. Specify the endpoint based on your business requirements. // Security risks may arise if you use the AccessKey pair of an Alibaba Cloud account to access 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 a RAM user, log on to the RAM console. client, err := oss.New("yourEndpoint", "yourAccessKeyId", "yourAccessKeySecret") if err != nil { fmt.Println("Error:", err) os.Exit(-1) } // Specify the bucket name. bucketName := "yourBucketName" bucket, err := client.Bucket(bucketName) if err != nil { fmt.Println("Error:", err) os.Exit(-1) } // Set the maximum number of objects that can be listed at a time and list the objects. lsRes, err := bucket.ListObjects(oss.MaxKeys(200)) if err != nil { fmt.Println("Error:", err) os.Exit(-1) } // Display the listed objects. By default, a maximum of 100 objects are returned at a time. fmt.Println("Objects:", lsRes.Objects) for _, object := range lsRes.Objects { fmt.Println("Object:", object.Key) } }
- Call the GetBucketV2 (ListObjectsV2) operation to list objects
The following code provides an example on how to call the GetBucketV2 (ListObjectsV2) operation to list the specified number of objects:
package main import ( "fmt" "os" "github.com/aliyun/aliyun-oss-go-sdk/oss" ) func HandleError(err error) { fmt.Println("Error:", err) os.Exit(-1) } func main() { // Create an OSSClient instance. // Set yourEndpoint to the endpoint of the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set yourEndpoint to https://oss-cn-hangzhou.aliyuncs.com. Specify the endpoint based on your business requirements. // Security risks may arise if you use the AccessKey pair of an Alibaba Cloud account to access 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 a RAM user, log on to the RAM console. client, err := oss.New("yourEndpoint", "yourAccessKeyId", "yourAccessKeySecret") if err != nil { HandleError(err) } // Specify the bucket name. bucketName := "yourBucketName" bucket, err := client.Bucket(bucketName) if err != nil { HandleError(err) } // Configure the MaxKeys parameter to specify the maximum number of objects that can be listed at a time and list the objects. lsRes, err := bucket.ListObjectsV2(oss.MaxKeys(200)) if err != nil { HandleError(err) } // Display the listed objects. By default, a maximum of 100 objects are returned at a time. for _, object := range lsRes.Objects { fmt.Println(object.Key, object.Type, object.Size, object.ETag, object.LastModified, object.StorageClass, object.Owner.ID, object.Owner.DisplayName) } }
List objects whose names contain the specified prefix
- Call the GetBucket (ListObjects) operation to list objects
The following code provides an example on how to call the GetBucket (ListObjects) operation to list objects whose names contain the specified prefix:
package main import ( "fmt" "os" "github.com/aliyun/aliyun-oss-go-sdk/oss" ) func main() { // Create an OSSClient instance. // Set yourEndpoint to the endpoint of the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set yourEndpoint to https://oss-cn-hangzhou.aliyuncs.com. Specify the endpoint based on your business requirements. // Security risks may arise if you use the AccessKey pair of an Alibaba Cloud account to access 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 a RAM user, log on to the RAM console. client, err := oss.New("yourEndpoint", "yourAccessKeyId", "yourAccessKeySecret") if err != nil { fmt.Println("Error:", err) os.Exit(-1) } // Specify the bucket name. bucketName := "yourBucketName" bucket, err := client.Bucket(bucketName) if err != nil { fmt.Println("Error:", err) os.Exit(-1) } // List objects whose names contain the specified prefix. By default, a maximum of 100 objects are listed. lsRes, err := bucket.ListObjects(oss.Prefix("my-object-")) if err != nil { fmt.Println("Error:", err) os.Exit(-1) } // Display the listed objects. By default, a maximum of 100 objects are returned at a time. fmt.Println("Objects:", lsRes.Objects) for _, object := range lsRes.Objects { fmt.Println("Object:", object.Key) } }
- Call the GetBucketV2 (ListObjectsV2) operation to list objects
The following code provides an example on how to call the GetBucketV2 (ListObjectsV2) operation to list objects whose names contain the specified prefix:
package main import ( "fmt" "os" "github.com/aliyun/aliyun-oss-go-sdk/oss" ) func HandleError(err error) { fmt.Println("Error:", err) os.Exit(-1) } func main() { // Create an OSSClient instance. // Set yourEndpoint to the endpoint of the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set yourEndpoint to https://oss-cn-hangzhou.aliyuncs.com. Specify the endpoint based on your business requirements. // Security risks may arise if you use the AccessKey pair of an Alibaba Cloud account to access 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 a RAM user, log on to the RAM console. client, err := oss.New("yourEndpoint", "yourAccessKeyId", "yourAccessKeySecret") if err != nil { HandleError(err) } // Specify the bucket name. bucketName := "yourBucketName" bucket, err := client.Bucket(bucketName) if err != nil { HandleError(err) } // Set the Prefix parameter to my-object-. lsRes, err := bucket.ListObjectsV2(oss.Prefix("my-object-")) if err != nil { HandleError(err) } // Display the listed objects. By default, a maximum of 100 objects are returned at a time. for _, object := range lsRes.Objects { fmt.Println(object.Key, object.Type, object.Size, object.ETag, object.LastModified, object.StorageClass, object.Owner.ID, object.Owner.DisplayName) } }
List all objects from the specified start position
- Call the GetBucket (ListObjects) operation to list objects
You can configure the Marker parameter to specify the start position from which the list operation starts. All objects whose names are alphabetically after the value of Marker are returned.
package main import ( "fmt" "github.com/aliyun/aliyun-oss-go-sdk/oss" "os" ) func main() { // Create an OSSClient instance. // Set yourEndpoint to the endpoint of the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set yourEndpoint to https://oss-cn-hangzhou.aliyuncs.com. Specify the endpoint based on your business requirements. // Security risks may arise if you use the AccessKey pair of an Alibaba Cloud account to access 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 a RAM user, log on to the RAM console. client, err := oss.New("yourEndpoint", "yourAccessKeyId", "yourAccessKeySecret") if err != nil { fmt.Println("Error:", err) os.Exit(-1) } // Specify the bucket name. bucketName := "yourBucketName" if err != nil { fmt.Println("Error:", err) os.Exit(-1) } marker := oss.Marker("") for { lsRes, err := bucket.ListObjects(marker) if err != nil { fmt.Println("Error:", err) os.Exit(-1) } // Display the listed objects. By default, a maximum of 100 objects are returned at a time. fmt.Println("Objects:", lsRes.Objects) for _, object := range lsRes.Objects { fmt.Println("Object:", object.Key) } if lsRes.IsTruncated { marker = oss.Marker(lsRes.NextMarker) }else{ break } } }
- Call the GetBucketV2 (ListObjectsV2) operation to list objects
You can 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 StartAfter are returned.
package main import ( "fmt" "os" "github.com/aliyun/aliyun-oss-go-sdk/oss" ) func HandleError(err error) { fmt.Println("Error:", err) os.Exit(-1) } func main() { // Create an OSSClient instance. // Set yourEndpoint to the endpoint of the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set yourEndpoint to https://oss-cn-hangzhou.aliyuncs.com. Specify the endpoint based on your business requirements. // Security risks may arise if you use the AccessKey pair of an Alibaba Cloud account to access 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 a RAM user, log on to the RAM console. client, err := oss.New("yourEndpoint", "yourAccessKeyId", "yourAccessKeySecret") if err != nil { HandleError(err) } // Specify the bucket name. bucketName := "yourBucketName" bucket, err := client.Bucket(bucketName) if err != nil { HandleError(err) } // Configure the StartAfter parameter to list objects whose names are alphabetically after the value of StartAfter. By default, a maximum of 100 objects are listed. lsRes, err := bucket.ListObjectsV2(oss.StartAfter("my-object-")) if err != nil { HandleError(err) } // Display the listed objects. By default, a maximum of 100 objects are returned at a time. for _, object := range lsRes.Objects { fmt.Println(object.Key, object.Type, object.Size, object.ETag, object.LastModified, object.StorageClass, object.Owner.ID, object.Owner.DisplayName) } }
List all objects by page
- Call the GetBucket (ListObjects) operation to list objects
The following code provides an example on how to call the GetBucket (ListObjects) operation to list all objects in the specified bucket by page: You can configure the MaxKeys parameter to specify the maximum number of objects that can be listed on each page.
package main import ( "fmt" "os" "github.com/aliyun/aliyun-oss-go-sdk/oss" ) func main() { // Create an OSSClient instance. // Set yourEndpoint to the endpoint of the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set yourEndpoint to https://oss-cn-hangzhou.aliyuncs.com. Specify the endpoint based on your business requirements. // Security risks may arise if you use the AccessKey pair of an Alibaba Cloud account to access 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 a RAM user, log on to the RAM console. client, err := oss.New("yourEndpoint", "yourAccessKeyId", "yourAccessKeySecret") if err != nil { fmt.Println("Error:", err) os.Exit(-1) } // Specify the bucket name. bucketName := "yourBucketName" bucket, err := client.Bucket(bucketName) if err != nil { fmt.Println("Error:", err) os.Exit(-1) } // List all objects by page, with up to 100 objects on each page. marker := oss.Marker("") for { lsRes, err := bucket.ListObjects(oss.MaxKeys(100), marker) if err != nil { fmt.Println("Error:", err) os.Exit(-1) } // Display the listed objects. By default, a maximum of 100 objects are returned at a time. fmt.Println("Objects:", lsRes.Objects) if lsRes.IsTruncated { marker = oss.Marker(lsRes.NextMarker) }else{ break } } }
- Call the GetBucketV2 (ListObjectsV2) operation to list objects
The following code provides an example on how to call the GetBucketV2 (ListObjectsV2) operation to list all objects in the specified bucket by page: You can configure the MaxKeys parameter to specify the maximum number of objects that can be listed on each page.
package main import ( "fmt" "os" "github.com/aliyun/aliyun-oss-go-sdk/oss" ) func HandleError(err error) { fmt.Println("Error:", err) os.Exit(-1) } func main() { // Create an OSSClient instance. // Set yourEndpoint to the endpoint of the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set yourEndpoint to https://oss-cn-hangzhou.aliyuncs.com. Specify the endpoint based on your business requirements. // Security risks may arise if you use the AccessKey pair of an Alibaba Cloud account to access 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 a RAM user, log on to the RAM console. client, err := oss.New("yourEndpoint", "yourAccessKeyId", "yourAccessKeySecret") if err != nil { HandleError(err) } // Specify the bucket name. bucketName := "yourBucketName" bucket, err := client.Bucket(bucketName) if err != nil { HandleError(err) } // List all objects by page, with up to 100 objects on each page. continueToken := "" for { lsRes, err := bucket.ListObjectsV2(oss.MaxKeys(100), oss.ContinuationToken(continueToken)) if err != nil { HandleError(err) } // Display the listed objects. By default, a maximum of 100 objects are returned at a time. for _, object := range lsRes.Objects { fmt.Println(object.Key, object.Type, object.Size, object.ETag, object.LastModified, object.StorageClass) } if lsRes.IsTruncated { continueToken = lsRes.NextContinuationToken } else { break } } }
List objects whose names contain the specified prefix by page
- Call the GetBucket (ListObjects) operation to list objects
The following code provides an example on how to call the GetBucket (ListObjects) operation to list objects whose names contain the specified prefix by page. You can configure the MaxKeys parameter to specify the maximum number of objects that can be listed on each page.
package main import ( "fmt" "os" "github.com/aliyun/aliyun-oss-go-sdk/oss" ) func main() { // Create an OSSClient instance. // Set yourEndpoint to the endpoint of the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set yourEndpoint to https://oss-cn-hangzhou.aliyuncs.com. Specify the endpoint based on your business requirements. // Security risks may arise if you use the AccessKey pair of an Alibaba Cloud account to access 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 a RAM user, log on to the RAM console. client, err := oss.New("yourEndpoint", "yourAccessKeyId", "yourAccessKeySecret") if err != nil { fmt.Println("Error:", err) os.Exit(-1) } // Specify the bucket name. bucketName := "yourBucketName" bucket, err := client.Bucket(bucketName) if err != nil { fmt.Println("Error:", err) os.Exit(-1) } // List the objects whose names contain the specified prefix by page, with up to 80 objects on each page. prefix := oss.Prefix("my-object-") marker := oss.Marker("") for { lsRes, err := bucket.ListObjects(oss.MaxKeys(80), marker, prefix) if err != nil { fmt.Println("Error:", err) os.Exit(-1) } // Display the listed objects. By default, a maximum of 100 objects are returned at a time. fmt.Println("Objects:", lsRes.Objects) if lsRes.IsTruncated { prefix = oss.Prefix(lsRes.Prefix) marker = oss.Marker(lsRes.NextMarker) }else{ break } } }
- Call the GetBucketV2 (ListObjectsV2) operation to list objects
The following code provides an example on how to call the GetBucketV2 (ListObjectsV2) operation to list objects whose names contain the specified prefix by page. You can configure the MaxKeys parameter to specify the maximum number of objects that can be listed on each page.
package main import ( "fmt" "os" "github.com/aliyun/aliyun-oss-go-sdk/oss" ) func HandleError(err error) { fmt.Println("Error:", err) os.Exit(-1) } func main() { // Create an OSSClient instance. // Set yourEndpoint to the endpoint of the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set yourEndpoint to https://oss-cn-hangzhou.aliyuncs.com. Specify the endpoint based on your business requirements. // Security risks may arise if you use the AccessKey pair of an Alibaba Cloud account to access 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 a RAM user, log on to the RAM console. client, err := oss.New("yourEndpoint", "yourAccessKeyId", "yourAccessKeySecret") if err != nil { HandleError(err) } // Specify the bucket name. bucketName := "yourBucketName" bucket, err := client.Bucket(bucketName) if err != nil { HandleError(err) } // List the objects whose names contain the specified prefix by page, with up to 80 objects on each page. prefix := oss.Prefix("my-object-") continueToken := "" for { lsRes, err := bucket.ListObjectsV2(prefix, oss.MaxKeys(80), oss.ContinuationToken(continueToken)) if err != nil { HandleError(err) } // Display the listed objects. By default, a maximum of 100 objects are returned at a time. for _, object := range lsRes.Objects { fmt.Println(object.Key, object.Type, object.Size, object.ETag, object.LastModified, object.StorageClass) } if lsRes.IsTruncated { continueToken = lsRes.NextContinuationToken prefix = oss.Prefix(lsRes.Prefix) } else { break } } }
List the information about all objects in the specified directory
- Call the GetBucket (ListObjects) operation to list the information about all objects
in the specified directory
The following code provides an example on how to call the GetBucket (ListObjects) operation to list the information about all objects in the specified directory, including the sizes, last modified time, and names of the objects:
package main import ( "fmt" "os" "github.com/aliyun/aliyun-oss-go-sdk/oss" ) func main() { // Create an OSSClient instance. // Set yourEndpoint to the endpoint of the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set yourEndpoint to https://oss-cn-hangzhou.aliyuncs.com. Specify the endpoint based on your business requirements. // Security risks may arise if you use the AccessKey pair of an Alibaba Cloud account to access 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 a RAM user, log on to the RAM console. client, err := oss.New("yourEndpoint", "yourAccessKeyId", "yourAccessKeySecret") if err != nil { fmt.Println("Error:", err) os.Exit(-1) } // Specify the bucket name. bucketName := "yourBucketName" bucket, err := client.Bucket(bucketName) if err != nil { fmt.Println("Error:", err) os.Exit(-1) } // Traverse the objects. marker := oss.Marker("") prefix := oss.Prefix("test") for { lor, err := bucket.ListObjects(marker, prefix) if err != nil { fmt.Println("Error:", err) os.Exit(-1) } for _, object := range lor.Objects { fmt.Println(object.Key, object.Type, object.Size, object.ETag, object.LastModified, object.StorageClass) } if lor.IsTruncated { prefix = oss.Prefix(lor.Prefix) marker = oss.Marker(lor.NextMarker) }else{ break } } }
- Call the GetBucketV2 (ListObjectsV2) operation to list the information about all objects
in the specified directory
The following code provides an example on how to call the GetBucketV2 (ListObjectsV2) operation to list the information about all objects in the specified directory, including the sizes, last modified time, and names of the objects:
package main import ( "fmt" "os" "github.com/aliyun/aliyun-oss-go-sdk/oss" ) func HandleError(err error) { fmt.Println("Error:", err) os.Exit(-1) } func main() { // Create an OSSClient instance. // Set yourEndpoint to the endpoint of the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set yourEndpoint to https://oss-cn-hangzhou.aliyuncs.com. Specify the endpoint based on your business requirements. // Security risks may arise if you use the AccessKey pair of an Alibaba Cloud account to access 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 a RAM user, log on to the RAM console. client, err := oss.New("yourEndpoint", "yourAccessKeyId", "yourAccessKeySecret") if err != nil { HandleError(err) } // Specify the bucket name. bucketName := "yourBucketName" bucket, err := client.Bucket(bucketName) if err != nil { HandleError(err) } continueToken := "" prefix := oss.Prefix("fun") for { lsRes, err := bucket.ListObjectsV2(prefix, oss.ContinuationToken(continueToken)) if err != nil { HandleError(err) } // Display the listed objects. By default, a maximum of 100 objects are returned at a time. for _, object := range lsRes.Objects { fmt.Println(object.Key, object.Type, object.Size, object.ETag, object.LastModified, object.StorageClass) } if lsRes.IsTruncated { continueToken = lsRes.NextContinuationToken prefix = oss.Prefix(lsRes.Prefix) } else { break } } }
List the information about all subdirectories in the specified directory
- Call the GetBucket (ListObjects) operation to list the information about all subdirectories
in the specified directory
The following code provides an example on how to call the GetBucket (ListObjects) operation to list tthe information about all subdirectories in the specified directory:
package main import ( "fmt" "os" "github.com/aliyun/aliyun-oss-go-sdk/oss" ) func main() { // Create an OSSClient instance. // Set yourEndpoint to the endpoint of the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set yourEndpoint to https://oss-cn-hangzhou.aliyuncs.com. Specify the endpoint based on your business requirements. // Security risks may arise if you use the AccessKey pair of an Alibaba Cloud account to access 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 a RAM user, log on to the RAM console. client, err := oss.New("yourEndpoint", "yourAccessKeyId", "yourAccessKeySecret") if err != nil { fmt.Println("Error:", err) os.Exit(-1) } // Specify the bucket name. bucketName := "yourBucketName" bucket, err := client.Bucket(bucketName) if err != nil { fmt.Println("Error:", err) os.Exit(-1) } marker := oss.Marker("") prefix := oss.Prefix("yourDirPrefix") for { lor, err := bucket.ListObjects(marker, prefix, oss.Delimiter("/")) if err != nil { fmt.Println("Error:", err) os.Exit(-1) } for _, dirName := range lor.CommonPrefixes { fmt.Println(dirName) } if lor.IsTruncated { prefix = oss.Prefix(lor.Prefix) marker = oss.Marker(lor.NextMarker) }else{ break } } }
- Call the GetBucketV2 (ListObjectsV2) operation to list the information about all subdirectories
in the specified directory
The following code provides an example on how to call the GetBucketV2 (ListObjectsV2) operation to list the information about all subdirectories in the specified directory:
package main import ( "fmt" "os" "github.com/aliyun/aliyun-oss-go-sdk/oss" ) func HandleError(err error) { fmt.Println("Error:", err) os.Exit(-1) } func main() { // Create an OSSClient instance. // Set yourEndpoint to the endpoint of the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set yourEndpoint to https://oss-cn-hangzhou.aliyuncs.com. Specify the endpoint based on your business requirements. // Security risks may arise if you use the AccessKey pair of an Alibaba Cloud account to access 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 a RAM user, log on to the RAM console. client, err := oss.New("yourEndpoint", "yourAccessKeyId", "yourAccessKeySecret") if err != nil { HandleError(err) } // Specify the bucket name. bucketName := "yourBucketName" bucket, err := client.Bucket(bucketName) if err != nil { HandleError(err) } continueToken := "" prefix := oss.Prefix("") for { lsRes, err := bucket.ListObjectsV2(prefix, oss.ContinuationToken(continueToken), oss.Delimiter("/")) if err != nil { HandleError(err) } for _, dirName := range lsRes.CommonPrefixes { fmt.Println(dirName) } if lsRes.IsTruncated { prefix = oss.Prefix(lsRes.Prefix) continueToken = lsRes.NextContinuationToken } else { break } } }
List objects and their owner information
The following code provides an example on how to call the GetBucketV2 (ListObjectsV2) operation to list objects and their owner information:
package main
import (
"fmt"
"os"
"github.com/aliyun/aliyun-oss-go-sdk/oss"
)
func HandleError(err error) {
fmt.Println("Error:", err)
os.Exit(-1)
}
func main() {
// Create an OSSClient instance.
// Set yourEndpoint to the endpoint of the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set yourEndpoint to https://oss-cn-hangzhou.aliyuncs.com. Specify the endpoint based on your business requirements.
// Security risks may arise if you use the AccessKey pair of an Alibaba Cloud account to access 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 a RAM user, log on to the RAM console.
client, err := oss.New("yourEndpoint", "yourAccessKeyId", "yourAccessKeySecret")
if err != nil {
HandleError(err)
}
// Specify the bucket name.
bucketName := "yourBucketName"
bucket, err := client.Bucket(bucketName)
if err != nil {
HandleError(err)
}
// Obtain the owner information.
lsRes, err := bucket.ListObjectsV2(oss.FetchOwner(true))
if err != nil {
HandleError(err)
}
// Display the listed objects. By default, a maximum of 100 objects are returned at a time.
for _, object := range lsRes.Objects {
fmt.Println(object.Key, object.Owner.ID, object.Owner.DisplayName)
}
}
Directory
OSS does not use a hierarchical structure for objects, but instead uses a flat structure. All elements are stored as objects in buckets. A directory is an object whose size is 0 and whose name ends with a forward slash (/). You can upload and download this object. By default, objects whose names end with a forward slash (/) are displayed as a directory in the OSS console.
You can specify the delimiter and prefix parameters to list objects by directory.
- If you set prefix to a directory name in the request, objects and subdirectories whose names contain the prefix are listed.
- If you also set delimiter to a forward slash (/) in the request, the objects and subdirectories whose names start with the specified prefix in the directory are listed. Each subdirectory is listed as a single result element in commonPrefixes. The objects and directories in these subdirectories are not listed.
Example: The following four objects are stored in the bucket: oss.jpg
, fun/test.jpg
, fun/movie/001.avi
, and fun/movie/007.avi
. The forward slash (/) is specified as the directory delimiter. The following examples
describe how to list objects in simulated directories.
- List all objects in a bucket
- The following code provides an example on how to call the GetBucket (ListObjects)
operation to list all objects in the specified bucket:
package main import ( "fmt" "github.com/aliyun/aliyun-oss-go-sdk/oss" "os" ) func HandleError(err error) { fmt.Println("Error:", err) os.Exit(-1) } func main() { // Create an OSSClient instance. // Set yourEndpoint to the endpoint of the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set yourEndpoint to https://oss-cn-hangzhou.aliyuncs.com. Specify the endpoint based on your business requirements. // Security risks may arise if you use the AccessKey pair of an Alibaba Cloud account to access 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 a RAM user, log on to the RAM console. client, err := oss.New("yourEndpoint", "yourAccessKeyId", "yourAccessKeySecret") if err != nil { HandleError(err) os.Exit(-1) } // Specify the bucket name. bucketName := "yourBucketName" bucket,err := client.Bucket(bucketName) if err != nil { HandleError(err) os.Exit(-1) } // List all objects in the specified bucket. marker := oss.Marker("") for { lsRes, err := bucket.ListObjects(marker) if err != nil { HandleError(err) os.Exit(-1) } for _,object := range lsRes.Objects{ fmt.Println("Bucket",object.Key) } if lsRes.IsTruncated { marker = oss.Marker(lsRes.NextMarker) }else{ break } } }
- The following code provides an example on how to call the GetBucketV2 (ListObjectsV2)
operation to list all objects in the specified bucket:
package main import ( "fmt" "github.com/aliyun/aliyun-oss-go-sdk/oss" "os" ) func HandleError(err error) { fmt.Println("Error:", err) os.Exit(-1) } func main() { // Create an OSSClient instance. // Set yourEndpoint to the endpoint of the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set yourEndpoint to https://oss-cn-hangzhou.aliyuncs.com. Specify the endpoint based on your business requirements. // Security risks may arise if you use the AccessKey pair of an Alibaba Cloud account to access 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 a RAM user, log on to the RAM console. client, err := oss.New("yourEndpoint", "yourAccessKeyId", "yourAccessKeySecret") if err != nil { HandleError(err) os.Exit(-1) } // Specify the bucket name. bucketName := "yourBucketName" bucket,err := client.Bucket(bucketName) if err != nil { HandleError(err) os.Exit(-1) } // List all objects in the specified bucket. startAfter := "" continueToken := "" for { lsRes, err := bucket.ListObjectsV2(oss.StartAfter(startAfter),oss.ContinuationToken(continueToken)) if err != nil { HandleError(err) os.Exit(-1) } for _,object := range lsRes.Objects{ fmt.Println("Bucket",object.Key) } if lsRes.IsTruncated { startAfter = lsRes.StartAfter continueToken = lsRes.NextContinuationToken }else{ break } } }
- The following results are returned when you use the preceding two methods to list
all objects in the bucket:
Objects: fun/movie/001.avi fun/movie/007.avi fun/test.jpg oss.jpg CommonPrefixes:
- The following code provides an example on how to call the GetBucket (ListObjects)
operation to list all objects in the specified bucket:
- List all objects in the specified directory
- The following code provides an example on how to call the GetBucket (ListObjects)
operation to list all objects in the specified directory:
package main import ( "fmt" "github.com/aliyun/aliyun-oss-go-sdk/oss" "os" ) func HandleError(err error) { fmt.Println("Error:", err) os.Exit(-1) } func main() { // Create an OSSClient instance. // Set yourEndpoint to the endpoint of the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set yourEndpoint to https://oss-cn-hangzhou.aliyuncs.com. Specify the endpoint based on your business requirements. // Security risks may arise if you use the AccessKey pair of an Alibaba Cloud account to access 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 a RAM user, log on to the RAM console. client, err := oss.New("yourEndpoint", "yourAccessKeyId", "yourAccessKeySecret") if err != nil { fmt.Println("Error:", err) os.Exit(-1) } // Specify the bucket name. bucket, err := client.Bucket("yourBucketName") if err != nil { HandleError(err) os.Exit(-1) } // List all objects in the specified directory. prefix := oss.Prefix("aaa/db-init") marker := oss.Marker("") for { lsRes, err := bucket.ListObjects(prefix,marker) if err != nil { HandleError(err) os.Exit(-1) } for _,object := range lsRes.Objects{ fmt.Println("Bucket",object.Key) } if lsRes.IsTruncated { prefix = oss.Prefix(lsRes.Prefix) marker = oss.Marker(lsRes.NextMarker) }else{ break } } }
- The following code provides an example on how to call the GetBucketV2 (ListObjectsV2)
operation to list all objects in the specified directory:
package main import ( "fmt" "github.com/aliyun/aliyun-oss-go-sdk/oss" "os" ) func HandleError(err error) { fmt.Println("Error:", err) os.Exit(-1) } func main() { // Create an OSSClient instance. // Set yourEndpoint to the endpoint of the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set yourEndpoint to https://oss-cn-hangzhou.aliyuncs.com. Specify the endpoint based on your business requirements. // Security risks may arise if you use the AccessKey pair of an Alibaba Cloud account to access 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 a RAM user, log on to the RAM console. client, err := oss.New("yourEndpoint", "yourAccessKeyId", "yourAccessKeySecret") if err != nil { fmt.Println("Error:", err) os.Exit(-1) } // Specify the bucket name. bucket, err := client.Bucket("yourBucketName") if err != nil { HandleError(err) os.Exit(-1) } // List all objects in the specified directory. prefix := oss.Prefix("aaa/db-init") continueToken := "" for { lsRes, err := bucket.ListObjectsV2(prefix,oss.ContinuationToken(continueToken)) if err != nil { HandleError(err) os.Exit(-1) } for _,object := range lsRes.Objects{ fmt.Println("Bucket",object.Key) } if lsRes.IsTruncated { prefix = oss.Prefix(lsRes.Prefix) continueToken = lsRes.NextContinuationToken }else{ break } } }
- The following results are returned when you use the preceding two methods to list
all objects in the specified directory:
Objects: fun/movie/001.avi fun/movie/007.avi fun/test.jpg CommonPrefixes:
- The following code provides an example on how to call the GetBucket (ListObjects)
operation to list all objects in the specified directory:
- List objects and subdirectories in the specified directory
- The following code provides an example on how to call the GetBucket (ListObjects)
operation to list objects and subdirectories in the specified directory:
package main import ( "fmt" "github.com/aliyun/aliyun-oss-go-sdk/oss" "os" ) func HandleError(err error) { fmt.Println("Error:", err) os.Exit(-1) } func main() { // Create an OSSClient instance. // Set yourEndpoint to the endpoint of the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set yourEndpoint to https://oss-cn-hangzhou.aliyuncs.com. Specify the endpoint based on your business requirements. // Security risks may arise if you use the AccessKey pair of an Alibaba Cloud account to access 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 a RAM user, log on to the RAM console. client, err := oss.New("yourEndpoint", "yourAccessKeyId", "yourAccessKeySecret") if err != nil { fmt.Println("Error:", err) os.Exit(-1) } // Specify the bucket name. bucket, err := client.Bucket("yourBucketName") if err != nil { HandleError(err) os.Exit(-1) } // List objects and subdirectories in the specified directory. prefix := oss.Prefix("aaa/db-init/") marker := oss.Marker("") delimiter := "/" for { lsRes, err := bucket.ListObjects(prefix,marker,oss.Delimiter(delimiter)) if err != nil { HandleError(err) os.Exit(-1) } for _,object := range lsRes.Objects{ fmt.Println("Objects",object.Key) } for _,object := range lsRes.CommonPrefixes{ fmt.Println("CommonPrefixes",object) } if lsRes.IsTruncated { prefix = oss.Prefix(lsRes.Prefix) marker = oss.Marker(lsRes.NextMarker) }else{ break } } }
- The following code provides an example on how to call the GetBucketV2 (ListObjectsV2)
operation to list objects and subdirectories in the specified directory:
package main import ( "fmt" "github.com/aliyun/aliyun-oss-go-sdk/oss" "os" ) func HandleError(err error) { fmt.Println("Error:", err) os.Exit(-1) } func main() { // Create an OSSClient instance. // Set yourEndpoint to the endpoint of the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set yourEndpoint to https://oss-cn-hangzhou.aliyuncs.com. Specify the endpoint based on your business requirements. // Security risks may arise if you use the AccessKey pair of an Alibaba Cloud account to access 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 a RAM user, log on to the RAM console. client, err := oss.New("yourEndpoint", "yourAccessKeyId", "yourAccessKeySecret") if err != nil { fmt.Println("Error:", err) os.Exit(-1) } // Specify the bucket name. bucket, err := client.Bucket("yourBucketName") if err != nil { HandleError(err) os.Exit(-1) } // List objects and subdirectories in the specified directory. prefix := oss.Prefix("") continueToken := "" delimiter := "/" for { lsRes, err := bucket.ListObjectsV2(prefix,oss.Delimiter(delimiter),oss.ContinuationToken(continueToken)) if err != nil { HandleError(err) os.Exit(-1) } for _,object := range lsRes.Objects{ fmt.Println("Objects",object.Key) } for _,object := range lsRes.CommonPrefixes{ fmt.Println("CommonPrefixes",object) } if lsRes.IsTruncated { prefix = oss.Prefix(lsRes.Prefix) continueToken = lsRes.NextContinuationToken }else{ break } } }
- The following results are returned when you use the preceding two methods to list
objects and subdirectories in the specified directory:
Objects: fun/test.jpg CommonPrefixes: fun/movie/
- The following code provides an example on how to call the GetBucket (ListObjects)
operation to list objects and subdirectories in the specified directory:
- List the sizes of objects in the specified directory
- The following code provides an example on how to call the GetBucket (ListObjects)
operation to list the sizes of objects in the specified directory:
package main import ( "fmt" "github.com/aliyun/aliyun-oss-go-sdk/oss" "os" ) func HandleError(err error) { fmt.Println("Error:", err) os.Exit(-1) } func main() { // Create an OSSClient instance. // Set yourEndpoint to the endpoint of the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set yourEndpoint to https://oss-cn-hangzhou.aliyuncs.com. Specify the endpoint based on your business requirements. // Security risks may arise if you use the AccessKey pair of an Alibaba Cloud account to access 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 a RAM user, log on to the RAM console. client, err := oss.New("yourEndpoint", "yourAccessKeyId", "yourAccessKeySecret") if err != nil { fmt.Println("Error:", err) os.Exit(-1) } // Specify the bucket name. bucket, err := client.Bucket("yourBucketName") if err != nil { HandleError(err) os.Exit(-1) } // List the sizes of objects in the specified directory. prefix := "test/" marker := "" for { lsRes, err := bucket.ListObjects(oss.Prefix(prefix),oss.Marker(marker)) if err != nil { HandleError(err) os.Exit(-1) } for _,object := range lsRes.Objects{ fmt.Println("Objects",object.Key, "length",object.Size ,"Byte.") } if lsRes.IsTruncated { marker = lsRes.NextMarker prefix = lsRes.Prefix }else{ break } } }
- The following code provides an example on how to call the GetBucketV2 (ListObjectsV2)
operation to list the sizes of objects in the specified directory:
package main import ( "fmt" "github.com/aliyun/aliyun-oss-go-sdk/oss" "os" ) func HandleError(err error) { fmt.Println("Error:", err) os.Exit(-1) } func main() { // Create an OSSClient instance. // Set yourEndpoint to the endpoint of the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set yourEndpoint to https://oss-cn-hangzhou.aliyuncs.com. Specify the endpoint based on your business requirements. // Security risks may arise if you use the AccessKey pair of an Alibaba Cloud account to access 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 a RAM user, log on to the RAM console. client, err := oss.New("yourEndpoint","yourAccessKeyId","yourAccessKeySecret") if err != nil { fmt.Println("Error:", err) os.Exit(-1) } // Obtain the bucket. bucket, err := client.Bucket("yourBucketName") if err != nil { HandleError(err) os.Exit(-1) } // List the sizes of objects in the specified directory. prefix := oss.Prefix("/fun") continueToken := "" for { lsRes, err := bucket.ListObjectsV2(prefix,oss.ContinuationToken(continueToken)) if err != nil { HandleError(err) os.Exit(-1) } for _,object := range lsRes.Objects{ fmt.Println("Objects",object.Key, "length",object.Size ,"Byte.") } if lsRes.IsTruncated { prefix = oss.Prefix(lsRes.Prefix) continueToken = lsRes.NextContinuationToken } else { break } } }
- The following code provides an example on how to call the GetBucket (ListObjects)
operation to list the sizes of objects in the specified directory:
References
- For more information about the complete sample code that is used to list objects, visit GitHub.
- For more information about the API operations that you can call to list objects, see GetBucket (ListObjects) and GetBucketV2 (ListObjectsV2).