Tüm Ürünler
Search
Belge Merkezi

Object Storage Service:List objects (Go SDK V1)

Son Güncelleme:Oct 01, 2026

Bu konuda, Object Storage Service (OSS) paketindeki tüm nesnelerin, belirli bir ön eke sahip nesnelerin ve belirli bir dizindeki nesneler ile alt dizinlerin nasıl listeleneceği açıklanmaktadır.

Notlar

  • Bu konuda Çin (Hangzhou) bölgesinin genel uç noktası kullanılmıştır. OSS ile aynı bölgedeki diğer Alibaba Cloud hizmetlerinden OSS'ye erişmek istiyorsanız dahili bir uç nokta kullanın. OSS bölgeleri ve uç noktaları hakkında daha fazla bilgi için bkz. Bölgeler ve uç noktalar.

  • Bu konuda erişim kimlik bilgileri ortam değişkenlerinden alınır. Erişim kimlik bilgilerinin nasıl yapılandırılacağı hakkında daha fazla bilgi için bkz. Erişim kimlik bilgilerini yapılandırma.

  • Bu konu, bir OSS uç noktası kullanarak bir OSSClient örneği oluşturmayı gösterir. Özel bir etki alanı kullanmak veya Security Token Service (STS) kimlik bilgileriyle kimlik doğrulaması yapmak gibi alternatif yapılandırmalar için bkz. İstemciyi yapılandırma (Go SDK V1).

  • Nesneleri listelemek için oss:ListObjects iznine sahip olmanız gerekir. Daha fazla bilgi için bkz. Özel ilke atama.

  • Go için OSS SDK 2.2.5 ve sonraki sürümleri RestoreInfo bilgilerinin döndürülmesini destekler.

Arka plan bilgileri

Bir paketteki en fazla 1.000 nesneyi tek seferde listelemek için ListObjectsV2 veya ListObjects işlemini çağırabilirsiniz. Farklı parametreler belirterek belirtilen bir başlangıç konumundan sonra tüm nesneleri listeleme, belirtilen bir dizindeki nesneleri ve alt dizinleri listeleme ve listeleme sonuçlarını sayfalandırma gibi çeşitli listeleme özelliklerini uygulayabilirsiniz. Bu iki işlem arasındaki temel farklar aşağıdaki gibidir:

  • ListObjectsV2 işlemini kullandığınızda, sonuçlara nesnelerin Owner bilgisini dahil edip etmeyeceğinizi belirtmek için fetchOwner parametresini kullanmanız gerekir.

  • ListObjects işlemini kullandığınızda, nesnelerin Owner bilgisi varsayılan olarak sonuçlara dahil edilir.

    Not

    Sürüm oluşturmanın etkinleştirildiği paketlerde nesneleri listelemek için ListObjectsV2 işlemini kullanmanız gerekir.

Aşağıdaki bölümlerde ListObjectsV2 ve ListObjects yöntemlerini kullanarak nesneleri listelemeye yönelik parametreler açıklanmaktadır.

ListObjectsV2

Aşağıdaki tabloda ListObjectsV2 yöntemini kullanarak nesneleri listelemeye yönelik parametreler açıklanmaktadır.

Parametre

Açıklama

prefix

Döndürülen nesne adlarının içermesi gereken ön ek.

delimiter

Nesne adlarını gruplamak için kullanılan bir karakter. Ön ekten delimiter öğesinin ilk oluşumuna kadar aynı dizeyi içeren tüm nesne adları tek bir öğe (commonPrefixes) olarak gruplanır.

startAfter

Bu listeleme işlemi için başlangıç noktası.

fetchOwner

Sonuçlara Owner bilgisinin dahil edilip edilmeyeceğini belirtir.

  • true: Owner bilgisi sonuçlara dahil edilir.

  • false: Owner bilgisi sonuçlara dahil edilmez.

Daha fazla bilgi için bkz. ListObjectsV2.

ListObjects

Aşağıdaki tabloda ListObjects yöntemini kullanarak nesneleri listelemeye yönelik parametreler açıklanmaktadır.


Parametre


Açıklama


delimiter


Nesne adlarını gruplamak için kullanılan bir karakter. Ön ekten delimiter öğesinin ilk oluşumuna kadar aynı dizeyi içeren tüm nesne adları tek bir öğe (commonPrefixes) olarak gruplanır.


prefix


Döndürülen nesne adlarının içermesi gereken ön ek.


maxKeys


Döndürülecek maksimum nesne sayısı. Varsayılan değer 100'dür. Maksimum değer 1000'dir.


marker


Bu listeleme işlemi için başlangıç noktası.

Daha fazla bilgi için bkz. ListObjects.

Örnekler

ListObjectsV2

Aşağıdaki örnek kod, bir paketteki 100 nesneyi listelemek için ListObjectsV2 yönteminin nasıl kullanılacağını gösterir.

package main

import (
	"log"
	"time"

	"github.com/aliyun/aliyun-oss-go-sdk/oss"
)

func main() {
	// Obtain access credentials from environment variables. Before you run this sample code, ensure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are set.
	provider, err := oss.NewEnvironmentVariableCredentialsProvider()
	if err != nil {
		log.Fatalf("Failed to create credentials provider: %v", err)
	}

	// Create an OSSClient instance.
	// Set yourEndpoint to the endpoint of the bucket. For example, if the bucket is in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com. For other regions, use the actual endpoint.
	// Set yourRegion to the region of the bucket. For example, if the bucket is in the China (Hangzhou) region, set the region to cn-hangzhou. For other regions, use the actual region.
	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 bucket name.
	bucketName := "yourBucketName" // Replace with the actual bucket name.
	bucket, err := client.Bucket(bucketName)
	if err != nil {
		log.Fatalf("Failed to get bucket: %v", err)
	}

	// The initial continuation token.
	continueToken := ""

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

		// Print the results. By default, 100 records 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 more objects need to be listed, update the continuation token and continue the loop.
		if lsRes.IsTruncated {
			continueToken = lsRes.NextContinuationToken
		} else {
			break
		}
	}

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

