列举对象
更新时间: 2019-03-14 10:05
您可以通过ObsClient.ListObjects列举出桶里的对象。
该接口可设置的参数如下:
参数 | 作用 |
---|---|
ListObjectsInput.Bucket | 桶名。 |
ListObjectsInput.Prefix | 限定返回的对象名必须带有Prefix前缀。 |
ListObjectsInput.Marker | 列举对象的起始位置,返回的对象列表将是对象名按照字典序排序后该参数以后的所有对象。 |
ListObjectsInput.MaxKeys | 列举对象的最大数目,取值范围为1~1000,当超出范围时,按照默认的1000进行处理。 |
ListObjectsInput.Delimiter | 用于对对象名进行分组的字符。对于对象名中包含Delimiter的对象,其对象名(如果请求中指定了Prefix,则此处的对象名需要去掉Prefix)中从首字符至第一个Delimiter之间的字符串将作为一个分组并作为CommonPrefix返回。 |
简单列举
以下代码展示如何简单列举对象,最多返回1000个对象:
- // 引入依赖包
- import (
- "fmt"
- "obs"
- )
- var ak = "*** Provide your Access Key ***"
- var sk = "*** Provide your Secret Key ***"
- var endpoint = "https://your-endpoint"
- // 创建ObsClient结构体
- var obsClient, _ = obs.New(ak, sk, endpoint)
- func main() {
- input := &obs.ListObjectsInput{}
- input.Bucket = "bucketname"
- output, err := obsClient.ListObjects(input)
- if err == nil {
- for index, val := range output.Contents {
- fmt.Printf("Content[%d]-OwnerId:%s, ETag:%s, Key:%s, LastModified:%s, Size:%d, StorageClass:%s\n",
- index, val.Owner.ID, val.ETag, val.Key, val.LastModified, val.Size, val.StorageClass)
- }
- } else if obsError, ok := err.(obs.ObsError); ok {
- fmt.Printf("Code:%s\n", obsError.Code)
- fmt.Printf("Message:%s\n", obsError.Message)
- }
- }
说明:
- 每次至多返回1000个对象,如果指定桶包含的对象数量大于1000,则返回结果中ListObjectsOutput.IsTruncated为true表明本次没有返回全部段,并可通过ListObjectsOutput.NextMarker获取下次列举的起始位置。
- 如果想获取指定桶包含的所有对象,可以采用分页列举的方式。
指定数目列举
以下代码展示如何指定数目列举对象:
- // 引入依赖包
- import (
- "fmt"
- "obs"
- )
- var ak = "*** Provide your Access Key ***"
- var sk = "*** Provide your Secret Key ***"
- var endpoint = "https://your-endpoint"
- // 创建ObsClient结构体
- var obsClient, _ = obs.New(ak, sk, endpoint)
- func main() {
- input := &obs.ListObjectsInput{}
- input.Bucket = "bucketname"
- // 只列举100个对象
- input.MaxKeys = 100
- output, err := obsClient.ListObjects(input)
- if err == nil {
- for index, val := range output.Contents {
- fmt.Printf("Content[%d]-OwnerId:%s, ETag:%s, Key:%s, LastModified:%s, Size:%d, StorageClass:%s\n",
- index, val.Owner.ID, val.ETag, val.Key, val.LastModified, val.Size, val.StorageClass)
- }
- } else if obsError, ok := err.(obs.ObsError); ok {
- fmt.Printf("Code:%s\n", obsError.Code)
- fmt.Printf("Message:%s\n", obsError.Message)
- }
- }
指定前缀列举
以下代码展示如何指定前缀列举对象:
- // 引入依赖包
- import (
- "fmt"
- "obs"
- )
- var ak = "*** Provide your Access Key ***"
- var sk = "*** Provide your Secret Key ***"
- var endpoint = "https://your-endpoint"
- // 创建ObsClient结构体
- var obsClient, _ = obs.New(ak, sk, endpoint)
- func main() {
- input := &obs.ListObjectsInput{}
- input.Bucket = "bucketname"
- // 设置列举带有prefix前缀的100个对象
- input.MaxKeys = 100
- input.Prefix = "prefix"
- output, err := obsClient.ListObjects(input)
- if err == nil {
- for index, val := range output.Contents {
- fmt.Printf("Content[%d]-OwnerId:%s, ETag:%s, Key:%s, LastModified:%s, Size:%d, StorageClass:%s\n",
- index, val.Owner.ID, val.ETag, val.Key, val.LastModified, val.Size, val.StorageClass)
- }
- } else if obsError, ok := err.(obs.ObsError); ok {
- fmt.Printf("Code:%s\n", obsError.Code)
- fmt.Printf("Message:%s\n", obsError.Message)
- }
- }
指定起始位置列举
以下代码展示如何指定起始位置列举对象:
- // 引入依赖包
- import (
- "fmt"
- "obs"
- )
- var ak = "*** Provide your Access Key ***"
- var sk = "*** Provide your Secret Key ***"
- var endpoint = "https://your-endpoint"
- // 创建ObsClient结构体
- var obsClient, _ = obs.New(ak, sk, endpoint)
- func main() {
- input := &obs.ListObjectsInput{}
- input.Bucket = "bucketname"
- // 设置列举对象名字典序在"test"之后的100个对象
- input.MaxKeys = 100
- input.Marker = "test"
- output, err := obsClient.ListObjects(input)
- if err == nil {
- for index, val := range output.Contents {
- fmt.Printf("Content[%d]-OwnerId:%s, ETag:%s, Key:%s, LastModified:%s, Size:%d, StorageClass:%s\n",
- index, val.Owner.ID, val.ETag, val.Key, val.LastModified, val.Size, val.StorageClass)
- }
- } else if obsError, ok := err.(obs.ObsError); ok {
- fmt.Printf("Code:%s\n", obsError.Code)
- fmt.Printf("Message:%s\n", obsError.Message)
- }
- }
分页列举全部对象
以下代码展示分页列举全部对象:
- // 引入依赖包
- import (
- "fmt"
- "obs"
- )
- var ak = "*** Provide your Access Key ***"
- var sk = "*** Provide your Secret Key ***"
- var endpoint = "https://your-endpoint"
- // 创建ObsClient结构体
- var obsClient, _ = obs.New(ak, sk, endpoint)
- func main() {
- input := &obs.ListObjectsInput{}
- input.Bucket = "bucketname"
- // 设置每页100个对象
- input.MaxKeys = 100
- index := 1
- for {
- output, err := obsClient.ListObjects(input)
- if err == nil {
- fmt.Printf("Page:%d\n", index)
- index++
- for index, val := range output.Contents {
- fmt.Printf("Content[%d]-OwnerId:%s, ETag:%s, Key:%s, LastModified:%s, Size:%d, StorageClass:%s\n",
- index, val.Owner.ID, val.ETag, val.Key, val.LastModified, val.Size, val.StorageClass)
- }
- if output.IsTruncated {
- input.Marker = output.NextMarker
- } else {
- break
- }
- } else {
- if obsError, ok := err.(obs.ObsError); ok {
- fmt.Printf("Code:%s\n", obsError.Code)
- fmt.Printf("Message:%s\n", obsError.Message)
- }
- break
- }
- }
- }
列举文件夹中的所有对象
OBS本身是没有文件夹的概念的,桶中存储的元素只有对象。文件夹对象实际上是一个大小为0且对象名以“/”结尾的对象,将这个文件夹对象名作为前缀,即可模拟列举文件夹中对象的功能。以下代码展示如何列举文件夹中的对象:
- // 引入依赖包
- import (
- "fmt"
- "obs"
- )
- var ak = "*** Provide your Access Key ***"
- var sk = "*** Provide your Secret Key ***"
- var endpoint = "https://your-endpoint"
- // 创建ObsClient结构体
- var obsClient, _ = obs.New(ak, sk, endpoint)
- func main() {
- input := &obs.ListObjectsInput{}
- input.Bucket = "bucketname"
- // 设置每页100个对象
- input.MaxKeys = 100
- // 设置文件夹对象名"dir/"为前缀
- input.Prefix = "dir/"
- // 列举根目录"dir/"下所有对象
- for {
- output, err := obsClient.ListObjects(input)
- if err == nil {
- for index, val := range output.Contents {
- fmt.Printf("Content[%d]-OwnerId:%s, ETag:%s, Key:%s, LastModified:%s, Size:%d, StorageClass:%s\n",
- index, val.Owner.ID, val.ETag, val.Key, val.LastModified, val.Size, val.StorageClass)
- }
- if output.IsTruncated {
- input.Marker = output.NextMarker
- } else {
- break
- }
- } else {
- if obsError, ok := err.(obs.ObsError); ok {
- fmt.Printf("Code:%s\n", obsError.Code)
- fmt.Printf("Message:%s\n", obsError.Message)
- }
- break
- }
- }
- }
按文件夹分组列举所有对象
以下代码展示如何按文件夹分组,列举桶内所有对象:
- // 引入依赖包
- import (
- "fmt"
- "obs"
- )
- var ak = "*** Provide your Access Key ***"
- var sk = "*** Provide your Secret Key ***"
- var endpoint = "https://your-endpoint"
- // 创建ObsClient结构体
- var obsClient, _ = obs.New(ak, sk, endpoint)
- func listObjectsByPrefix(output *obs.ListObjectsOutput) {
- input := &obs.ListObjectsInput{}
- input.Bucket = "bucketname"
- // 设置文件夹分隔符
- input.Delimiter = "/"
- for _, commonPrefix := range output.CommonPrefixes {
- input.Prefix = commonPrefix
- output, err := obsClient.ListObjects(input)
- if err == nil {
- fmt.Printf("Objects in folder [%s]:\n", commonPrefix)
- for index, val := range output.Contents {
- fmt.Printf("Content[%d]-OwnerId:%s, ETag:%s, Key:%s, LastModified:%s, Size:%d, StorageClass:%s\n",
- index, val.Owner.ID, val.ETag, val.Key, val.LastModified, val.Size, val.StorageClass)
- }
- listObjectsByPrefix(output)
- } else if obsError, ok := err.(obs.ObsError); ok {
- fmt.Printf("Code:%s\n", obsError.Code)
- fmt.Printf("Message:%s\n", obsError.Message)
- }
- }
- }
- func main() {
- input := &obs.ListObjectsInput{}
- input.Bucket = "bucketname"
- // 设置文件夹分隔符
- input.Delimiter = "/"
- output, err := obsClient.ListObjects(input)
- if err == nil {
- fmt.Println("Objects in the root directory:")
- for index, val := range output.Contents {
- fmt.Printf("Content[%d]-OwnerId:%s, ETag:%s, Key:%s, LastModified:%s, Size:%d, StorageClass:%s\n",
- index, val.Owner.ID, val.ETag, val.Key, val.LastModified, val.Size, val.StorageClass)
- }
- listObjectsByPrefix(output)
- } else if obsError, ok := err.(obs.ObsError); ok {
- fmt.Printf("Code:%s\n", obsError.Code)
- fmt.Printf("Message:%s\n", obsError.Message)
- }
- }
说明:
- 以上代码示例没有考虑文件夹中对象数超过1000个的情况。
- 由于是需要列举出文件夹中的对象和子文件夹,且文件夹对象总是以“/”结尾,因此Delimiter总是为“/”。
- 每次递归的返回结果中ListObjectsOutput.Contents包含的是文件夹中的对象;ListObjectsOutput.CommonPrefixes包含的是文件夹的子文件夹。
父主题:管理对象