All Products
Search
Document Center

Object Storage Service:List objects by using OSS SDK for Go

Last Updated:Nov 15, 2024

This topic describes how to list all objects, objects whose names contain a specific prefix, and objects and subdirectories in a specific directory in an Object Storage Service (OSS) 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, endpoints and open ports.

  • 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 2.2.5 and later support the RestoreInfo response parameter.

Background information

You can call the ListObjectsV2 or ListObjects operation to list up to 1,000 objects in a bucket at a time. You can configure parameters to list objects based on different conditions. For example, you can configure parameters to list objects from a specific start position, list objects and subdirectories in a specific directory, and list objects by page. The ListObjectsV2 and ListObjects operations are different in the following aspects:

  • If you call the ListObjectsV2 operation to list objects, you can configure the fetchOwner parameter to specify whether to include the object owner information in the response.

  • By default, if you call the ListObjects operation, the object owner information is included in the response.

    Note

    To list objects in a versioning-enabled bucket, we recommend that you call the ListObjectsV2 operation.

The following tables describe the parameters that you can configure when you call the ListObjectsV2 or ListObjects operation to list objects.

ListObjectsV2

The following table describes the parameters that you can configure when you call the ListObjectsV2 operation to list objects.

Parameter

Description

prefix

The prefix that must be included in the names of the objects that you want to list.

delimiter

The character 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

  • false

For more information, see ListObjectsV2.

ListObjects

The following table describes the parameters that you can configure when you call the ListObjects operation to list objects.

Parameter

Description

delimiter

The character 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 the objects that you want to list.

maxKeys

The maximum number of objects that can be listed at a time. Maximum value: 1000. Default value: 100.

marker

The position from which the list operation starts.

For more information, see ListObjects.

Examples

ListObjectsV2

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

package main

import (
	"log"
	"time"

	"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 {
		log.Fatalf("Failed to create credentials provider: %v", err)
	}

	// 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. 
	// Specify the region in which the bucket is located. For example, if your bucket is located in the China (Hangzhou) region, set the region to cn-hangzhou.  
	client, err := oss.New("yourEndpoint", "", "", oss.SetCredentialsProvider(&provider), oss.Region("yourRegion"), oss.AuthVersion(oss.AuthV4))
	if err != nil {
		log.Fatalf("Failed to create OSS client: %v", err)
	}

	// Specify the name of the bucket. 
	bucketName := "yourBucketName" // Replace yourBucketName with the actual bucket name.
	bucket, err := client.Bucket(bucketName)
	if err != nil {
		log.Fatalf("Failed to get bucket: %v", err)
	}

	// Specify the position from which the list starts.
	continueToken := ""

	for {
		// List all objects.
		lsRes, err := bucket.ListObjectsV2(oss.ContinuationToken(continueToken))
		if err != nil {
			log.Fatalf("Failed to list objects: %v", err)
		}

		// Display the listed objects. By default, up to 100 objects are returned at a time. 
		for _, object := range lsRes.Objects {
			log.Printf("Object Key: %s, Type: %s, Size: %d, ETag: %s, LastModified: %s, StorageClass: %s\n",
				object.Key, object.Type, object.Size, object.ETag, object.LastModified.Format(time.RFC3339), object.StorageClass)
		}

		// If you want to list more objects, update the value of the continueToken parameter. 
		if lsRes.IsTruncated {
			continueToken = lsRes.NextContinuationToken
		} else {
			break
		}
	}

	log.Println("All objects have been listed.")
}

ListObjects

The following sample code provides an example on how to call the ListObjects operation to list 100 objects in a specific bucket:

package main

import (
	"log"

	"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 {
		log.Fatalf("Failed to create credentials provider: %v", err)
	}

	// 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 the region in which the bucket is located. For example, if your bucket is located in the China (Hangzhou) region, set the region to cn-hangzhou.  
	client, err := oss.New("yourEndpoint", "", "", oss.SetCredentialsProvider(&provider), oss.Region("yourRegion"), oss.AuthVersion(oss.AuthV4))
	if err != nil {
		log.Fatalf("Failed to create OSS client: %v", err)
	}

	// Specify the name of the bucket. 
	bucketName := "yourBucketName" // Replace yourBucketName with the actual bucket name.
	bucket, err := client.Bucket(bucketName)
	if err != nil {
		log.Fatalf("Failed to get bucket: %v", err)
	}

	// List all objects. 
	marker := ""
	for {
		lsRes, err := bucket.ListObjects(oss.Marker(marker))
		if err != nil {
			log.Fatalf("Failed to list objects: %v", err)
		}

		// Display the listed objects. By default, up to 100 objects are returned at a time. 
		for _, object := range lsRes.Objects {
			log.Printf("Object Name: %s\n", object.Key)
		}

		// If you want to list more objects, update the value of the marker parameter. 
		if lsRes.IsTruncated {
			marker = lsRes.NextMarker
		} else {
			break
		}
	}

	log.Println("All objects have been listed.")
}

Common scenarios

List a specific number of objects

ListObjectsV2

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

package main