ListObjects

Aşağıdaki örnek kod, bir paketteki 100 nesneyi listelemek için ListObjects yönteminin nasıl kullanılacağını gösterir.

package main

import (
	"log"

	"github.com/aliyun/aliyun-oss-go-sdk/oss"
)

func main() {
	// Obtain access credentials from environment variables. Before you run this sample code, ensure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are set.
	provider, err := oss.NewEnvironmentVariableCredentialsProvider()
	if err != nil {
		log.Fatalf("Failed to create credentials provider: %v", err)
	}

	// Create an OSSClient instance.
	// Set yourEndpoint to the endpoint of the bucket. For example, if the bucket is in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com. For other regions, use the actual endpoint.
	// Set yourRegion to the region of the bucket. For example, if the bucket is in the China (Hangzhou) region, set the region to cn-hangzhou. For other regions, use the actual region.
	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 bucket name.
	bucketName := "yourBucketName" // Replace 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)
		}

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

		// If more objects need to be listed, update the marker and continue the loop.
		if lsRes.IsTruncated {
			marker = lsRes.NextMarker
		} else {
			break
		}
	}

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

Yaygın senaryolar

Belirli sayıda nesneyi listeleme

ListObjectsV2

Aşağıdaki örnek kod, belirli sayıda nesneyi listelemek için ListObjectsV2 yönteminin nasıl kullanılacağını gösterir.

package main

import (
	"log"
	"time"

	"github.com/aliyun/aliyun-oss-go-sdk/oss"
)

func main() {
	// Obtain access credentials from environment variables. Before you run this sample code, ensure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are set.
	provider, err := oss.NewEnvironmentVariableCredentialsProvider()
	if err != nil {
		log.Fatalf("Failed to create credentials provider: %v", err)
	}

	// Create an OSSClient instance.
	// Set yourEndpoint to the endpoint of the bucket. For example, if the bucket is in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com. For other regions, use the actual endpoint.
	// Set yourRegion to the region of the bucket. For example, if the bucket is in the China (Hangzhou) region, set the region to cn-hangzhou. For other regions, use the actual region.
	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 bucket name.
	bucketName := "yourBucketName" // Replace with the actual bucket name.
	bucket, err := client.Bucket(bucketName)
	if err != nil {
		log.Fatalf("Failed to get bucket: %v", err)
	}

	// Set MaxKeys to specify the maximum number of objects to list, and then 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)
		}

		// Print the results. By default, 100 records 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 more objects need to be listed, update the continuation token and continue the loop.
		if lsRes.IsTruncated {
			continueToken = lsRes.ContinuationToken
		} else {
			break
		}
	}

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

ListObjects

Aşağıdaki örnek kod, belirli sayıda nesneyi listelemek için ListObjects yönteminin nasıl kullanılacağını gösterir.

package main

import (
	"log"

	"github.com/aliyun/aliyun-oss-go-sdk/oss"
)

func main() {
	// Obtain access credentials from environment variables. Before you run this sample code, ensure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are set.
	provider, err := oss.NewEnvironmentVariableCredentialsProvider()
	if err != nil {
		log.Fatalf("Failed to create credentials provider: %v", err)
	}

	// Create an OSSClient instance.
	// Set yourEndpoint to the endpoint of the bucket. For example, if the bucket is in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com. For other regions, use the actual endpoint.
	// Set yourRegion to the region of the bucket. For example, if the bucket is in the China (Hangzhou) region, set the region to cn-hangzhou. For other regions, use the actual region.
	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 bucket name.
	bucketName := "yourBucketName" // Replace with the actual bucket name.
	bucket, err := client.Bucket(bucketName)
	if err != nil {
		log.Fatalf("Failed to get bucket: %v", err)
	}

	// Set the maximum number of objects to list, and then list the objects.
	maxKeys := 200
	lsRes, err := bucket.ListObjects(oss.MaxKeys(maxKeys))
	if err != nil {
		log.Fatalf("Failed to list objects: %v", err)
	}

	// Print the results. By default, 100 records 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.")
	}
}

Belirli bir ön eke sahip nesneleri listeleme

ListObjectsV2

Aşağıdaki örnek kod, belirli bir ön eke sahip nesneleri listelemek için ListObjectsV2 yönteminin nasıl kullanılacağını gösterir.

package main

import (
	"log"
	"time"

	"github.com/aliyun/aliyun-oss-go-sdk/oss"
)

