本文介绍如何通过 ListObjectsType2 列举指定桶下的所有对象、指定前缀文件、指定目录下的文件和子目录。
tos:ListBucket
权限,具体操作,请参见 IAM 策略概述。ListObjectsType2 支持的参数如下:
参数 | 描述 |
---|---|
Prefix | 本次查询结果的前缀。 |
Delimiter | 对对象名称进行分组的字符。 |
StartAfter | 此次列举对象的起点。 |
ContinuationToken | 指定列举操作需要从此 token 开始。可从上次列举结果中的 NextContinuationToken 中获取。 |
MaxKeys | 返回列举对象的最大数,默认值为 1000。 |
EncodingType | 对返回的内容进行编码并指定编码的类型。 |
以下代码用于列举指定存储桶获取最多 10 个对象。
package main import ( "context" "fmt" "github.com/volcengine/ve-tos-golang-sdk/v2/tos" ) func checkErr(err error) { if err != nil { if serverErr, ok := err.(*tos.TosServerError); ok { fmt.Println("Error:", serverErr.Error()) fmt.Println("Request ID:", serverErr.RequestID) fmt.Println("Response Status Code:", serverErr.StatusCode) fmt.Println("Response Header:", serverErr.Header) fmt.Println("Response Err Code:", serverErr.Code) fmt.Println("Response Err Msg:", serverErr.Message) } else if clientErr, ok := err.(*tos.TosClientError); ok { fmt.Println("Error:", clientErr.Error()) fmt.Println("Client Cause Err:", clientErr.Cause.Error()) } else { fmt.Println("Error:", err) } panic(err) } } func main() { var ( accessKey = os.Getenv("TOS_ACCESS_KEY") secretKey = os.Getenv("TOS_SECRET_KEY") // Bucket 对应的 Endpoint,以华北2(北京)为例:https://tos-cn-beijing.volces.com endpoint = "https://tos-cn-beijing.volces.com" region = "cn-beijing" // 填写 BucketName bucketName = "*** Provide your bucket name ***" ctx = context.Background() ) // 初始化客户端 client, err := tos.NewClientV2(endpoint, tos.WithRegion(region), tos.WithCredentials(tos.NewStaticCredentials(accessKey, secretKey))) checkErr(err) // 获取 bucket 中最多 10 个对象 output, err := client.ListObjectsType2(ctx, &tos.ListObjectsType2Input{ Bucket: bucketName, MaxKeys: 10, }) checkErr(err) for _, obj := range output.Contents { // 对象 Key fmt.Println("Object Key:", obj.Key) // 对象最后修改时间 fmt.Println("Object LastModified:", obj.LastModified) // 对象 Etag fmt.Println("Object ETag:", obj.ETag) // 对象大小 fmt.Println("Object Size:", obj.Size) // 对象 Owner fmt.Println("Object Owner:", obj.Owner) // 对象存储类型 fmt.Println("Object StorageClass:", obj.StorageClass) // 对象 CRC64 fmt.Println("Object HashCrc64ecma:", obj.HashCrc64ecma) } }
以下代码用于分页列举指定存储桶中的对象。
package main import ( "context" "fmt" "github.com/volcengine/ve-tos-golang-sdk/v2/tos" ) func checkErr(err error) { if err != nil { if serverErr, ok := err.(*tos.TosServerError); ok { fmt.Println("Error:", serverErr.Error()) fmt.Println("Request ID:", serverErr.RequestID) fmt.Println("Response Status Code:", serverErr.StatusCode) fmt.Println("Response Header:", serverErr.Header) fmt.Println("Response Err Code:", serverErr.Code) fmt.Println("Response Err Msg:", serverErr.Message) } else if clientErr, ok := err.(*tos.TosClientError); ok { fmt.Println("Error:", clientErr.Error()) fmt.Println("Client Cause Err:", clientErr.Cause.Error()) } else { fmt.Println("Error:", err) } panic(err) } } func printObjectContent(contents []tos.ListedObjectV2) { for _, obj := range contents { // 对象 Key fmt.Println("Object Key:", obj.Key) // 对象最后修改时间 fmt.Println("Object LastModified:", obj.LastModified) // 对象 Etag fmt.Println("Object ETag:", obj.ETag) // 对象大小 fmt.Println("Object Size:", obj.Size) // 对象 Owner fmt.Println("Object Owner:", obj.Owner) // 对象存储类型 fmt.Println("Object StorageClass:", obj.StorageClass) // 对象 CRC64 fmt.Println("Object HashCrc64ecma:", obj.HashCrc64ecma) } } func main() { var ( accessKey = os.Getenv("TOS_ACCESS_KEY") secretKey = os.Getenv("TOS_SECRET_KEY") // Bucket 对应的 Endpoint,以华北2(北京)为例:https://tos-cn-beijing.volces.com endpoint = "https://tos-cn-beijing.volces.com" region = "cn-beijing" // 填写 BucketName bucketName = "*** Provide your bucket name ***" ctx = context.Background() ) // 初始化客户端 client, err := tos.NewClientV2(endpoint, tos.WithRegion(region), tos.WithCredentials(tos.NewStaticCredentials(accessKey, secretKey))) checkErr(err) // 获取 bucket 中前 1000 个对象 output, err := client.ListObjectsType2(ctx, &tos.ListObjectsType2Input{ Bucket: bucketName, MaxKeys: 1000, }) checkErr(err) printObjectContent(output.Contents) // 分页列举后 1000 个对象 output, err = client.ListObjectsType2(ctx, &tos.ListObjectsType2Input{ Bucket: bucketName, MaxKeys: 1000, ContinuationToken: output.NextContinuationToken, }) printObjectContent(output.Contents) }
以下代码用于列举指定存储桶指定前缀的所有对象。
package main import ( "context" "fmt" "github.com/volcengine/ve-tos-golang-sdk/v2/tos" ) func checkErr(err error) { if err != nil { if serverErr, ok := err.(*tos.TosServerError); ok { fmt.Println("Error:", serverErr.Error()) fmt.Println("Request ID:", serverErr.RequestID) fmt.Println("Response Status Code:", serverErr.StatusCode) fmt.Println("Response Header:", serverErr.Header) fmt.Println("Response Err Code:", serverErr.Code) fmt.Println("Response Err Msg:", serverErr.Message) } else if clientErr, ok := err.(*tos.TosClientError); ok { fmt.Println("Error:", clientErr.Error()) fmt.Println("Client Cause Err:", clientErr.Cause.Error()) } else { fmt.Println("Error:", err) } panic(err) } } func main() { var ( accessKey = os.Getenv("TOS_ACCESS_KEY") secretKey = os.Getenv("TOS_SECRET_KEY") // Bucket 对应的 Endpoint,以华北2(北京)为例:https://tos-cn-beijing.volces.com endpoint = "https://tos-cn-beijing.volces.com" region = "cn-beijing" // 填写 BucketName bucketName = "*** Provide your bucket name ***" prefix = "*** Provide your object key prefix ***" ctx = context.Background() ) // 初始化客户端 client, err := tos.NewClientV2(endpoint, tos.WithRegion(region), tos.WithCredentials(tos.NewStaticCredentials(accessKey, secretKey))) checkErr(err) // 列举指定前缀下的所有对象 truncated := true continuationToken := "" for truncated { output, err := client.ListObjectsType2(ctx, &tos.ListObjectsType2Input{ Bucket: bucketName, MaxKeys: 1000, ContinuationToken: continuationToken, Prefix: prefix, }) checkErr(err) for _, obj := range output.Contents { // 对象 Key fmt.Println("Object Key:", obj.Key) // 对象最后修改时间 fmt.Println("Object LastModified:", obj.LastModified) // 对象 Etag fmt.Println("Object ETag:", obj.ETag) // 对象大小 fmt.Println("Object Size:", obj.Size) // 对象 Owner fmt.Println("Object Owner:", obj.Owner) // 对象存储类型 fmt.Println("Object StorageClass:", obj.StorageClass) // 对象 CRC64 fmt.Println("Object HashCrc64ecma:", obj.HashCrc64ecma) } truncated = output.IsTruncated continuationToken = output.NextContinuationToken } }
以下代码用于列举指定存储桶中所有对象。
package main import ( "context" "fmt" "github.com/volcengine/ve-tos-golang-sdk/v2/tos" ) func checkErr(err error) { if err != nil { if serverErr, ok := err.(*tos.TosServerError); ok { fmt.Println("Error:", serverErr.Error()) fmt.Println("Request ID:", serverErr.RequestID) fmt.Println("Response Status Code:", serverErr.StatusCode) fmt.Println("Response Header:", serverErr.Header) fmt.Println("Response Err Code:", serverErr.Code) fmt.Println("Response Err Msg:", serverErr.Message) } else if clientErr, ok := err.(*tos.TosClientError); ok { fmt.Println("Error:", clientErr.Error()) fmt.Println("Client Cause Err:", clientErr.Cause.Error()) } else { fmt.Println("Error:", err) } panic(err) } } func main() { var ( accessKey = os.Getenv("TOS_ACCESS_KEY") secretKey = os.Getenv("TOS_SECRET_KEY") // Bucket 对应的 Endpoint,以华北2(北京)为例:https://tos-cn-beijing.volces.com endpoint = "https://tos-cn-beijing.volces.com" region = "cn-beijing" // 填写 BucketName bucketName = "*** Provide your bucket name ***" ctx = context.Background() ) // 初始化客户端 client, err := tos.NewClientV2(endpoint, tos.WithRegion(region), tos.WithCredentials(tos.NewStaticCredentials(accessKey, secretKey))) checkErr(err) // 列举指定桶中所有对象 truncated := true continuationToken := "" for truncated { output, err := client.ListObjectsType2(ctx, &tos.ListObjectsType2Input{ Bucket: bucketName, MaxKeys: 1000, ContinuationToken: continuationToken, }) checkErr(err) for _, obj := range output.Contents { // 对象 Key fmt.Println("Object Key:", obj.Key) // 对象最后修改时间 fmt.Println("Object LastModified:", obj.LastModified) // 对象 Etag fmt.Println("Object ETag:", obj.ETag) // 对象大小 fmt.Println("Object Size:", obj.Size) // 对象 Owner fmt.Println("Object Owner:", obj.Owner) // 对象存储类型 fmt.Println("Object StorageClass:", obj.StorageClass) // 对象 CRC64 fmt.Println("Object HashCrc64ecma:", obj.HashCrc64ecma) } truncated = output.IsTruncated continuationToken = output.NextContinuationToken } }
TOS 只有对象的概念, 可通过创建一个大小为 0 并且以斜线 /
结尾的对象, 模拟目录的功能。
通过 Delimiter
和 Prefix
两个参数可以模拟目录的功能:
Delimiter
为 /
同时设置 Prefix
为空, 可返回根目录下的对象和子目录信息。Delimiter
为 /
同时设置 Prefix
为子目录(subfiledir), 可返回子目录的对象和次级目录。以下代码用于列举根目录下的对象和子目录。
package main import ( "context" "fmt" "github.com/volcengine/ve-tos-golang-sdk/v2/tos" ) func checkErr(err error) { if err != nil { if serverErr, ok := err.(*tos.TosServerError); ok { fmt.Println("Error:", serverErr.Error()) fmt.Println("Request ID:", serverErr.RequestID) fmt.Println("Response Status Code:", serverErr.StatusCode) fmt.Println("Response Header:", serverErr.Header) fmt.Println("Response Err Code:", serverErr.Code) fmt.Println("Response Err Msg:", serverErr.Message) } else if clientErr, ok := err.(*tos.TosClientError); ok { fmt.Println("Error:", clientErr.Error()) fmt.Println("Client Cause Err:", clientErr.Cause.Error()) } else { fmt.Println("Error:", err) } panic(err) } } func main() { var ( accessKey = os.Getenv("TOS_ACCESS_KEY") secretKey = os.Getenv("TOS_SECRET_KEY") // Bucket 对应的 Endpoint,以华北2(北京)为例:https://tos-cn-beijing.volces.com endpoint = "https://tos-cn-beijing.volces.com" region = "cn-beijing" // 填写 BucketName bucketName = "*** Provide your bucket name ***" ctx = context.Background() ) // 初始化客户端 client, err := tos.NewClientV2(endpoint, tos.WithRegion(region), tos.WithCredentials(tos.NewStaticCredentials(accessKey, secretKey))) checkErr(err) // 列举目录下的文件和子目录 truncated := true continuationToken := "" for truncated { output, err := client.ListObjectsType2(ctx, &tos.ListObjectsType2Input{ Bucket: bucketName, MaxKeys: 1000, ContinuationToken: continuationToken, Delimiter: "/", }) checkErr(err) // 返回根目录下的子目录 for _, prefix := range output.CommonPrefixes { fmt.Println("Sub Dir:", prefix.Prefix) } // Contents 中返回根目录下的对象 for _, obj := range output.Contents { // 对象 Key fmt.Println("Object Key:", obj.Key) // 对象最后修改时间 fmt.Println("Object LastModified:", obj.LastModified) // 对象 Etag fmt.Println("Object ETag:", obj.ETag) // 对象大小 fmt.Println("Object Size:", obj.Size) // 对象 Owner fmt.Println("Object Owner:", obj.Owner) // 对象存储类型 fmt.Println("Object StorageClass:", obj.StorageClass) // 对象 CRC64 fmt.Println("Object HashCrc64ecma:", obj.HashCrc64ecma) } truncated = output.IsTruncated continuationToken = output.NextContinuationToken } }