このトピックでは、すべてのオブジェクト、名前に特定のプレフィックスが含まれるオブジェクト、および特定のディレクトリ内のオブジェクトとサブディレクトリをObject Storage Service (OSS) バケットに一覧表示する方法について説明します。
使用上の注意
このトピックでは、中国 (杭州) リージョンのパブリックエンドポイントを使用します。 OSSと同じリージョンにある他のAlibaba CloudサービスからOSSにアクセスする場合は、内部エンドポイントを使用します。 OSSリージョンとエンドポイントの詳細については、「リージョン、エンドポイント、オープンポート」をご参照ください。
このトピックでは、アクセス資格情報は環境変数から取得します。 アクセス資格情報の設定方法の詳細については、「アクセス資格情報の設定」をご参照ください。
このトピックでは、OSSエンドポイントを使用してOSSClientインスタンスを作成します。 カスタムドメイン名またはSTS (Security Token Service) を使用してOSSClientインスタンスを作成する場合は、「初期化」をご参照ください。
オブジェクトを一覧表示するには、
oss:ListObjects
権限が必要です。 詳細については、「RAMユーザーへのカスタムポリシーのアタッチ」をご参照ください。Go 2.2.5以降のOSS SDKは、RestoreInfoレスポンスパラメーターをサポートしています。
背景情報
ListObjectsV2
またはListObjects
操作を呼び出して、一度に最大1,000個のオブジェクトをバケットに一覧表示できます。 さまざまな条件に基づいてオブジェクトを一覧表示するようにパラメーターを設定できます。 たとえば、特定の開始位置からオブジェクトを一覧表示し、特定のディレクトリ内のオブジェクトとサブディレクトリを一覧表示し、ページごとにオブジェクトを一覧表示するようにパラメーターを設定できます。 ListObjectsV2とListObjectsの操作は、次の点で異なります。
ListObjectsV2
操作を呼び出してオブジェクトを一覧表示する場合、fetchOwner
パラメーターを設定して、応答にオブジェクト所有者情報を含めるかどうかを指定できます。デフォルトでは、
ListObjects
操作を呼び出すと、オブジェクト所有者情報が応答に含まれます。説明バージョン管理が有効なバケット内のオブジェクトを一覧表示するには、ListObjectsV2操作を呼び出すことを推奨します。
次の表では、ListObjectsV2またはListObjects操作を呼び出してオブジェクトを一覧表示するときに設定できるパラメーターについて説明します。
ListObjectsV2
次の表に、ListObjectsV2操作を呼び出してオブジェクトを一覧表示するときに設定できるパラメーターを示します。
パラメーター | 説明 |
プレフィックス | 一覧表示するオブジェクトの名前に含める必要があるプレフィックス。 |
区切り文字 | リストするオブジェクトを名前でグループ化するために使用される文字。 指定されたプレフィックスから区切り文字の最初の出現までの同じ文字列を名前に含むオブジェクトは、CommonPrefixes要素としてグループ化されます。 |
startAfter | リスト操作の開始位置。 |
fetchOwner | レスポンスにオブジェクト所有者情報を含めるかどうかを指定します。 有効な値:
|
詳細については、「ListObjectsV2」をご参照ください。
ListObjects
次の表に、ListObjects操作を呼び出してオブジェクトを一覧表示するときに設定できるパラメーターを示します。
パラメーター | 説明 |
区切り文字 | リストするオブジェクトを名前でグループ化するために使用される文字。 指定されたプレフィックスから区切り文字の最初の出現までの同じ文字列を名前に含むオブジェクトは、CommonPrefixes要素としてグループ化されます。 |
プレフィックス | 一覧表示するオブジェクトの名前に含める必要があるプレフィックス。 |
maxKeys | 一度に一覧表示できるオブジェクトの最大数。 最大値は 1000 です。 デフォルト値:100 |
マーカー | リスト操作の開始位置。 |
詳細については、「ListObjects」をご参照ください。
例
ListObjectsV2
次のサンプルコードは、ListObjectsV2操作を呼び出して特定のバケット内の100のオブジェクトを一覧表示する方法の例を示しています。
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
次のサンプルコードは、ListObjects操作を呼び出して特定のバケット内の100のオブジェクトを一覧表示する方法の例を示しています。
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.")
}
一般的なシナリオ
特定の数のオブジェクトのリスト
ListObjectsV2
次のサンプルコードは、ListObjectsV2操作を呼び出して、バケット内の特定の数のオブジェクトを一覧表示する方法の例を示しています。
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
次のサンプルコードは、ListObjects操作を呼び出してバケット内の特定の数のオブジェクトを一覧表示する方法の例を示しています。
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.")
}
}
名前に特定のプレフィックスが含まれるオブジェクトの一覧表示
ListObjectsV2
次のサンプルコードでは、ListObjectsV2操作を呼び出して、名前に特定のプレフィックスが含まれるオブジェクトを一覧表示する方法の例を示します。
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
次のサンプルコードでは、ListObjects操作を呼び出して、名前に特定のプレフィックスが含まれるオブジェクトを一覧表示する方法の例を示します。
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.")
}
}
特定の位置からのすべてのオブジェクトの一覧表示
ListObjectsV2
StartAfterパラメーターを設定して、リスト操作の開始位置を指定できます。 StartAfterパラメータの値の後に名前がアルファベット順になっているすべてのオブジェクトが返されます。
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
Markerパラメーターを設定して、リスト操作の開始位置を指定できます。 Markerパラメーターの値の後に名前がアルファベット順になっているすべてのオブジェクトが返されます。
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.")
}
すべてのオブジェクトをページ単位で一覧表示
ListObjectsV2
次のサンプルコードでは、ListObjectsV2操作を呼び出して、特定のバケット内のすべてのオブジェクトをページごとに一覧表示する方法の例を示します。 MaxKeysパラメーターを設定して、各ページに一覧表示できるオブジェクトの最大数を指定できます。
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
次のサンプルコードでは、ListObjects操作を呼び出して、特定のバケット内のすべてのオブジェクトをページごとに一覧表示する方法の例を示します。 MaxKeysパラメーターを設定して、各ページに一覧表示できるオブジェクトの最大数を指定できます。
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.")
}
名前に特定のプレフィックスが含まれるオブジェクトをページ単位でリストする
ListObjectsV2
次のサンプルコードでは、ListObjectsV2操作を呼び出して、名前に特定のプレフィックスが含まれているオブジェクトをページごとに一覧表示する方法の例を示します。 MaxKeysパラメーターを設定して、各ページに一覧表示できるオブジェクトの最大数を指定できます。
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
次のサンプルコードでは、ListObjects操作を呼び出して、名前に特定のプレフィックスが含まれているオブジェクトをページごとに一覧表示する方法の例を示します。 MaxKeysパラメーターを設定して、各ページに一覧表示できるオブジェクトの最大数を指定できます。
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.")
}
特定のディレクトリ内のすべてのオブジェクトに関する情報の一覧表示
ListObjectsV2
次のサンプルコードでは、ListObjectsV2操作を呼び出して、オブジェクトサイズ、最終変更時刻、オブジェクト名など、特定のディレクトリ内のすべてのオブジェクトに関する情報を一覧表示する方法の例を示します。
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
次のサンプルコードでは、ListObjects操作を呼び出して、オブジェクトサイズ、最終変更時刻、オブジェクト名など、特定のディレクトリ内のすべてのオブジェクトに関する情報を一覧表示する方法の例を示します。
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.")
}
特定のディレクトリ内のすべてのサブディレクトリに関する情報の一覧表示
ListObjectsV2
次のサンプルコードでは、ListObjectsV2操作を呼び出して、特定のディレクトリ内のすべてのサブディレクトリに関する情報を一覧表示する方法の例を示します。
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
次のサンプルコードでは、ListObjects操作を呼び出して、特定のディレクトリ内のすべてのサブディレクトリに関する情報を一覧表示する方法の例を示します。
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.")
}
リストオブジェクトとその所有者情報
次のサンプルコードでは、ListObjectsV2操作を呼び出してオブジェクトとその所有者情報を一覧表示する方法の例を示します。
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.")
}
ディレクトリ
OSSはフラット構造を使用してオブジェクトを格納します。 ディレクトリは、名前がスラッシュ (/) で終わるゼロバイトのオブジェクトです。 このオブジェクトをアップロードおよびダウンロードできます。 デフォルトでは、名前がスラッシュ (/) で終わるオブジェクトは、OSSコンソールにディレクトリとして表示されます。 ディレクトリの作成に使用する完全なサンプルコードについては、『GitHub』をご参照ください。
デリミタとプレフィックスパラメータを指定して、ディレクトリごとにオブジェクトを一覧表示できます。
リクエストでプレフィックスをディレクトリ名に設定すると、プレフィックスを含む名前のオブジェクトとサブディレクトリが一覧表示されます。
リクエストでプレフィックスを指定し、区切り文字をスラッシュ (/) に設定すると、ディレクトリ内で指定されたプレフィックスで始まる名前のオブジェクトとサブディレクトリが一覧表示されます。 各サブディレクトリは、CommonPrefixesで単一の結果要素としてリストされます。 これらのサブディレクトリ内のオブジェクトおよびディレクトリはリストされません。
たとえば、バケットにはoss.jpg
、fun/test.jpg
、fun/movie/001.avi
、fun/movie/007.avi
のオブジェクトが含まれています。 ディレクトリ区切り文字としてスラッシュ (/) を指定します。 次の例では、シミュレートされたディレクトリにオブジェクトを一覧表示します。
バケット内のすべてのオブジェクトの一覧表示
ListObjectsV2
次のサンプルコードでは、ListObjectsV2操作を呼び出してバケット内のすべてのオブジェクトを一覧表示する方法の例を示します。
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
次のサンプルコードは、ListObjects操作を呼び出してバケット内のすべてのオブジェクトを一覧表示する方法の例を示しています。
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.") }
上記の2つの操作を呼び出してバケット内のすべてのオブジェクトを一覧表示すると、次のレスポンスが返されます。
Objects: fun/movie/001.avi fun/movie/007.avi fun/test.jpg oss.jpg CommonPrefixes:
ディレクトリ内のすべてのオブジェクトを一覧表示する
ListObjectsV2
次のサンプルコードでは、ListObjectsV2操作を呼び出して、特定のディレクトリ内のすべてのオブジェクトを一覧表示する方法の例を示します。
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
次のサンプルコードは、ListObjects操作を呼び出して特定のディレクトリ内のすべてのオブジェクトを一覧表示する方法の例を示しています。
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.") }
上記の2つの操作を呼び出して、特定のディレクトリ内のすべてのオブジェクトを一覧表示すると、次の応答が返されます。
Objects: fun/movie/001.avi fun/movie/007.avi fun/test.jpg CommonPrefixes:
特定のディレクトリ内のオブジェクトとサブディレクトリの一覧表示
ListObjectsV2
次のサンプルコードでは、ListObjectsV2操作を呼び出して、特定のディレクトリ内のオブジェクトとサブディレクトリを一覧表示する方法の例を示します。
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
次のサンプルコードでは、ListObjects操作を呼び出して、特定のディレクトリ内のオブジェクトとサブディレクトリを一覧表示する方法の例を示します。
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.") }
上記の2つの操作を呼び出して、特定のディレクトリ内のオブジェクトとサブディレクトリを一覧表示すると、次の応答が返されます。
Objects: fun/test.jpg CommonPrefixes: fun/movie/
特定のディレクトリ内のオブジェクトのサイズを一覧表示する
ListObjectsV2
次のサンプルコードでは、ListObjectsV2操作を呼び出して、特定のディレクトリ内のオブジェクトのサイズを一覧表示する方法の例を示します。
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
次のサンプルコードでは、ListObjects操作を呼び出して、特定のディレクトリ内のオブジェクトのサイズを一覧表示する方法の例を示します。
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.") }
よくある質問
オブジェクトをリストするときに、最後に変更された時刻に基づいてオブジェクトをソートできますか?
いいえ、オブジェクトをリストするときに最後に変更された時刻に基づいてオブジェクトを並べ替えることはできません。 最終変更時刻に基づいてオブジェクトをソートする必要がある場合は、データインデックス機能を使用することを推奨します。 詳細については、「データインデックス作成」をご参照ください。
関連ドキュメント
オブジェクトの一覧表示に使用する完全なサンプルコードについては、『GitHub』をご参照ください。
オブジェクトを一覧表示するために呼び出すAPI操作の詳細については、「ListObjectsV2」および「ListObjects」をご参照ください。