func main() {
	// Obtain access credentials from environment variables. Before you run this sample code, ensure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are set.
	provider, err := oss.NewEnvironmentVariableCredentialsProvider()
	if err != nil {
		log.Fatalf("Failed to create credentials provider: %v", err)
	}

	// Create an OSSClient instance.
	// Set yourEndpoint to the endpoint of the bucket. For example, if the bucket is in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com. For other regions, use the actual endpoint.
	// Set yourRegion to the region of the bucket. For example, if the bucket is in the China (Hangzhou) region, set the region to cn-hangzhou. For other regions, use the actual region.
	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 bucket name.
	bucketName := "yourBucketName" // Replace 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 list objects whose names start with my-object-.
	prefix := "my-object-"
	lsRes, err := bucket.ListObjectsV2(oss.Prefix(prefix))
	if err != nil {
		log.Fatalf("Failed to list objects: %v", err)
	}

	// Print the results. By default, 100 records 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

Aşağıdaki örnek kod, belirli bir ön eke sahip nesneleri listelemek için ListObjects yönteminin nasıl kullanılacağını gösterir.

package main

import (
	"log"

	"github.com/aliyun/aliyun-oss-go-sdk/oss"
)

func main() {
	// Obtain access credentials from environment variables. Before you run this sample code, ensure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are set.
	provider, err := oss.NewEnvironmentVariableCredentialsProvider()
	if err != nil {
		log.Fatalf("Failed to create credentials provider: %v", err)
	}

	// Create an OSSClient instance.
	// Set yourEndpoint to the endpoint of the bucket. For example, if the bucket is in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com. For other regions, use the actual endpoint.
	// Set yourRegion to the region of the bucket. For example, if the bucket is in the China (Hangzhou) region, set the region to cn-hangzhou. For other regions, use the actual region.
	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 bucket name.
	bucketName := "yourBucketName" // Replace with the actual bucket name.
	bucket, err := client.Bucket(bucketName)
	if err != nil {
		log.Fatalf("Failed to get bucket: %v", err)
	}

	// List objects with the specified prefix. By default, 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)
	}

	// Print the results. By default, 100 records 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.")
	}
}

Belirtilen bir başlangıç noktasından sonraki nesneleri listeleme

ListObjectsV2

Listeleme için başlangıç noktasını belirtmek üzere StartAfter parametresini ayarlayabilirsiniz. StartAfter değerinden sonra sözlük sırasına göre gelen tüm nesneler döndürülür.

package main

import (
	"log"
	"time"

	"github.com/aliyun/aliyun-oss-go-sdk/oss"
)

func main() {
	// Obtain access credentials from environment variables. Before you run this sample code, ensure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are set.
	provider, err := oss.NewEnvironmentVariableCredentialsProvider()
	if err != nil {
		log.Fatalf("Failed to create credentials provider: %v", err)
	}

	// Create an OSSClient instance.
	// Set yourEndpoint to the endpoint of the bucket. For example, if the bucket is in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com. For other regions, use the actual endpoint.
	// Set yourRegion to the region of the bucket. For example, if the bucket is in the China (Hangzhou) region, set the region to cn-hangzhou. For other regions, use the actual region.
	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 bucket name.
	bucketName := "yourBucketName" // Replace with the actual bucket name.
	bucket, err := client.Bucket(bucketName)
	if err != nil {
		log.Fatalf("Failed to get bucket: %v", err)
	}

	// Specify to list objects after StartAfter. By default, 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)
	}

	// Print the results. By default, 100 records 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

Listeleme için başlangıç noktasını belirtmek üzere Marker parametresini ayarlayabilirsiniz. Marker değerinden sonra sözlük sırasına göre gelen tüm nesneler döndürülür.

package main

import (
	"log"

	"github.com/aliyun/aliyun-oss-go-sdk/oss"
)

func main() {
	// Obtain access credentials from environment variables. Before you run this sample code, ensure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are set.
	provider, err := oss.NewEnvironmentVariableCredentialsProvider()
	if err != nil {
		log.Fatalf("Failed to create credentials provider: %v", err)
	}

	// Create an OSSClient instance.
	// Set yourEndpoint to the endpoint of the bucket. For example, if the bucket is in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com. For other regions, use the actual endpoint.
	// Set yourRegion to the region of the bucket. For example, if the bucket is in the China (Hangzhou) region, set the region to cn-hangzhou. For other regions, use the actual region.
	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 bucket name.
	bucketName := "yourBucketName" // Replace with the actual bucket name.
	bucket, err := client.Bucket(bucketName)
	if err != nil {
		log.Fatalf("Failed to get bucket: %v", err)
	}

	// Initialize the marker.
	marker := ""

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

		// Print the results. By default, 100 records 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 result is truncated, update the marker and continue the loop.
		if lsRes.IsTruncated {
			marker = lsRes.NextMarker
		} else {
			break
		}
	}

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

Tüm nesneleri sayfa sayfa listeleme

ListObjectsV2

Bir paketteki tüm nesneleri sayfa sayfa listelemek için ListObjectsV2 yöntemini kullanabilirsiniz. Her sayfada listelenecek nesne sayısını belirtmek için MaxKeys değerini ayarlayabilirsiniz.

package main

import (
	"log"
	"time"

	"github.com/aliyun/aliyun-oss-go-sdk/oss"
)