import (
	"log"
	"time"

	"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 {
		log.Fatalf("Failed to create credentials provider: %v", err)
	}

	// 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 the region in which the bucket is located. For example, if your bucket is located in the China (Hangzhou) region, set the region to cn-hangzhou.  
	client, err := oss.New("yourEndpoint", "", "", oss.SetCredentialsProvider(&provider), oss.Region("yourRegion"), oss.AuthVersion(oss.AuthV4))
	if err != nil {
		log.Fatalf("Failed to create OSS client: %v", err)
	}

	// Specify the name of the bucket. 
	bucketName := "yourBucketName" // Replace yourBucketName with the actual bucket name.
	bucket, err := client.Bucket(bucketName)
	if err != nil {
		log.Fatalf("Failed to get bucket: %v", err)
	}

	// Configure the maxKeys parameter to specify the maximum number of objects that can be listed at a time and list the objects. 
	maxKeys := 200
	continueToken := ""

	for {
		lsRes, err := bucket.ListObjectsV2(oss.MaxKeys(maxKeys), oss.StartAfter(continueToken))
		if err != nil {
			log.Fatalf("Failed to list objects: %v", err)
		}

		// Display the listed objects. By default, up to 100 objects are returned at a time. 
		for _, object := range lsRes.Objects {
			log.Printf("Object Key: %s, Type: %s, Size: %d, ETag: %s, LastModified: %s, StorageClass: %s, Owner ID: %s, Owner DisplayName: %s\n",
				object.Key, object.Type, object.Size, object.ETag, object.LastModified.Format(time.RFC3339), object.StorageClass, object.Owner.ID, object.Owner.DisplayName)
		}

		// If you want to list more objects, update the value of the continueToken parameter. 
		if lsRes.IsTruncated {
			continueToken = lsRes.ContinuationToken
		} else {
			break
		}
	}

	log.Println("All objects have been listed.")
}

ListObjects

The following sample code provides an example on how to call the ListObjects operation to list a specific number of objects in a bucket:

package main

import (
	"log"

	"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 {
		log.Fatalf("Failed to create credentials provider: %v", err)
	}

	// 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 the region in which the bucket is located. For example, if your bucket is located in the China (Hangzhou) region, set the region to cn-hangzhou.  
	client, err := oss.New("yourEndpoint", "", "", oss.SetCredentialsProvider(&provider), oss.Region("yourRegion"), oss.AuthVersion(oss.AuthV4))
	if err != nil {
		log.Fatalf("Failed to create OSS client: %v", err)
	}

	// Specify the name of the bucket. 
	bucketName := "yourBucketName" // Replace yourBucketName with the actual bucket name.
	bucket, err := client.Bucket(bucketName)
	if err != nil {
		log.Fatalf("Failed to get bucket: %v", err)
	}

	// Specify the maximum number of objects that can be listed at a time and list the objects. 
	maxKeys := 200
	lsRes, err := bucket.ListObjects(oss.MaxKeys(maxKeys))
	if err != nil {
		log.Fatalf("Failed to list objects: %v", err)
	}

	// Display the listed objects. By default, up to 100 objects are returned at a time. 
	log.Printf("Found %d objects:\n", len(lsRes.Objects))
	for _, object := range lsRes.Objects {
		log.Printf("Object: %s\n", object.Key)
	}

	if lsRes.IsTruncated {
		log.Printf("More objects available. NextMarker: %s\n", lsRes.NextMarker)
	} else {
		log.Println("All objects have been listed.")
	}
}

List objects whose names contain a specific prefix

ListObjectsV2

The following sample code provides an example on how to call the ListObjectsV2 operation to list objects whose names contain a specific prefix:

package main

import (
	"log"
	"time"

	"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 {
		log.Fatalf("Failed to create credentials provider: %v", err)
	}

	// 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. 
	// Specify the region in which the bucket is located. For example, if your bucket is located in the China (Hangzhou) region, set the region to cn-hangzhou.  
	client, err := oss.New("yourEndpoint", "", "", oss.SetCredentialsProvider(&provider), oss.Region("yourRegion"), oss.AuthVersion(oss.AuthV4))
	if err != nil {
		log.Fatalf("Failed to create OSS client: %v", err)
	}

	// Specify the name of the bucket. 
	bucketName := "yourBucketName" // Replace yourBucketName with the actual bucket name.
	bucket, err := client.Bucket(bucketName)
	if err != nil {
		log.Fatalf("Failed to get bucket: %v", err)
	}

	// Set the prefix parameter to my-object-. 
	prefix := "my-object-"
	lsRes, err := bucket.ListObjectsV2(oss.Prefix(prefix))
	if err != nil {
		log.Fatalf("Failed to list objects: %v", err)
	}

	// Display the listed objects. By default, up to 100 objects are returned at a time. 
	log.Printf("Found %d objects with prefix '%s':\n", len(lsRes.Objects), prefix)
	for _, object := range lsRes.Objects {
		log.Printf("Object Key: %s, Type: %s, Size: %d, ETag: %s, LastModified: %s, StorageClass: %s, Owner ID: %s, Owner DisplayName: %s\n",
			object.Key, object.Type, object.Size, object.ETag, object.LastModified.Format(time.RFC3339), object.StorageClass, object.Owner.ID, object.Owner.DisplayName)
	}

	if lsRes.IsTruncated {
		log.Printf("More objects available. Next Continuation Token: %s\n", lsRes.NextContinuationToken)
	} else {
		log.Println("All objects have been listed.")
	}
}

ListObjects

The following sample code provides an example on how to call the ListObjects operation to list objects whose names contain a specific prefix:

package main

import (
	"log"

	"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 {
		log.Fatalf("Failed to create credentials provider: %v", err)
	}

	// 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. 
	// Specify the region in which the bucket is located. For example, if your bucket is located in the China (Hangzhou) region, set the region to cn-hangzhou.  
	client, err := oss.New("yourEndpoint", "", "", oss.SetCredentialsProvider(&provider), oss.Region("yourRegion"), oss.AuthVersion(oss.AuthV4))
	if err != nil {
		log.Fatalf("Failed to create OSS client: %v", err)
	}

	// Specify the name of the bucket. 
	bucketName := "yourBucketName" // Replace yourBucketName with the actual bucket name.
	bucket, err := client.Bucket(bucketName)
	if err != nil {
		log.Fatalf("Failed to get bucket: %v", err)
	}

	// List objects whose names contain a specific prefix. By default, up to 100 objects are listed. 
	prefix := "my-object-"
	lsRes, err := bucket.ListObjects(oss.Prefix(prefix))
	if err != nil {
		log.Fatalf("Failed to list objects: %v", err)
	}

	// Display the listed objects. By default, up to 100 objects are returned at a time. 
	log.Printf("Found %d objects with prefix '%s':\n", len(lsRes.Objects), prefix)
	for _, object := range lsRes.Objects {
		log.Printf("Object Key: %s\n", object.Key)
	}

	if lsRes.IsTruncated {
		log.Printf("More objects available. Next Marker: %s\n", lsRes.NextMarker)
	} else {
		log.Println("All objects have been listed.")
	}
}

