This topic describes how to list all objects, objects whose names contain a specific prefix, and objects and subdirectories in a specific directory in a bucket.
Usage notes
In this topic, the public endpoint of the China (Hangzhou) region is used. If you want to access OSS from other Alibaba Cloud services in the same region as OSS, use an internal endpoint. For more information about OSS regions and endpoints, see Regions and endpoints.
In this topic, access credentials are obtained from environment variables. For more information about how to configure access credentials, see Configure access credentials.
In this topic, an OSSClient instance is created by using an OSS endpoint. If you want to create an OSSClient instance by using custom domain names or Security Token Service (STS), see Initialization.
To list objects, you must have the
oss:ListObjects
permission. For more information, see Attach a custom policy to a RAM user.OSS SDKs for Go of v2.2.5 and later support the RestoreInfo response parameter.
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 parameters to use different methods to list objects. For example, you can configure the parameters to list objects from a specific start position, list objects and subdirectories in a specific directory, and list objects by page. The GetBucket (ListObjects) and GetBucketV2 (ListObjectsV2) operations are different in the following aspects:
If you call the GetBucket (ListObjects) operation to list objects, the object 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.
NoteTo list objects in a versioning-enabled 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.
GetBucket (ListObjects)
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 the objects that you want to list by name. Objects whose names contain the same string that stretches from the specified prefix to the first occurrence of the delimiter are grouped as a CommonPrefixes element.
prefix
The prefix that must be included in the names of objects you want to list.
maxKeys
The maximum number of objects to return. Maximum value: 1000. Default value: 100.
marker
The position from which the list operation starts.
For more information, see GetBucket (ListObjects).
GetBucketV2 (ListObjectsV2)
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 must be included in the names of objects you want to list.
delimiter
The character that is used to group the objects that you want to list by name. Objects whose names contain the same string that stretches from the specified prefix to the first occurrence of the delimiter are grouped as a CommonPrefixes element.
startAfter
The position from which the list operation starts.
fetchOwner
Specifies whether to include the object owner information in the response. Valid values:
true: The response includes the object owner information.
false: The response does not include the object owner information.
For more information, see ListObjectsV2 (GetBucketV2).
List objects by using simple list
GetBucket (ListObjects)
The following sample code provides an example on how to call the GetBucket (ListObjects) operation to list 100 objects in a specific 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() { // Obtain access credentials from environment variables. Before you run the sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are configured. provider, err := oss.NewEnvironmentVariableCredentialsProvider() if err != nil { fmt.Println("Error:", err) os.Exit(-1) } // Create an OSSClient instance. // Specify the endpoint of the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com. Specify your actual endpoint. client, err := oss.New("yourEndpoint", "", "", oss.SetCredentialsProvider(&provider)) if err != nil { HandleError(err) } // Specify the name of the bucket. bucketName := "yourBucketName" bucket, err := client.Bucket(bucketName) if err != nil { HandleError(err) } // List all objects. marker := "" for { lsRes, err := bucket.ListObjects(oss.Marker(marker)) if err != nil { HandleError(err) } // Display the listed objects. By default, up to 100 objects are returned at a time. for _, object := range lsRes.Objects { fmt.Println("Object Name: ", object.Key) } if lsRes.IsTruncated { marker = lsRes.NextMarker } else { break } } }
GetBucketV2 (ListObjectsV2)
The following sample code provides an example on how to call the GetBucketV2 (ListObjectsV2) operation to list 100 objects in a specific 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() { // Obtain access credentials from environment variables. Before you run the sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are configured. provider, err := oss.NewEnvironmentVariableCredentialsProvider() if err != nil { fmt.Println("Error:", err) os.Exit(-1) } // Create an OSSClient instance. // Specify the endpoint of the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com. Specify your actual endpoint. client, err := oss.New("yourEndpoint", "", "", oss.SetCredentialsProvider(&provider)) if err != nil { HandleError(err) } // Specify the name of the bucket. 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, up to 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 a specific number of objects
GetBucket (ListObjects)
The following sample code provides an example on how to call the GetBucket (ListObjects) operation to list a specific number of objects:
package main import ( "fmt" "os" "github.com/aliyun/aliyun-oss-go-sdk/oss" ) func main() { // Obtain access credentials from environment variables. Before you run the sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are configured. provider, err := oss.NewEnvironmentVariableCredentialsProvider() if err != nil { fmt.Println("Error:", err) os.Exit(-1) } // Create an OSSClient instance. // Specify the endpoint of the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com. Specify your actual endpoint. client, err := oss.New("yourEndpoint", "", "", oss.SetCredentialsProvider(&provider)) if err != nil { fmt.Println("Error:", err) os.Exit(-1) } // Specify the name of the bucket. bucketName := "yourBucketName" bucket, err := client.Bucket(bucketName) if err != nil { fmt.Println("Error:", err) os.Exit(-1) } // Specify 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, up to 100 objects are returned at a time. fmt.Println("Objects:", lsRes.Objects) for _, object := range lsRes.Objects { fmt.Println("Object:", object.Key) } }
GetBucketV2 (ListObjectsV2)
The following sample code provides an example on how to call the GetBucketV2 (ListObjectsV2) operation to list a specific 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() { // Obtain access credentials from environment variables. Before you run the sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are configured. provider, err := oss.NewEnvironmentVariableCredentialsProvider() if err != nil { fmt.Println("Error:", err) os.Exit(-1) } // Create an OSSClient instance. // Specify the endpoint of the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com. Specify your actual endpoint. client, err := oss.New("yourEndpoint", "", "", oss.SetCredentialsProvider(&provider)) if err != nil { fmt.Println("Error:", err) os.Exit(-1) } // Specify the name of the bucket. 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, up to 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 a specific prefix
GetBucket (ListObjects)
The following sample code provides an example on how to call the GetBucket (ListObjects) operation to list objects whose names contain a specific prefix:
package main import ( "fmt" "os" "github.com/aliyun/aliyun-oss-go-sdk/oss" ) func main() { // Obtain access credentials from environment variables. Before you run the sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are configured. provider, err := oss.NewEnvironmentVariableCredentialsProvider() if err != nil { fmt.Println("Error:", err) os.Exit(-1) } // Create an OSSClient instance. // Specify the endpoint of the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com. Specify your actual endpoint. client, err := oss.New("yourEndpoint", "", "", oss.SetCredentialsProvider(&provider)) if err != nil { fmt.Println("Error:", err) os.Exit(-1) } // Specify the name of the bucket. bucketName := "yourBucketName" bucket, err := client.Bucket(bucketName) if err != nil { fmt.Println("Error:", err) os.Exit(-1) } // List objects whose names contain a specific prefix. By default, up to 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, up to 100 objects are returned at a time. fmt.Println("Objects:", lsRes.Objects) for _, object := range lsRes.Objects { fmt.Println("Object:", object.Key) } }
GetBucketV2 (ListObjectsV2)
The following sample code provides an example on how to call the GetBucketV2 (ListObjectsV2) operation to list objects whose names contain a specific 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() { // Obtain access credentials from environment variables. Before you run the sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are configured. provider, err := oss.NewEnvironmentVariableCredentialsProvider() if err != nil { fmt.Println("Error:", err) os.Exit(-1) } // Create an OSSClient instance. // Specify the endpoint of the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com. Specify your actual endpoint. client, err := oss.New("yourEndpoint", "", "", oss.SetCredentialsProvider(&provider)) if err != nil { HandleError(err) } // Specify the name of the bucket. 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, up to 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 a specific start position
GetBucket (ListObjects)
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 the Marker parameter are returned.
package main import ( "fmt" "github.com/aliyun/aliyun-oss-go-sdk/oss" "os" ) func main() { // Obtain access credentials from environment variables. Before you run the sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are configured. provider, err := oss.NewEnvironmentVariableCredentialsProvider() if err != nil { fmt.Println("Error:", err) os.Exit(-1) } // Create an OSSClient instance. // Specify the endpoint of the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com. Specify your actual endpoint. client, err := oss.New("yourEndpoint", "", "", oss.SetCredentialsProvider(&provider)) if err != nil { fmt.Println("Error:", err) os.Exit(-1) } // Specify the name of the bucket. 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, up to 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 } } }
GetBucketV2 (ListObjectsV2)
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 the StartAfter parameter 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() { // Obtain access credentials from environment variables. Before you run the sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are configured. provider, err := oss.NewEnvironmentVariableCredentialsProvider() if err != nil { fmt.Println("Error:", err) os.Exit(-1) } // Create an OSSClient instance. // Specify the endpoint of the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com. Specify your actual endpoint. client, err := oss.New("yourEndpoint", "", "", oss.SetCredentialsProvider(&provider)) if err != nil { HandleError(err) } // Specify the name of the bucket. 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 the StartAfter parameter. By default, up to 100 objects are listed. lsRes, err := bucket.ListObjectsV2(oss.StartAfter("my-object-")) if err != nil { HandleError(err) } // Display the listed objects. By default, up to 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
GetBucket (ListObjects)
The following sample code provides an example on how to call the GetBucket (ListObjects) operation to list all objects in a specific 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() { // Obtain access credentials from environment variables. Before you run the sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are configured. provider, err := oss.NewEnvironmentVariableCredentialsProvider() if err != nil { fmt.Println("Error:", err) os.Exit(-1) } // Create an OSSClient instance. // Specify the endpoint of the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com. Specify your actual endpoint. client, err := oss.New("yourEndpoint", "", "", oss.SetCredentialsProvider(&provider)) if err != nil { fmt.Println("Error:", err) os.Exit(-1) } // Specify the name of the bucket. bucketName := "yourBucketName" bucket, err := client.Bucket(bucketName) if err != nil { fmt.Println("Error:", err) os.Exit(-1) } // List all objects by page. Specify that up to 100 objects can be listed 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, up to 100 objects are returned at a time. fmt.Println("Objects:", lsRes.Objects) if lsRes.IsTruncated { marker = oss.Marker(lsRes.NextMarker) }else{ break } } }
GetBucketV2 (ListObjectsV2)
The following sample code provides an example on how to call the GetBucketV2 (ListObjectsV2) operation to list all objects in a specific 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() { // Obtain access credentials from environment variables. Before you run the sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are configured. provider, err := oss.NewEnvironmentVariableCredentialsProvider() if err != nil { fmt.Println("Error:", err) os.Exit(-1) } // Create an OSSClient instance. // Specify the endpoint of the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com. Specify your actual endpoint. client, err := oss.New("yourEndpoint", "", "", oss.SetCredentialsProvider(&provider)) if err != nil { HandleError(err) } // Specify the name of the bucket. bucketName := "yourBucketName" bucket, err := client.Bucket(bucketName) if err != nil { HandleError(err) } // List all objects by page. Specify that up to 100 objects can be listed 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, up to 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 a specific prefix by page
GetBucket (ListObjects)
The following sample code provides an example on how to call the GetBucket (ListObjects) operation to list objects whose names contain a specific 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() { // Obtain access credentials from environment variables. Before you run the sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are configured. provider, err := oss.NewEnvironmentVariableCredentialsProvider() if err != nil { fmt.Println("Error:", err) os.Exit(-1) } // Create an OSSClient instance. // Specify the endpoint of the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com. Specify your actual endpoint. client, err := oss.New("yourEndpoint", "", "", oss.SetCredentialsProvider(&provider)) if err != nil { fmt.Println("Error:", err) os.Exit(-1) } // Specify the name of the bucket. bucketName := "yourBucketName" bucket, err := client.Bucket(bucketName) if err != nil { fmt.Println("Error:", err) os.Exit(-1) } // List the objects whose names contain a specific prefix by page. Specify that up to 80 objects can be listed 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, up to 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 } } }
GetBucketV2 (ListObjectsV2)
The following sample code provides an example on how to call the GetBucketV2 (ListObjectsV2) operation to list objects whose names contain a specific 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() { // Obtain access credentials from environment variables. Before you run the sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are configured. provider, err := oss.NewEnvironmentVariableCredentialsProvider() if err != nil { fmt.Println("Error:", err) os.Exit(-1) } // Create an OSSClient instance. // Specify the endpoint of the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com. Specify your actual endpoint. client, err := oss.New("yourEndpoint", "", "", oss.SetCredentialsProvider(&provider)) if err != nil { HandleError(err) } // Specify the name of the bucket. bucketName := "yourBucketName" bucket, err := client.Bucket(bucketName) if err != nil { HandleError(err) } // List the objects whose names contain a specific prefix by page. Specify that up to 80 objects can be listed 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, up to 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 information about all objects in a specific directory
GetBucket (ListObjects)
The following sample code provides an example on how to call the GetBucket (ListObjects) operation to list information about all objects in a specific directory, including the object size, last modified time, and object name:
package main import ( "fmt" "os" "github.com/aliyun/aliyun-oss-go-sdk/oss" ) func main() { // Obtain access credentials from environment variables. Before you run the sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are configured. provider, err := oss.NewEnvironmentVariableCredentialsProvider() if err != nil { fmt.Println("Error:", err) os.Exit(-1) } // Create an OSSClient instance. // Specify the endpoint of the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com. Specify your actual endpoint. client, err := oss.New("yourEndpoint", "", "", oss.SetCredentialsProvider(&provider)) if err != nil { fmt.Println("Error:", err) os.Exit(-1) } // Specify the name of the bucket. 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 } } }
GetBucketV2 (ListObjectsV2)
The following sample code provides an example on how to call the GetBucketV2 (ListObjectsV2) operation to list information about all objects in a specific directory, including the object size, last modified time, and object name:
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() { // Obtain access credentials from environment variables. Before you run the sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are configured. provider, err := oss.NewEnvironmentVariableCredentialsProvider() if err != nil { fmt.Println("Error:", err) os.Exit(-1) } // Create an OSSClient instance. // Specify the endpoint of the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com. Specify your actual endpoint. client, err := oss.New("yourEndpoint", "", "", oss.SetCredentialsProvider(&provider)) if err != nil { HandleError(err) } // Specify the name of the bucket. 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 information about all subdirectories in a specific directory
GetBucket (ListObjects)
The following sample code provides an example on how to call the GetBucket (ListObjects) operation to list information about all subdirectories in a specific directory:
package main import ( "fmt" "os" "github.com/aliyun/aliyun-oss-go-sdk/oss" ) func main() { // Obtain access credentials from environment variables. Before you run the sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are configured. provider, err := oss.NewEnvironmentVariableCredentialsProvider() if err != nil { fmt.Println("Error:", err) os.Exit(-1) } // Create an OSSClient instance. // Specify the endpoint of the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com. Specify your actual endpoint. client, err := oss.New("yourEndpoint", "", "", oss.SetCredentialsProvider(&provider)) if err != nil { fmt.Println("Error:", err) os.Exit(-1) } // Specify the name of the bucket. 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 } } }
GetBucketV2 (ListObjectsV2)
The following sample code provides an example on how to call the GetBucketV2 (ListObjectsV2) operation to list information about all subdirectories in a specific 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() { // Obtain access credentials from environment variables. Before you run the sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are configured. provider, err := oss.NewEnvironmentVariableCredentialsProvider() if err != nil { fmt.Println("Error:", err) os.Exit(-1) } // Create an OSSClient instance. // Specify the endpoint of the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com. Specify your actual endpoint. client, err := oss.New("yourEndpoint", "", "", oss.SetCredentialsProvider(&provider)) if err != nil { HandleError(err) } // Specify the name of the bucket. 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 sample 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() {
// Obtain access credentials from environment variables. Before you run the sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are configured.
provider, err := oss.NewEnvironmentVariableCredentialsProvider()
if err != nil {
fmt.Println("Error:", err)
os.Exit(-1)
}
// Create an OSSClient instance.
// Specify the endpoint of the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com. Specify your actual endpoint.
client, err := oss.New("yourEndpoint", "", "", oss.SetCredentialsProvider(&provider))
if err != nil {
HandleError(err)
}
// Specify the name of the bucket.
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, up to 100 objects are returned at a time.
for _, object := range lsRes.Objects {
fmt.Println(object.Key, object.Owner.ID, object.Owner.DisplayName)
}
}
Directory
OSS uses a flat structure to store objects. A directory is a zero-byte object whose name ends with a forward slash (/). You can upload and download this object. By default, an object whose name ends with a forward slash (/) is displayed as a directory in the OSS console. For the complete sample code for creating directories, visit GitHub.
You can specify the delimiter and prefix parameters to list objects by directory.
If you set prefix to a directory name in the request, the objects and subdirectories whose names contain the prefix are listed.
If you specify a prefix and 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.
Assume that a bucket contains the following objects: 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 sample code provides an example on how to call the GetBucket (ListObjects) operation to list all objects in a 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() { // Obtain access credentials from environment variables. Before you run the sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are configured. provider, err := oss.NewEnvironmentVariableCredentialsProvider() if err != nil { fmt.Println("Error:", err) os.Exit(-1) } // Create an OSSClient instance. // Specify the endpoint of the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com. Specify your actual endpoint. client, err := oss.New("yourEndpoint", "", "", oss.SetCredentialsProvider(&provider)) if err != nil { HandleError(err) os.Exit(-1) } // Specify the name of the bucket. bucketName := "yourBucketName" bucket,err := client.Bucket(bucketName) if err != nil { HandleError(err) os.Exit(-1) } // List all objects in the 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 sample code provides an example on how to call the GetBucketV2 (ListObjectsV2) operation to list all objects in a 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() { // Obtain access credentials from environment variables. Before you run the sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are configured. provider, err := oss.NewEnvironmentVariableCredentialsProvider() if err != nil { fmt.Println("Error:", err) os.Exit(-1) } // Create an OSSClient instance. // Specify the endpoint of the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com. Specify your actual endpoint. client, err := oss.New("yourEndpoint", "", "", oss.SetCredentialsProvider(&provider)) if err != nil { HandleError(err) os.Exit(-1) } // Specify the name of the bucket. bucketName := "yourBucketName" bucket,err := client.Bucket(bucketName) if err != nil { HandleError(err) os.Exit(-1) } // List all objects in the 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 response is returned when you call the preceding two operations to list all objects in the bucket:
Objects: fun/movie/001.avi fun/movie/007.avi fun/test.jpg oss.jpg CommonPrefixes:
List all objects in a specific directory
The following sample code provides an example on how to call the GetBucket (ListObjects) operation to list all objects in a specific 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() { // Obtain access credentials from environment variables. Before you run the sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are configured. provider, err := oss.NewEnvironmentVariableCredentialsProvider() if err != nil { fmt.Println("Error:", err) os.Exit(-1) } // Create an OSSClient instance. // Specify the endpoint of the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com. Specify your actual endpoint. client, err := oss.New("yourEndpoint", "", "", oss.SetCredentialsProvider(&provider)) if err != nil { fmt.Println("Error:", err) os.Exit(-1) } // Specify the name of the bucket. bucket, err := client.Bucket("yourBucketName") if err != nil { HandleError(err) os.Exit(-1) } // List all objects in the 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 sample code provides an example on how to call the GetBucketV2 (ListObjectsV2) operation to list all objects in a specific 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() { // Obtain access credentials from environment variables. Before you run the sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are configured. provider, err := oss.NewEnvironmentVariableCredentialsProvider() if err != nil { fmt.Println("Error:", err) os.Exit(-1) } // Create an OSSClient instance. // Specify the endpoint of the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com. Specify your actual endpoint. client, err := oss.New("yourEndpoint", "", "", oss.SetCredentialsProvider(&provider)) if err != nil { fmt.Println("Error:", err) os.Exit(-1) } // Specify the name of the bucket. bucket, err := client.Bucket("yourBucketName") if err != nil { HandleError(err) os.Exit(-1) } // List all objects in the 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 response is returned when you call the preceding two operations to list all objects in a specific directory:
Objects: fun/movie/001.avi fun/movie/007.avi fun/test.jpg CommonPrefixes:
List objects and subdirectories in a directory
The following sample code provides an example on how to call the GetBucket (ListObjects) operation to list objects and subdirectories in a specific 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() { // Obtain access credentials from environment variables. Before you run the sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are configured. provider, err := oss.NewEnvironmentVariableCredentialsProvider() if err != nil { fmt.Println("Error:", err) os.Exit(-1) } // Create an OSSClient instance. // Specify the endpoint of the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com. Specify your actual endpoint. client, err := oss.New("yourEndpoint", "", "", oss.SetCredentialsProvider(&provider)) if err != nil { fmt.Println("Error:", err) os.Exit(-1) } // Specify the name of the bucket. bucket, err := client.Bucket("yourBucketName") if err != nil { HandleError(err) os.Exit(-1) } // List objects and subdirectories in the 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 sample code provides an example on how to call the GetBucketV2 (ListObjectsV2) operation to list objects and subdirectories in a specific 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() { // Obtain access credentials from environment variables. Before you run the sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are configured. provider, err := oss.NewEnvironmentVariableCredentialsProvider() if err != nil { fmt.Println("Error:", err) os.Exit(-1) } // Create an OSSClient instance. // Specify the endpoint of the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com. Specify your actual endpoint. client, err := oss.New("yourEndpoint", "", "", oss.SetCredentialsProvider(&provider)) if err != nil { fmt.Println("Error:", err) os.Exit(-1) } // Specify the name of the bucket. bucket, err := client.Bucket("yourBucketName") if err != nil { HandleError(err) os.Exit(-1) } // List objects and subdirectories in the 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 response is returned when you call the preceding two operations to list objects and subdirectories in a specific directory:
Objects: fun/test.jpg CommonPrefixes: fun/movie/
List the sizes of objects in a directory
The following sample code provides an example on how to call the GetBucket (ListObjects) operation to list the sizes of objects in a specific 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() { // Obtain access credentials from environment variables. Before you run the sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are configured. provider, err := oss.NewEnvironmentVariableCredentialsProvider() if err != nil { fmt.Println("Error:", err) os.Exit(-1) } // Create an OSSClient instance. // Specify the endpoint of the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com. Specify your actual endpoint. client, err := oss.New("yourEndpoint", "", "", oss.SetCredentialsProvider(&provider)) if err != nil { fmt.Println("Error:", err) os.Exit(-1) } // Specify the name of the bucket. bucket, err := client.Bucket("yourBucketName") if err != nil { HandleError(err) os.Exit(-1) } // List the sizes of objects in the 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 sample code provides an example on how to call the GetBucketV2 (ListObjectsV2) operation to list the sizes of objects in a specific 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() { // Obtain access credentials from environment variables. Before you run the sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are configured. provider, err := oss.NewEnvironmentVariableCredentialsProvider() if err != nil { fmt.Println("Error:", err) os.Exit(-1) } // Create an OSSClient instance. // Specify the endpoint of the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com. Specify your actual endpoint. client, err := oss.New("yourEndpoint", "", "", oss.SetCredentialsProvider(&provider)) if err != nil { fmt.Println("Error:", err) os.Exit(-1) } // Query the name of the bucket. bucket, err := client.Bucket("yourBucketName") if err != nil { HandleError(err) os.Exit(-1) } // List the sizes of objects in the 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 } } }
FAQ
Can I sort objects based on the last modified time when I list the objects?
No, you cannot sort objects based on the last modified time when you list objects. If you need to sort objects based on the last modified time, we recommend that you use the data indexing feature. For more information, see Data indexing.
References
For 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 ListObjectsV2(GetBucketV2).