func main() {
	// Obtain access credentials from environment variables. Before you run this sample code, ensure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are set.
	provider, err := oss.NewEnvironmentVariableCredentialsProvider()
	if err != nil {
		log.Fatalf("Failed to create credentials provider: %v", err)
	}

	// Create an OSSClient instance.
	// Set yourEndpoint to the endpoint of the bucket. For example, if the bucket is in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com. For other regions, use the actual endpoint.
	// Set yourRegion to the region of the bucket. For example, if the bucket is in the China (Hangzhou) region, set the region to cn-hangzhou. For other regions, use the actual region.
	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 bucket name.
	bucketName := "yourBucketName" // Replace 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. List 100 objects per page.
	continueToken := ""
	for {
		lsRes, err := bucket.ListObjectsV2(oss.MaxKeys(100), oss.ContinuationToken(continueToken))
		if err != nil {
			log.Fatalf("Failed to list objects: %v", err)
		}

		// Print the results. By default, 100 records 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 result is truncated, update the continuation token and continue the loop.
		if lsRes.IsTruncated {
			continueToken = lsRes.NextContinuationToken
		} else {
			break
		}
	}

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

ListObjects

Bir paketteki tüm nesneleri sayfa sayfa listelemek için ListObjects yöntemini kullanabilirsiniz. Her sayfada listelenecek nesne sayısını belirtmek için MaxKeys değerini ayarlayabilirsiniz.

package main

import (
	"log"
	"time"

	"github.com/aliyun/aliyun-oss-go-sdk/oss"
)

func main() {
	// Obtain access credentials from environment variables. Before you run this sample code, ensure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are set.
	provider, err := oss.NewEnvironmentVariableCredentialsProvider()
	if err != nil {
		log.Fatalf("Failed to create credentials provider: %v", err)
	}

	// Create an OSSClient instance.
	// Set yourEndpoint to the endpoint of the bucket. For example, if the bucket is in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com. For other regions, use the actual endpoint.
	// Set yourRegion to the region of the bucket. For example, if the bucket is in the China (Hangzhou) region, set the region to cn-hangzhou. For other regions, use the actual region.
	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 bucket name.
	bucketName := "yourBucketName" // Replace 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. List 100 objects per page.
	marker := ""
	for {
		lsRes, err := bucket.ListObjects(oss.MaxKeys(100), oss.Marker(marker))
		if err != nil {
			log.Fatalf("Failed to list objects: %v", err)
		}

		// Print the results. By default, 100 records 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 result is truncated, update the continuation token and continue the loop.
		if lsRes.IsTruncated {
			marker = lsRes.NextMarker
		} else {
			break
		}
	}

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

Belirli bir ön eke sahip nesneleri sayfa sayfa listeleme

ListObjectsV2

Belirli bir ön eke sahip nesneleri sayfa sayfa listelemek için ListObjectsV2 yöntemini kullanabilirsiniz. Her sayfada listelenecek nesne sayısını belirtmek için MaxKeys değerini ayarlayabilirsiniz.

package main

import (
	"log"
	"time"

	"github.com/aliyun/aliyun-oss-go-sdk/oss"
)

func main() {
	// Obtain access credentials from environment variables. Before you run this sample code, ensure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are set.
	provider, err := oss.NewEnvironmentVariableCredentialsProvider()
	if err != nil {
		log.Fatalf("Failed to create credentials provider: %v", err)
	}

	// Create an OSSClient instance.
	// Set yourEndpoint to the endpoint of the bucket. For example, if the bucket is in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com. For other regions, use the actual endpoint.
	// Set yourRegion to the region of the bucket. For example, if the bucket is in the China (Hangzhou) region, set the region to cn-hangzhou. For other regions, use the actual region.
	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 bucket name.
	bucketName := "yourBucketName" // Replace with the actual bucket name.
	bucket, err := client.Bucket(bucketName)
	if err != nil {
		log.Fatalf("Failed to get bucket: %v", err)
	}

	// List objects with a specific prefix by page. List 80 objects per 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)
		}

		// Print the results. By default, 100 records 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 result is truncated, update the continuation token and continue the loop.
		if lsRes.IsTruncated {
			continueToken = lsRes.NextContinuationToken
		} else {
			break
		}
	}

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

ListObjects

Belirli bir ön eke sahip nesneleri sayfa sayfa listelemek için ListObjects yöntemini kullanabilirsiniz. Her sayfada listelenecek nesne sayısını belirtmek için MaxKeys değerini ayarlayabilirsiniz.

package main

import (
	"log"
	"time"

	"github.com/aliyun/aliyun-oss-go-sdk/oss"
)

func main() {
	// Obtain access credentials from environment variables. Before you run this sample code, ensure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are set.
	provider, err := oss.NewEnvironmentVariableCredentialsProvider()
	if err != nil {
		log.Fatalf("Failed to create credentials provider: %v", err)
	}

	// Create an OSSClient instance.
	// Set yourEndpoint to the endpoint of the bucket. For example, if the bucket is in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com. For other regions, use the actual endpoint.
	// Set yourRegion to the region of the bucket. For example, if the bucket is in the China (Hangzhou) region, set the region to cn-hangzhou. For other regions, use the actual region.
	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 bucket name.
	bucketName := "yourBucketName" // Replace with the actual bucket name.
	bucket, err := client.Bucket(bucketName)
	if err != nil {
		log.Fatalf("Failed to get bucket: %v", err)
	}

	// List objects with the specified prefix by page. List 80 objects per 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)
		}

		// Print the results. By default, 100 records 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 result is truncated, update the marker and continue the loop.
		if lsRes.IsTruncated {
			marker = lsRes.NextMarker
		} else {
			break
		}
	}

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

Belirli bir dizindeki tüm nesneler hakkında bilgi listeleme

ListObjectsV2

Belirli bir dizindeki (ön ek) tüm nesneler hakkında; nesne boyutu, son değiştirilme zamanı ve nesne adı dahil olmak üzere bilgi listelemek için ListObjectsV2 yöntemini kullanabilirsiniz.

package main

import (
	"log"
	"time"

	"github.com/aliyun/aliyun-oss-go-sdk/oss"
)

func main() {
	// Obtain access credentials from environment variables. Before you run this sample code, ensure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are set.
	provider, err := oss.NewEnvironmentVariableCredentialsProvider()
	if err != nil {
		log.Fatalf("Failed to create credentials provider: %v", err)
	}

	// Create an OSSClient instance.
	// Set yourEndpoint to the endpoint of the bucket. For example, if the bucket is in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com. For other regions, use the actual endpoint.
	// Set yourRegion to the region of the bucket. For example, if the bucket is in the China (Hangzhou) region, set the region to cn-hangzhou. For other regions, use the actual region.
	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 bucket name.
	bucketName := "yourBucketName" // Replace with the actual bucket name.
	bucket, err := client.Bucket(bucketName)
	if err != nil {
		log.Fatalf("Failed to get bucket: %v", err)
	}

	// List objects with the specified prefix by page. By default, 100 records 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)
		}

		// Print the results.
		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 result is truncated, update the continuation token and continue the loop.
		if lsRes.IsTruncated {
			continueToken = lsRes.NextContinuationToken
		} else {
			break
		}
	}

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