List all objects from a specific position

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 (
	"log"
	"time"

	"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 {
		log.Fatalf("Failed to create credentials provider: %v", err)
	}

	// 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. 
	// Specify the region in which the bucket is located. For example, if your bucket is located in the China (Hangzhou) region, set the region to cn-hangzhou.  
	client, err := oss.New("yourEndpoint", "", "", oss.SetCredentialsProvider(&provider), oss.Region("yourRegion"), oss.AuthVersion(oss.AuthV4))
	if err != nil {
		log.Fatalf("Failed to create OSS client: %v", err)
	}

	// Specify the name of the bucket. 
	bucketName := "yourBucketName" // Replace yourBucketName with the actual bucket name.
	bucket, err := client.Bucket(bucketName)
	if err != nil {
		log.Fatalf("Failed to get bucket: %v", 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. 
	startAfter := "my-object-"
	lsRes, err := bucket.ListObjectsV2(oss.StartAfter(startAfter))
	if err != nil {
		log.Fatalf("Failed to list objects: %v", err)
	}

	// Display the listed objects. By default, up to 100 objects are returned at a time. 
	log.Printf("Found %d objects starting after '%s':\n", len(lsRes.Objects), startAfter)
	for _, object := range lsRes.Objects {
		log.Printf("Object Key: %s, Type: %s, Size: %d, ETag: %s, LastModified: %s, StorageClass: %s, Owner ID: %s, Owner DisplayName: %s\n",
			object.Key, object.Type, object.Size, object.ETag, object.LastModified.Format(time.RFC3339), object.StorageClass, object.Owner.ID, object.Owner.DisplayName)
	}

	if lsRes.IsTruncated {
		log.Printf("More objects available. Next Continuation Token: %s\n", lsRes.NextContinuationToken)
	} else {
		log.Println("All objects have been listed.")
	}
}

ListObjects

You can configure the Marker parameter to specify the 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 (
	"log"

	"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 {
		log.Fatalf("Failed to create credentials provider: %v", err)
	}

	// 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. 
	// Specify the region in which the bucket is located. For example, if your bucket is located in the China (Hangzhou) region, set the region to cn-hangzhou.  
	client, err := oss.New("yourEndpoint", "", "", oss.SetCredentialsProvider(&provider), oss.Region("yourRegion"), oss.AuthVersion(oss.AuthV4))
	if err != nil {
		log.Fatalf("Failed to create OSS client: %v", err)
	}

	// Specify the name of the bucket. 
	bucketName := "yourBucketName" // Replace yourBucketName with the actual bucket name.
	bucket, err := client.Bucket(bucketName)
	if err != nil {
		log.Fatalf("Failed to get bucket: %v", err)
	}

	// Specify the marker parameter.
	marker := ""

	// List all objects whose names are alphabetically after the value of the Marker parameter.
	for {
		lsRes, err := bucket.ListObjects(oss.Marker(marker))
		if err != nil {
			log.Fatalf("Failed to list objects: %v", err)
		}

		// Display the listed objects. By default, up to 100 objects are returned at a time. 
		log.Printf("Found %d objects:\n", len(lsRes.Objects))
		for _, object := range lsRes.Objects {
			log.Printf("Object: %s\n", object.Key)
		}

		// If the returned results are truncated, update the value of the marker parameter to obtain the remaining results.
		if lsRes.IsTruncated {
			marker = lsRes.NextMarker
		} else {
			break
		}
	}

	log.Println("All objects have been listed.")
}

List all objects by page

ListObjectsV2

The following sample code provides an example on how to call the 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 (
	"log"
	"time"

	"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 {
		log.Fatalf("Failed to create credentials provider: %v", err)
	}

	// 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. 
	// Specify the region in which the bucket is located. For example, if your bucket is located in the China (Hangzhou) region, set the region to cn-hangzhou.  
	client, err := oss.New("yourEndpoint", "", "", oss.SetCredentialsProvider(&provider), oss.Region("yourRegion"), oss.AuthVersion(oss.AuthV4))
	if err != nil {
		log.Fatalf("Failed to create OSS client: %v", err)
	}

	// Specify the name of the bucket. 
	bucketName := "yourBucketName" // Replace yourBucketName with the actual bucket name.
	bucket, err := client.Bucket(bucketName)
	if err != nil {
		log.Fatalf("Failed to get bucket: %v", 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 {
			log.Fatalf("Failed to list objects: %v", err)
		}

		// Display the listed objects. By default, up to 100 objects are returned at a time. 
		log.Printf("Found %d objects:\n", len(lsRes.Objects))
		for _, object := range lsRes.Objects {
			log.Printf("Object Key: %s, Type: %s, Size: %d, ETag: %s, LastModified: %s, StorageClass: %s\n",
				object.Key, object.Type, object.Size, object.ETag, object.LastModified.Format(time.RFC3339), object.StorageClass)
		}

		// If the returned results are truncated, update the value of the continueToken parameter to obtain the remaining results.
		if lsRes.IsTruncated {
			continueToken = lsRes.NextContinuationToken
		} else {
			break
		}
	}

	log.Println("All objects have been listed.")
}

ListObjects

The following sample code provides an example on how to call the 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 (
	"log"
	"time"

	"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 {
		log.Fatalf("Failed to create credentials provider: %v", err)
	}

	// 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. 
	// Specify the region in which the bucket is located. For example, if your bucket is located in the China (Hangzhou) region, set the region to cn-hangzhou.  
	client, err := oss.New("yourEndpoint", "", "", oss.SetCredentialsProvider(&provider), oss.Region("yourRegion"), oss.AuthVersion(oss.AuthV4))
	if err != nil {
		log.Fatalf("Failed to create OSS client: %v", err)
	}

	// Specify the name of the bucket. 
	bucketName := "yourBucketName" // Replace yourBucketName with the actual bucket name.
	bucket, err := client.Bucket(bucketName)
	if err != nil {
		log.Fatalf("Failed to get bucket: %v", err)
	}

	// List all objects by page. Specify that up to 100 objects can be listed on each page. 
	marker := ""
	for {
		lsRes, err := bucket.ListObjects(oss.MaxKeys(100), oss.Marker(marker))
		if err != nil {
			log.Fatalf("Failed to list objects: %v", err)
		}

		// Display the listed objects. By default, up to 100 objects are returned at a time. 
		log.Printf("Found %d objects:\n", len(lsRes.Objects))
		for _, object := range lsRes.Objects {
			log.Printf("Object Key: %s, Type: %s, Size: %d, ETag: %s, LastModified: %s, StorageClass: %s\n",
				object.Key, object.Type, object.Size, object.ETag, object.LastModified.Format(time.RFC3339), object.StorageClass)
		}

		// If the returned results are truncated, update the value of the marker parameter to obtain the remaining results.
		if lsRes.IsTruncated {
			marker = lsRes.NextMarker
		} else {
			break
		}
	}

	log.Println("All objects have been listed.")
}

List objects whose names contain a specific prefix by page

ListObjectsV2

The following sample code provides an example on how to call the 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 (
	"log"
	"time"

	"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 {
		log.Fatalf("Failed to create credentials provider: %v", err)
	}

	// 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. 
	// Specify the region in which the bucket is located. For example, if your bucket is located in the China (Hangzhou) region, set the region to cn-hangzhou.  
	client, err := oss.New("yourEndpoint", "", "", oss.SetCredentialsProvider(&provider), oss.Region("yourRegion"), oss.AuthVersion(oss.AuthV4))
	if err != nil {
		log.Fatalf("Failed to create OSS client: %v", err)
	}

	// Specify the name of the bucket. 
	bucketName := "yourBucketName" // Replace yourBucketName with the actual bucket name.
	bucket, err := client.Bucket(bucketName)
	if err != nil {
		log.Fatalf("Failed to get bucket: %v", 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 := "my-object-"
	continueToken := ""
	for {
		lsRes, err := bucket.ListObjectsV2(oss.Prefix(prefix), oss.MaxKeys(80), oss.ContinuationToken(continueToken))
		if err != nil {
			log.Fatalf("Failed to list objects: %v", err)
		}

		// Display the listed objects. By default, up to 100 objects are returned at a time. 
		log.Printf("Found %d objects with prefix '%s':\n", len(lsRes.Objects), prefix)
		for _, object := range lsRes.Objects {
			log.Printf("Object Key: %s, Type: %s, Size: %d, ETag: %s, LastModified: %s, StorageClass: %s\n",
				object.Key, object.Type, object.Size, object.ETag, object.LastModified.Format(time.RFC3339), object.StorageClass)
		}

		// If the returned results are truncated, update the value of the continueToken parameter to obtain the remaining results.
		if lsRes.IsTruncated {
			continueToken = lsRes.NextContinuationToken
		} else {
			break
		}
	}

	log.Println("All objects have been listed.")
}

ListObjects

The following sample code provides an example on how to call the 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 (
	"log"
	"time"

	"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 {
		log.Fatalf("Failed to create credentials provider: %v", err)
	}

	// 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. 
	// Specify the region in which the bucket is located. For example, if your bucket is located in the China (Hangzhou) region, set the region to cn-hangzhou.  
	client, err := oss.New("yourEndpoint", "", "", oss.SetCredentialsProvider(&provider), oss.Region("yourRegion"), oss.AuthVersion(oss.AuthV4))
	if err != nil {
		log.Fatalf("Failed to create OSS client: %v", err)
	}

	// Specify the name of the bucket. 
	bucketName := "yourBucketName" // Replace yourBucketName with the actual bucket name.
	bucket, err := client.Bucket(bucketName)
	if err != nil {
		log.Fatalf("Failed to get bucket: %v", err)
	}

	// List objects whose names contain a specific prefix by page. Specify that up to 80 objects can be listed on each page. 
	prefix := "my-object-"
	marker := ""
	for {
		lsRes, err := bucket.ListObjects(oss.MaxKeys(80), oss.Marker(marker), oss.Prefix(prefix))
		if err != nil {
			log.Fatalf("Failed to list objects: %v", err)
		}

		// Display the listed objects. By default, up to 100 objects are returned at a time. 
		log.Printf("Found %d objects with prefix '%s':\n", len(lsRes.Objects), prefix)
		for _, object := range lsRes.Objects {
			log.Printf("Object Key: %s, Type: %s, Size: %d, ETag: %s, LastModified: %s, StorageClass: %s\n",
				object.Key, object.Type, object.Size, object.ETag, object.LastModified.Format(time.RFC3339), object.StorageClass)
		}

		// If the returned results are truncated, update the value of the marker parameter to obtain the remaining results.
		if lsRes.IsTruncated {
			marker = lsRes.NextMarker
		} else {
			break
		}
	}

	log.Println("All objects have been listed.")
}

List information about all objects in a specific directory

ListObjectsV2