ListObjects

Belirli bir dizindeki (ön ek) tüm nesneler hakkında; nesne boyutu, son değiştirilme zamanı ve nesne adı dahil olmak üzere bilgi listelemek için ListObjects yöntemini kullanabilirsiniz.

package main

import (
	"log"
	"time"

	"github.com/aliyun/aliyun-oss-go-sdk/oss"
)

func main() {
	// Obtain access credentials from environment variables. Before you run this sample code, ensure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are set.
	provider, err := oss.NewEnvironmentVariableCredentialsProvider()
	if err != nil {
		log.Fatalf("Failed to create credentials provider: %v", err)
	}

	// Create an OSSClient instance.
	// Set yourEndpoint to the endpoint of the bucket. For example, if the bucket is in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com. For other regions, use the actual endpoint.
	// Set yourRegion to the region of the bucket. For example, if the bucket is in the China (Hangzhou) region, set the region to cn-hangzhou. For other regions, use the actual region.
	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 bucket name.
	bucketName := "yourBucketName" // Replace with the actual bucket name.
	bucket, err := client.Bucket(bucketName)
	if err != nil {
		log.Fatalf("Failed to get bucket: %v", err)
	}

	// Traverse the 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)
		}

		// Print the results.
		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 result is truncated, update the marker and continue the loop.
		if lor.IsTruncated {
			marker = lor.NextMarker
		} else {
			break
		}
	}

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

Belirli bir dizindeki tüm alt dizinler hakkında bilgi listeleme

ListObjectsV2

Aşağıdaki örnek kod, belirli bir dizindeki (ön ek) tüm alt dizinler hakkında bilgi listelemek için ListObjectsV2 yönteminin nasıl kullanılacağını gösterir.

package main

import (
	"log"

	"github.com/aliyun/aliyun-oss-go-sdk/oss"
)

func main() {
	// Obtain access credentials from environment variables. Before you run this sample code, ensure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are set.
	provider, err := oss.NewEnvironmentVariableCredentialsProvider()
	if err != nil {
		log.Fatalf("Failed to create credentials provider: %v", err)
	}

	// Create an OSSClient instance.
	// Set yourEndpoint to the endpoint of the bucket. For example, if the bucket is in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com. For other regions, use the actual endpoint.
	// Set yourRegion to the region of the bucket. For example, if the bucket is in the China (Hangzhou) region, set the region to cn-hangzhou. For other regions, use the actual region.
	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 bucket name.
	bucketName := "yourBucketName" // Replace with the actual bucket name.
	bucket, err := client.Bucket(bucketName)
	if err != nil {
		log.Fatalf("Failed to get bucket: %v", err)
	}

	// List folders with the specified prefix by page. By default, 100 records 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)
		}

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

		// If the result is truncated, update the continuation token and continue the loop.
		if lsRes.IsTruncated {
			continueToken = lsRes.NextContinuationToken
		} else {
			break
		}
	}

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

ListObjects

Aşağıdaki örnek kod, belirli bir dizindeki (ön ek) tüm alt dizinler hakkında bilgi listelemek için ListObjects yönteminin nasıl kullanılacağını gösterir.

package main

import (
	"log"

	"github.com/aliyun/aliyun-oss-go-sdk/oss"
)

func main() {
	// Obtain access credentials from environment variables. Before you run this sample code, ensure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are set.
	provider, err := oss.NewEnvironmentVariableCredentialsProvider()
	if err != nil {
		log.Fatalf("Failed to create credentials provider: %v", err)
	}

	// Create an OSSClient instance.
	// Set yourEndpoint to the endpoint of the bucket. For example, if the bucket is in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com. For other regions, use the actual endpoint.
	// Set yourRegion to the region of the bucket. For example, if the bucket is in the China (Hangzhou) region, set the region to cn-hangzhou. For other regions, use the actual region.
	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 bucket name.
	bucketName := "yourBucketName" // Replace with the actual bucket name.
	bucket, err := client.Bucket(bucketName)
	if err != nil {
		log.Fatalf("Failed to get bucket: %v", err)
	}

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

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

		// If the result is truncated, update the continuation token and continue the loop.
		if lor.IsTruncated {
			marker = lor.NextMarker
		} else {
			break
		}
	}

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

Nesneleri listeleme ve Owner bilgisi döndürme

Aşağıdaki örnek kod, nesneleri listelemek ve Owner bilgilerini döndürmek için ListObjectsV2 yönteminin nasıl kullanılacağını gösterir.

package main

import (
	"log"

	"github.com/aliyun/aliyun-oss-go-sdk/oss"
)

func main() {
	// Obtain access credentials from environment variables. Before you run this sample code, ensure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are set.
	provider, err := oss.NewEnvironmentVariableCredentialsProvider()
	if err != nil {
		log.Fatalf("Failed to create credentials provider: %v", err)
	}

	// Create an OSSClient instance.
	// Set yourEndpoint to the endpoint of the bucket. For example, if the bucket is in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com. For other regions, use the actual endpoint.
	// Set yourRegion to the region of the bucket. For example, if the bucket is in the China (Hangzhou) region, set the region to cn-hangzhou. For other regions, use the actual region.
	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 bucket name.
	bucketName := "yourBucketName" // Replace with the actual bucket name.
	bucket, err := client.Bucket(bucketName)
	if err != nil {
		log.Fatalf("Failed to get bucket: %v", err)
	}

	// Obtain 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)
	}

	// Print the results. By default, 100 records 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.")
}