The following sample code provides an example on how to call the 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 (
	"log"
	"time"

	"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 {
		log.Fatalf("Failed to create credentials provider: %v", err)
	}

	// 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. 
	// Specify the region in which the bucket is located. For example, if your bucket is located in the China (Hangzhou) region, set the region to cn-hangzhou.  
	client, err := oss.New("yourEndpoint", "", "", oss.SetCredentialsProvider(&provider), oss.Region("yourRegion"), oss.AuthVersion(oss.AuthV4))
	if err != nil {
		log.Fatalf("Failed to create OSS client: %v", err)
	}

	// Specify the name of the bucket. 
	bucketName := "yourBucketName" // Replace yourBucketName with the actual bucket name.
	bucket, err := client.Bucket(bucketName)
	if err != nil {
		log.Fatalf("Failed to get bucket: %v", err)
	}

	// List objects whose names contain a specific prefix in a directory by page. By default, up to 100 objects are returned at a time. 
	continueToken := ""
	prefix := "fun"
	for {
		lsRes, err := bucket.ListObjectsV2(oss.Prefix(prefix), oss.ContinuationToken(continueToken))
		if err != nil {
			log.Fatalf("Failed to list objects: %v", err)
		}

		// Display the listed objects. 
		for _, object := range lsRes.Objects {
			log.Printf("Object Key: %s, Type: %s, Size: %d, ETag: %s, LastModified: %s, StorageClass: %s\n",
				object.Key, object.Type, object.Size, object.ETag, object.LastModified.Format(time.RFC3339), object.StorageClass)
		}

		// If the returned results are truncated, update the value of the continueToken parameter to obtain the remaining results.
		if lsRes.IsTruncated {
			continueToken = lsRes.NextContinuationToken
		} else {
			break
		}
	}

	log.Println("All objects have been listed.")
}

ListObjects

The following sample code provides an example on how to call the 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 (
	"log"
	"time"

	"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 {
		log.Fatalf("Failed to create credentials provider: %v", err)
	}

	// 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. 
	// Specify the region in which the bucket is located. For example, if your bucket is located in the China (Hangzhou) region, set the region to cn-hangzhou.  
	client, err := oss.New("yourEndpoint", "", "", oss.SetCredentialsProvider(&provider), oss.Region("yourRegion"), oss.AuthVersion(oss.AuthV4))
	if err != nil {
		log.Fatalf("Failed to create OSS client: %v", err)
	}

	// Specify the name of the bucket. 
	bucketName := "yourBucketName" // Replace yourBucketName with the actual bucket name.
	bucket, err := client.Bucket(bucketName)
	if err != nil {
		log.Fatalf("Failed to get bucket: %v", err)
	}

	// Traverse all objects. 
	marker := ""
	prefix := "test"
	for {
		lor, err := bucket.ListObjects(oss.Marker(marker), oss.Prefix(prefix))
		if err != nil {
			log.Fatalf("Failed to list objects: %v", err)
		}

		// Display the listed objects. 
		for _, object := range lor.Objects {
			log.Printf("Object Key: %s, Type: %s, Size: %d, ETag: %s, LastModified: %s, StorageClass: %s\n",
				object.Key, object.Type, object.Size, object.ETag, object.LastModified.Format(time.RFC3339), object.StorageClass)
		}

		// If the returned results are truncated, update the value of the marker parameter to obtain the remaining results.
		if lor.IsTruncated {
			marker = lor.NextMarker
		} else {
			break
		}
	}

	log.Println("All objects have been listed.")
}

List information about all subdirectories in a specific directory

ListObjectsV2

The following sample code provides an example on how to call the ListObjectsV2 operation to list information about all subdirectories in a specific directory:

package main

import (
	"log"

	"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 {
		log.Fatalf("Failed to create credentials provider: %v", err)
	}

	// 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. 
	// Specify the region in which the bucket is located. For example, if your bucket is located in the China (Hangzhou) region, set the region to cn-hangzhou.  
	client, err := oss.New("yourEndpoint", "", "", oss.SetCredentialsProvider(&provider), oss.Region("yourRegion"), oss.AuthVersion(oss.AuthV4))
	if err != nil {
		log.Fatalf("Failed to create OSS client: %v", err)
	}

	// Specify the name of the bucket. 
	bucketName := "yourBucketName" // Replace yourBucketName with the actual bucket name.
	bucket, err := client.Bucket(bucketName)
	if err != nil {
		log.Fatalf("Failed to get bucket: %v", err)
	}

	// List all subdirectories in a specific directory by page. By default, up to 100 objects are returned at a time. 
	continueToken := ""
	prefix := ""
	for {
		lsRes, err := bucket.ListObjectsV2(oss.Prefix(prefix), oss.ContinuationToken(continueToken), oss.Delimiter("/"))
		if err != nil {
			log.Fatalf("Failed to list objects: %v", err)
		}

		// Display the listed objects. 
		for _, dirName := range lsRes.CommonPrefixes {
			log.Println("Directory Name:", dirName)
		}

		// If the returned results are truncated, update the value of the continueToken parameter to obtain the remaining results.
		if lsRes.IsTruncated {
			continueToken = lsRes.NextContinuationToken
		} else {
			break
		}
	}

	log.Println("All directories have been listed.")
}

ListObjects

The following sample code provides an example on how to call the ListObjects operation to list information about all subdirectories in a specific directory:

package main