Klasörler

OSS, nesneleri depolamak için düz bir yapı kullanır. Bir dizin, adı eğik çizgi (/) ile biten sıfır baytlık bir nesnedir. Bu nesneyi yükleyebilir ve indirebilirsiniz. Varsayılan olarak, adı eğik çizgi (/) ile biten bir nesne OSS konsolunda bir dizin olarak görüntülenir. Dizin oluşturmaya yönelik tam örnek kod için GitHub adresini ziyaret edin.

Nesneleri dizine göre listelemek için delimiter ve prefix parametrelerini belirtebilirsiniz.

  • İstekte prefix öğesini bir dizin adı olarak ayarlarsanız, adları ön eki içeren nesneler ve alt dizinler listelenir.

  • İstekte bir ön ek belirtir ve delimiter öğesini eğik çizgi (/) olarak ayarlarsanız, dizindeki adı belirtilen ön ek ile başlayan nesneler ve alt dizinler listelenir. Her alt dizin, CommonPrefixes içinde tek bir sonuç öğesi olarak listelenir. Bu alt dizinlerdeki nesneler ve dizinler listelenmez.

Örneğin, bir pakette şu nesneler bulunmaktadır: oss.jpg, fun/test.jpg, fun/movie/001.avi ve fun/movie/007.avi. Eğik çizgi (/) dizin sınırlayıcısı olarak belirtilmiştir. Aşağıdaki örnekler, simüle edilmiş dizinlerdeki nesnelerin nasıl listeleneceğini açıklamaktadır.

  • Bir paketteki tüm nesneleri listeleme

    • ListObjectsV2

      Aşağıdaki örnek kod, bir paketteki tüm nesneleri listelemek için ListObjectsV2'nin nasıl kullanılacağını gösterir.

      package main
      
      import (
      	"log"
      
      	"github.com/aliyun/aliyun-oss-go-sdk/oss"
      )
      
      func main() {
      	// Obtain access credentials from environment variables. Before you run this sample code, ensure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are set.
      	provider, err := oss.NewEnvironmentVariableCredentialsProvider()
      	if err != nil {
      		log.Fatalf("Failed to create credentials provider: %v", err)
      	}
      
      	// Create an OSSClient instance.
      	// Set yourEndpoint to the endpoint of the bucket. For example, if the bucket is in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com. For other regions, use the actual endpoint.
      	// Set yourRegion to the region of the bucket. For example, if the bucket is in the China (Hangzhou) region, set the region to cn-hangzhou. For other regions, use the actual region.
      	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 bucket name.
      	bucketName := "yourBucketName" // Replace 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 specified 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)
      		}
      
      		// Print the results. By default, 100 records are returned at a time.
      		for _, object := range lsRes.Objects {
      			log.Printf("Object Key: %s\n", object.Key)
      		}
      
      		// If the result is truncated, update the continuation token and continue the loop.
      		if lsRes.IsTruncated {
      			startAfter = lsRes.StartAfter
      			continueToken = lsRes.NextContinuationToken
      		} else {
      			break
      		}
      	}
      
      	log.Println("All objects have been listed.")
      }
      

      ListObjects

      Aşağıdaki örnek kod, bir paketteki tüm nesneleri listelemek için ListObjects yönteminin nasıl kullanılacağını gösterir.

      package main
      
      import (
      	"log"
      
      	"github.com/aliyun/aliyun-oss-go-sdk/oss"
      )
      
      func main() {
      	// Obtain access credentials from environment variables. Before you run this sample code, ensure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are set.
      	provider, err := oss.NewEnvironmentVariableCredentialsProvider()
      	if err != nil {
      		log.Fatalf("Failed to create credentials provider: %v", err)
      	}
      
      	// Create an OSSClient instance.
      	// Set yourEndpoint to the endpoint of the bucket. For example, if the bucket is in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com. For other regions, use the actual endpoint.
      	// Set yourRegion to the region of the bucket. For example, if the bucket is in the China (Hangzhou) region, set the region to cn-hangzhou. For other regions, use the actual region.
      	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 bucket name.
      	bucketName := "yourBucketName" // Replace 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 specified bucket.
      	marker := ""
      	for {
      		lsRes, err := bucket.ListObjects(oss.Marker(marker))
      		if err != nil {
      			log.Fatalf("Failed to list objects: %v", err)
      		}
      
      		// Print the results. By default, 100 records are returned at a time.
      		for _, object := range lsRes.Objects {
      			log.Printf("Object Key: %s\n", object.Key)
      		}
      
      		// If the result is truncated, update the continuation token and continue the loop.
      		if lsRes.IsTruncated {
      			marker = lsRes.NextMarker
      		} else {
      			break
      		}
      	}
      
      	log.Println("All objects have been listed.")
      }
      
    • Paketteki tüm nesneleri listelemek için yukarıdaki iki yöntemi kullandığınızda aşağıdaki sonuç döndürülür.

      Objects:
      fun/movie/001.avi
      fun/movie/007.avi
      fun/test.jpg
      oss.jpg
      CommonPrefixes:  
  • Belirli bir dizindeki tüm nesneleri listeleme

    • ListObjectsV2

      Aşağıdaki örnek kod, belirli bir dizindeki tüm nesneleri listelemek için ListObjectsV2 yönteminin nasıl kullanılacağını gösterir.

      package main
      
      import (
      	"log"
      
      	"github.com/aliyun/aliyun-oss-go-sdk/oss"
      )
      
      func main() {
      	// Obtain access credentials from environment variables. Before you run this sample code, ensure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are set.
      	provider, err := oss.NewEnvironmentVariableCredentialsProvider()
      	if err != nil {
      		log.Fatalf("Failed to create credentials provider: %v", err)
      	}
      
      	// Create an OSSClient instance.
      	// Set yourEndpoint to the endpoint of the bucket. For example, if the bucket is in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com. For other regions, use the actual endpoint.
      	// Set yourRegion to the region of the bucket. For example, if the bucket is in the China (Hangzhou) region, set the region to cn-hangzhou. For other regions, use the actual region.
      	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 bucket name.
      	bucketName := "yourBucketName" // Replace 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 specified 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)
      		}
      
      		// Print the results. By default, 100 records are returned at a time.
      		for _, object := range lsRes.Objects {
      			log.Printf("Object Key: %s\n", object.Key)
      		}
      
      		// If the result is truncated, update the continuation token and continue the loop.
      		if lsRes.IsTruncated {
      			continueToken = lsRes.NextContinuationToken
      		} else {
      			break
      		}
      	}
      
      	log.Println("All objects have been listed.")
      }
      

      ListObjects

      Aşağıdaki örnek kod, belirli bir dizindeki tüm nesneleri listelemek için ListObjects yönteminin nasıl kullanılacağını gösterir.

      package main
      
      import (
      	"log"
      
      	"github.com/aliyun/aliyun-oss-go-sdk/oss"
      )
      
      func main() {
      	// Obtain access credentials from environment variables. Before you run this sample code, ensure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are set.
      	provider, err := oss.NewEnvironmentVariableCredentialsProvider()
      	if err != nil {
      		log.Fatalf("Failed to create credentials provider: %v", err)
      	}
      
      	// Create an OSSClient instance.
      	// Set yourEndpoint to the endpoint of the bucket. For example, if the bucket is in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com. For other regions, use the actual endpoint.
      	// Set yourRegion to the region of the bucket. For example, if the bucket is in the China (Hangzhou) region, set the region to cn-hangzhou. For other regions, use the actual region.
      	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 bucket name.
      	bucketName := "yourBucketName" // Replace 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 specified directory.
      	prefix := "aaa/db-init" // Replace with the actual prefix.
      	marker := ""
      	for {
      		lsRes, err := bucket.ListObjects(oss.Prefix(prefix), oss.Marker(marker))
      		if err != nil {
      			log.Fatalf("Failed to list objects: %v", err)
      		}
      
      		// Print the results. By default, 100 records are returned at a time.
      		for _, object := range lsRes.Objects {
      			log.Printf("Object Key: %s\n", object.Key)
      		}
      
      		// If the result is truncated, update the continuation token and continue the loop.
      		if lsRes.IsTruncated {
      			marker = lsRes.NextMarker
      		} else {
      			break
      		}
      	}
      
      	log.Println("All objects have been listed.")
      }
      
    • Belirtilen dizindeki tüm nesneleri listelemek için yukarıdaki iki yöntemi kullandığınızda aşağıdaki sonuç döndürülür.

      Objects:
      fun/movie/001.avi
      fun/movie/007.avi
      fun/test.jpg
      CommonPrefixes: 
  • Bir dizindeki nesneleri ve alt dizinleri listeleme

    • ListObjectsV2

      Aşağıdaki örnek kod, bir dizindeki nesneleri ve alt dizinleri listelemek için ListObjectsV2 yönteminin nasıl kullanılacağını gösterir.

      package main
      
      import (
      	"log"
      
      	"github.com/aliyun/aliyun-oss-go-sdk/oss"
      )
      
      func main() {
      	// Obtain access credentials from environment variables. Before you run this sample code, ensure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are set.
      	provider, err := oss.NewEnvironmentVariableCredentialsProvider()
      	if err != nil {
      		log.Fatalf("Failed to create credentials provider: %v", err)
      	}
      
      	// Create an OSSClient instance.
      	// Set yourEndpoint to the endpoint of the bucket. For example, if the bucket is in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com. For other regions, use the actual endpoint.
      	// Set yourRegion to the region of the bucket. For example, if the bucket is in the China (Hangzhou) region, set the region to cn-hangzhou. For other regions, use the actual region.
      	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 bucket name.
      	bucketName := "yourBucketName" // Replace 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 specified directory.
      	prefix := "" // Set a prefix as needed.
      	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)
      		}
      
      		// Print the results. By default, 100 records are returned at a time.
      		for _, object := range lsRes.Objects {
      			log.Printf("Object Key: %s\n", object.Key)
      		}
      
      		// Print the subdirectories.
      		for _, commonPrefix := range lsRes.CommonPrefixes {
      			log.Printf("Common Prefix: %s\n", commonPrefix)
      		}
      
      		// If the result is truncated, update the continuation token and continue the loop.
      		if lsRes.IsTruncated {
      			continueToken = lsRes.NextContinuationToken
      		} else {
      			break
      		}
      	}
      
      	log.Println("All objects and subdirectories have been listed.")
      }
      

      ListObjects

      Aşağıdaki örnek kod, bir dizindeki nesneleri ve alt dizinleri listelemek için ListObjects yönteminin nasıl kullanılacağını gösterir.

      package main
      
      import (
      	"log"
      
      	"github.com/aliyun/aliyun-oss-go-sdk/oss"
      )
      
      func main() {
      	// Obtain access credentials from environment variables. Before you run this sample code, ensure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are set.
      	provider, err := oss.NewEnvironmentVariableCredentialsProvider()
      	if err != nil {
      		log.Fatalf("Failed to create credentials provider: %v", err)
      	}
      
      	// Create an OSSClient instance.
      	// Set yourEndpoint to the endpoint of the bucket. For example, if the bucket is in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com. For other regions, use the actual endpoint.
      	// Set yourRegion to the region of the bucket. For example, if the bucket is in the China (Hangzhou) region, set the region to cn-hangzhou. For other regions, use the actual region.
      	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 bucket name.
      	bucketName := "yourBucketName" // Replace 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 specified directory.
      	prefix := "aaa/db-init/" // Replace with the actual prefix.
      	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)
      		}
      
      		// Print the list of objects.
      		for _, object := range lsRes.Objects {
      			log.Printf("Object Key: %s\n", object.Key)
      		}
      
      		// Print the list of subdirectories.
      		for _, commonPrefix := range lsRes.CommonPrefixes {
      			log.Printf("Common Prefix: %s\n", commonPrefix)
      		}
      
      		// If the result is truncated, update the continuation token and continue the loop.
      		if lsRes.IsTruncated {
      			marker = lsRes.NextMarker
      		} else {
      			break
      		}
      	}
      
      	log.Println("All objects and subdirectories have been listed.")
      }
      
    • Belirtilen dizindeki nesneleri ve alt dizinleri listelemek için yukarıdaki iki yöntemi kullandığınızda aşağıdaki sonuç döndürülür.

      Objects:
      fun/test.jpg
      
      CommonPrefixes:
      fun/movie/   
  • Belirli bir dizindeki nesnelerin boyutunu alma

    • ListObjectsV2

      Aşağıdaki örnek kod, belirli bir dizindeki nesnelerin boyutunu almak için ListObjectsV2 yönteminin nasıl kullanılacağını gösterir.

      package main
      
      import (
      	"log"
      
      	"github.com/aliyun/aliyun-oss-go-sdk/oss"
      )
      
      func main() {
      	// Obtain access credentials from environment variables. Before you run this sample code, ensure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are set.
      	provider, err := oss.NewEnvironmentVariableCredentialsProvider()
      	if err != nil {
      		log.Fatalf("Failed to create credentials provider: %v", err)
      	}
      
      	// Create an OSSClient instance.
      	// Set yourEndpoint to the endpoint of the bucket. For example, if the bucket is in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com. For other regions, use the actual endpoint.
      	// Set yourRegion to the region of the bucket. For example, if the bucket is in the China (Hangzhou) region, set the region to cn-hangzhou. For other regions, use the actual region.
      	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)
      	}
      
      	// Get the bucket.
      	bucketName := "yourBucketName" // Replace with the actual bucket name.
      	bucket, err := client.Bucket(bucketName)
      	if err != nil {
      		log.Fatalf("Failed to get bucket: %v", err)
      	}
      
      	// Get the size of objects in the specified directory.
      	prefix := "/fun" // Replace with the actual prefix.
      	continueToken := ""
      	for {
      		lsRes, err := bucket.ListObjectsV2(oss.Prefix(prefix), oss.ContinuationToken(continueToken))
      		if err != nil {
      			log.Fatalf("Failed to list objects: %v", err)
      		}
      
      		// Print the list of 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 result is truncated, update the continuation token and continue the loop.
      		if lsRes.IsTruncated {
      			continueToken = lsRes.NextContinuationToken
      		} else {
      			break
      		}
      	}
      
      	log.Println("All objects have been listed with their sizes.")
      }
      

      ListObjects

      Aşağıdaki örnek kod, belirli bir dizindeki nesnelerin boyutunu almak için ListObjects yönteminin nasıl kullanılacağını gösterir.

      package main
      
      import (
      	"log"
      
      	"github.com/aliyun/aliyun-oss-go-sdk/oss"
      )
      
      func main() {
      	// Obtain access credentials from environment variables. Before you run this sample code, ensure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are set.
      	provider, err := oss.NewEnvironmentVariableCredentialsProvider()
      	if err != nil {
      		log.Fatalf("Failed to create credentials provider: %v", err)
      	}
      
      	// Create an OSSClient instance.
      	// Set yourEndpoint to the endpoint of the bucket. For example, if the bucket is in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com. For other regions, use the actual endpoint.
      	// Set yourRegion to the region of the bucket. For example, if the bucket is in the China (Hangzhou) region, set the region to cn-hangzhou. For other regions, use the actual region.
      	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 bucket name.
      	bucketName := "yourBucketName" // Replace with the actual bucket name.
      	bucket, err := client.Bucket(bucketName)
      	if err != nil {
      		log.Fatalf("Failed to get bucket: %v", err)
      	}
      
      	// Get the size of objects in the specified directory.
      	prefix := "test/" // Replace with the actual prefix.
      	marker := ""
      	for {
      		lsRes, err := bucket.ListObjects(oss.Prefix(prefix), oss.Marker(marker))
      		if err != nil {
      			log.Fatalf("Failed to list objects: %v", err)
      		}
      
      		// Print the list of 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 result is truncated, update the continuation token and continue the loop.
      		if lsRes.IsTruncated {
      			marker = lsRes.NextMarker
      		} else {
      			break
      		}
      	}
      
      	log.Println("All objects have been listed with their sizes.")
      }
      

SSS

Nesneleri listelerken son değiştirilme zamanına göre sıralayabilir miyim?

Hayır. Nesneleri son değiştirilme zamanına göre sıralamak için veri indeksleme özelliğini kullanın.

İlgili Konular