import (
	"log"

	"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 {
		log.Fatalf("Failed to create credentials provider: %v", err)
	}

	// 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. 
	// Specify the region in which the bucket is located. For example, if your bucket is located in the China (Hangzhou) region, set the region to cn-hangzhou.  
	client, err := oss.New("yourEndpoint", "", "", oss.SetCredentialsProvider(&provider), oss.Region("yourRegion"), oss.AuthVersion(oss.AuthV4))
	if err != nil {
		log.Fatalf("Failed to create OSS client: %v", err)
	}

	// Specify the name of the bucket. 
	bucketName := "yourBucketName" // Replace yourBucketName with the actual bucket name.
	bucket, err := client.Bucket(bucketName)
	if err != nil {
		log.Fatalf("Failed to get bucket: %v", err)
	}

	// List all subdirectories in a specific directory by page. By default, up to 100 objects are returned at a time. 
	marker := ""
	prefix := "yourDirPrefix" // Replace yourDirPrefix with the actual directory name.
	for {
		lor, err := bucket.ListObjects(oss.Marker(marker), oss.Prefix(prefix), oss.Delimiter("/"))
		if err != nil {
			log.Fatalf("Failed to list objects: %v", err)
		}

		// Display the listed objects. 
		for _, dirName := range lor.CommonPrefixes {
			log.Println("Directory Name:", dirName)
		}

		// If the returned results are truncated, update the value of the marker parameter to obtain the remaining results.
		if lor.IsTruncated {
			marker = lor.NextMarker
		} else {
			break
		}
	}

	log.Println("All directories have been listed.")
}

List objects and their owner information

The following sample code provides an example on how to call the ListObjectsV2 operation to list objects and their owner information:

package main

import (
	"log"

	"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 {
		log.Fatalf("Failed to create credentials provider: %v", err)
	}

	// 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. 
	// Specify the region in which the bucket is located. For example, if your bucket is located in the China (Hangzhou) region, set the region to cn-hangzhou.  
	client, err := oss.New("yourEndpoint", "", "", oss.SetCredentialsProvider(&provider), oss.Region("yourRegion"), oss.AuthVersion(oss.AuthV4))
	if err != nil {
		log.Fatalf("Failed to create OSS client: %v", err)
	}

	// Specify the name of the bucket. 
	bucketName := "yourBucketName" // Replace yourBucketName with the actual bucket name.
	bucket, err := client.Bucket(bucketName)
	if err != nil {
		log.Fatalf("Failed to get bucket: %v", err)
	}

	// Obtain the object owner information and list the objects. 
	lsRes, err := bucket.ListObjectsV2(oss.FetchOwner(true))
	if err != nil {
		log.Fatalf("Failed to list objects with owner information: %v", err)
	}

	// Display the listed objects. By default, up to 100 objects are returned at a time. 
	for _, object := range lsRes.Objects {
		log.Printf("Object Key: %s, Owner ID: %s, Display Name: %s\n",
			object.Key, object.Owner.ID, object.Owner.DisplayName)
	}

	log.Println("All objects have been listed.")
}

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 that is used to create 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.

For example, 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

    • ListObjectsV2

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

      package main
      
      import (
      	"log"
      
      	"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 {
      		log.Fatalf("Failed to create credentials provider: %v", err)
      	}
      
      	// 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. 
      	// Specify the region in which the bucket is located. For example, if your bucket is located in the China (Hangzhou) region, set the region to cn-hangzhou.  
      	client, err := oss.New("yourEndpoint", "", "", oss.SetCredentialsProvider(&provider), oss.Region("yourRegion"), oss.AuthVersion(oss.AuthV4))
      	if err != nil {
      		log.Fatalf("Failed to create OSS client: %v", err)
      	}
      
      	// Specify the name of the bucket. 
      	bucketName := "yourBucketName" // Replace yourBucketName with the actual bucket name.
      	bucket, err := client.Bucket(bucketName)
      	if err != nil {
      		log.Fatalf("Failed to get bucket: %v", err)
      	}
      
      	// List all objects in the bucket. 
      	startAfter := ""
      	continueToken := ""
      	for {
      		lsRes, err := bucket.ListObjectsV2(oss.StartAfter(startAfter), oss.ContinuationToken(continueToken))
      		if err != nil {
      			log.Fatalf("Failed to list objects: %v", err)
      		}
      
      		// Display the listed objects. By default, up to 100 objects are returned at a time. 
      		for _, object := range lsRes.Objects {
      			log.Printf("Object Key: %s\n", object.Key)
      		}
      
      		// If the returned results are truncated, update the value of the continueToken parameter to obtain the remaining results.
      		if lsRes.IsTruncated {
      			startAfter = lsRes.StartAfter
      			continueToken = lsRes.NextContinuationToken
      		} else {
      			break
      		}
      	}
      
      	log.Println("All objects have been listed.")
      }
      

      ListObjects

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

      package main
      
      import (
      	"log"
      
      	"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 {
      		log.Fatalf("Failed to create credentials provider: %v", err)
      	}
      
      	// 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. 
      	// Specify the region in which the bucket is located. For example, if your bucket is located in the China (Hangzhou) region, set the region to cn-hangzhou.  
      	client, err := oss.New("yourEndpoint", "", "", oss.SetCredentialsProvider(&provider), oss.Region("yourRegion"), oss.AuthVersion(oss.AuthV4))
      	if err != nil {
      		log.Fatalf("Failed to create OSS client: %v", err)
      	}
      
      	// Specify the name of the bucket. 
      	bucketName := "yourBucketName" // Replace yourBucketName with the actual bucket name.
      	bucket, err := client.Bucket(bucketName)
      	if err != nil {
      		log.Fatalf("Failed to get bucket: %v", err)
      	}
      
      	// List all objects in the bucket. 
      	marker := ""
      	for {
      		lsRes, err := bucket.ListObjects(oss.Marker(marker))
      		if err != nil {
      			log.Fatalf("Failed to list objects: %v", err)
      		}
      
      		// Display the listed objects. By default, up to 100 objects are returned at a time. 
      		for _, object := range lsRes.Objects {
      			log.Printf("Object Key: %s\n", object.Key)
      		}
      
      		// If the returned results are truncated, update the value of the marker parameter to obtain the remaining results.
      		if lsRes.IsTruncated {
      			marker = lsRes.NextMarker
      		} else {
      			break
      		}
      	}
      
      	log.Println("All objects have been listed.")
      }
      
    • The following response is returned when you call the preceding two operations to list all objects in a bucket:

      Objects:
      fun/movie/001.avi
      fun/movie/007.avi
      fun/test.jpg
      oss.jpg
      CommonPrefixes:  
  • List all objects in a directory

    • ListObjectsV2

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

      package main
      
      import (
      	"log"
      
      	"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 {
      		log.Fatalf("Failed to create credentials provider: %v", err)
      	}
      
      	// 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. 
      	// Specify the region in which the bucket is located. For example, if your bucket is located in the China (Hangzhou) region, set the region to cn-hangzhou.  
      	client, err := oss.New("yourEndpoint", "", "", oss.SetCredentialsProvider(&provider), oss.Region("yourRegion"), oss.AuthVersion(oss.AuthV4))
      	if err != nil {
      		log.Fatalf("Failed to create OSS client: %v", err)
      	}
      
      	// Specify the name of the bucket. 
      	bucketName := "yourBucketName" // Replace yourBucketName with the actual bucket name.
      	bucket, err := client.Bucket(bucketName)
      	if err != nil {
      		log.Fatalf("Failed to get bucket: %v", err)
      	}
      
      	// List all objects in the directory. 
      	prefix := "aaa/db-init"
      	continueToken := ""
      	for {
      		lsRes, err := bucket.ListObjectsV2(oss.Prefix(prefix), oss.ContinuationToken(continueToken))
      		if err != nil {
      			log.Fatalf("Failed to list objects: %v", err)
      		}
      
      		// Display the listed objects. By default, up to 100 objects are returned at a time. 
      		for _, object := range lsRes.Objects {
      			log.Printf("Object Key: %s\n", object.Key)
      		}
      
      		// If the returned results are truncated, update the value of the continueToken parameter to obtain the remaining results.
      		if lsRes.IsTruncated {
      			continueToken = lsRes.NextContinuationToken
      		} else {
      			break
      		}
      	}
      
      	log.Println("All objects have been listed.")
      }
      

      ListObjects

      The following sample code provides an example on how to call the ListObjects operation to list all objects in a specific directory:

      package main
      
      import (
      	"log"
      
      	"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 {
      		log.Fatalf("Failed to create credentials provider: %v", err)
      	}
      
      	// 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. 
      	// Specify the region in which the bucket is located. For example, if your bucket is located in the China (Hangzhou) region, set the region to cn-hangzhou.  
      	client, err := oss.New("yourEndpoint", "", "", oss.SetCredentialsProvider(&provider), oss.Region("yourRegion"), oss.AuthVersion(oss.AuthV4))
      	if err != nil {
      		log.Fatalf("Failed to create OSS client: %v", err)
      	}
      
      	// Specify the name of the bucket. 
      	bucketName := "yourBucketName" // Replace yourBucketName with the actual bucket name.
      	bucket, err := client.Bucket(bucketName)
      	if err != nil {
      		log.Fatalf("Failed to get bucket: %v", err)
      	}
      
      	// List all objects in the directory. 
      	prefix := "aaa/db-init" // Replace aaa/db-init with the actual directory name.
      	marker := ""
      	for {
      		lsRes, err := bucket.ListObjects(oss.Prefix(prefix), oss.Marker(marker))
      		if err != nil {
      			log.Fatalf("Failed to list objects: %v", err)
      		}
      
      		// Display the listed objects. By default, up to 100 objects are returned at a time. 
      		for _, object := range lsRes.Objects {
      			log.Printf("Object Key: %s\n", object.Key)
      		}
      
      		// If the returned results are truncated, update the value of the marker parameter to obtain the remaining results.
      		if lsRes.IsTruncated {
      			marker = lsRes.NextMarker
      		} else {
      			break
      		}
      	}
      
      	log.Println("All objects have been listed.")
      }
      
    • 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 specific directory

    • ListObjectsV2

      The following sample code provides an example on how to call the ListObjectsV2 operation to list objects and subdirectories in a specific directory:

      package main
      
      import (
      	"log"
      
      	"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 {
      		log.Fatalf("Failed to create credentials provider: %v", err)
      	}
      
      	// 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. 
      	// Specify the region in which the bucket is located. For example, if your bucket is located in the China (Hangzhou) region, set the region to cn-hangzhou.  
      	client, err := oss.New("yourEndpoint", "", "", oss.SetCredentialsProvider(&provider), oss.Region("yourRegion"), oss.AuthVersion(oss.AuthV4))
      	if err != nil {
      		log.Fatalf("Failed to create OSS client: %v", err)
      	}
      
      	// Specify the name of the bucket. 
      	bucketName := "yourBucketName" // Replace yourBucketName with the actual bucket name.
      	bucket, err := client.Bucket(bucketName)
      	if err != nil {
      		log.Fatalf("Failed to get bucket: %v", err)
      	}
      
      	// List objects and subdirectories in the directory. 
      	prefix := "" // Specify the name of the directory based on your business requirements.
      	delimiter := "/"
      	continueToken := ""
      	for {
      		lsRes, err := bucket.ListObjectsV2(oss.Prefix(prefix), oss.Delimiter(delimiter), oss.ContinuationToken(continueToken))
      		if err != nil {
      			log.Fatalf("Failed to list objects: %v", err)
      		}
      
      		// Display the listed objects. By default, up to 100 objects are returned at a time. 
      		for _, object := range lsRes.Objects {
      			log.Printf("Object Key: %s\n", object.Key)
      		}
      
      		// Display the subdirectories. 
      		for _, commonPrefix := range lsRes.CommonPrefixes {
      			log.Printf("Common Prefix: %s\n", commonPrefix)
      		}
      
      		// If the returned results are truncated, update the value of the continueToken parameter to obtain the remaining results.
      		if lsRes.IsTruncated {
      			continueToken = lsRes.NextContinuationToken
      		} else {
      			break
      		}
      	}
      
      	log.Println("All objects and subdirectories have been listed.")
      }
      

      ListObjects

      The following sample code provides an example on how to call the ListObjects operation to list objects and subdirectories in a specific directory:

      package main
      
      import (
      	"log"
      
      	"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 {
      		log.Fatalf("Failed to create credentials provider: %v", err)
      	}
      
      	// 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. 
      	// Specify the region in which the bucket is located. For example, if your bucket is located in the China (Hangzhou) region, set the region to cn-hangzhou.  
      	client, err := oss.New("yourEndpoint", "", "", oss.SetCredentialsProvider(&provider), oss.Region("yourRegion"), oss.AuthVersion(oss.AuthV4))
      	if err != nil {
      		log.Fatalf("Failed to create OSS client: %v", err)
      	}
      
      	// Specify the name of the bucket. 
      	bucketName := "yourBucketName" // Replace yourBucketName with the actual bucket name.
      	bucket, err := client.Bucket(bucketName)
      	if err != nil {
      		log.Fatalf("Failed to get bucket: %v", err)
      	}
      
      	// List objects and subdirectories in the directory. 
      	prefix := "aaa/db-init" // Replace aaa/db-init with the actual directory name.
      	marker := ""
      	delimiter := "/"
      	for {
      		lsRes, err := bucket.ListObjects(oss.Prefix(prefix), oss.Marker(marker), oss.Delimiter(delimiter))
      		if err != nil {
      			log.Fatalf("Failed to list objects: %v", err)
      		}
      
      		// Display the listed objects.
      		for _, object := range lsRes.Objects {
      			log.Printf("Object Key: %s\n", object.Key)
      		}
      
      		// Display the listed subdirectories.
      		for _, commonPrefix := range lsRes.CommonPrefixes {
      			log.Printf("Common Prefix: %s\n", commonPrefix)
      		}
      
      		// If the returned results are truncated, update the value of the marker parameter to obtain the remaining results.
      		if lsRes.IsTruncated {
      			marker = lsRes.NextMarker
      		} else {
      			break
      		}
      	}
      
      	log.Println("All objects and subdirectories have been listed.")
      }
      
    • 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 specific directory

    • ListObjectsV2

      The following sample code provides an example on how to call the ListObjectsV2 operation to list the sizes of objects in a specific directory:

      package main
      
      import (
      	"log"
      
      	"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 {
      		log.Fatalf("Failed to create credentials provider: %v", err)
      	}
      
      	// 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. 
      	// Specify the region in which the bucket is located. For example, if your bucket is located in the China (Hangzhou) region, set the region to cn-hangzhou.  
      	client, err := oss.New("yourEndpoint", "", "", oss.SetCredentialsProvider(&provider), oss.Region("yourRegion"), oss.AuthVersion(oss.AuthV4))
      	if err != nil {
      		log.Fatalf("Failed to create OSS client: %v", err)
      	}
      
      	// Specify the name of the bucket. 
      	bucketName := "yourBucketName" // Replace yourBucketName with the actual bucket name.
      	bucket, err := client.Bucket(bucketName)
      	if err != nil {
      		log.Fatalf("Failed to get bucket: %v", err)
      	}
      
      	// List the sizes of the objects in a specific directory. 
      	prefix := "/fun" // Replace /fun with the actual directory name.
      	continueToken := ""
      	for {
      		lsRes, err := bucket.ListObjectsV2(oss.Prefix(prefix), oss.ContinuationToken(continueToken))
      		if err != nil {
      			log.Fatalf("Failed to list objects: %v", err)
      		}
      
      		// Display the listed objects and their sizes.
      		for _, object := range lsRes.Objects {
      			log.Printf("Object Key: %s, Size: %d Byte(s)\n", object.Key, object.Size)
      		}
      
      		// If the returned results are truncated, update the value of the continueToken parameter to obtain the remaining results.
      		if lsRes.IsTruncated {
      			continueToken = lsRes.NextContinuationToken
      		} else {
      			break
      		}
      	}
      
      	log.Println("All objects have been listed with their sizes.")
      }
      

      ListObjects

      The following sample code provides an example on how to call the ListObjects operation to list the sizes of objects in a specific directory:

      package main
      
      import (
      	"log"
      
      	"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 {
      		log.Fatalf("Failed to create credentials provider: %v", err)
      	}
      
      	// 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. 
      	// Specify the region in which the bucket is located. For example, if your bucket is located in the China (Hangzhou) region, set the region to cn-hangzhou.  
      	client, err := oss.New("yourEndpoint", "", "", oss.SetCredentialsProvider(&provider), oss.Region("yourRegion"), oss.AuthVersion(oss.AuthV4))
      	if err != nil {
      		log.Fatalf("Failed to create OSS client: %v", err)
      	}
      
      	// Specify the name of the bucket. 
      	bucketName := "yourBucketName" // Replace yourBucketName with the actual bucket name.
      	bucket, err := client.Bucket(bucketName)
      	if err != nil {
      		log.Fatalf("Failed to get bucket: %v", err)
      	}
      
      	// List the sizes of the objects in a specific directory. 
      	prefix := "test/" // Replace test/ with the actual directory name.
      	marker := ""
      	for {
      		lsRes, err := bucket.ListObjects(oss.Prefix(prefix), oss.Marker(marker))
      		if err != nil {
      			log.Fatalf("Failed to list objects: %v", err)
      		}
      
      		// Display the listed objects and their sizes.
      		for _, object := range lsRes.Objects {
      			log.Printf("Object Key: %s, Size: %d Byte(s)\n", object.Key, object.Size)
      		}
      
      		// If the returned results are truncated, update the value of the marker parameter to obtain the remaining results.
      		if lsRes.IsTruncated {
      			marker = lsRes.NextMarker
      		} else {
      			break
      		}
      	}
      
      	log.Println("All objects have been listed with their sizes.")
      }
      

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 ListObjectsV2 and ListObjects.