列举多版本对象
更新时间: 2019-03-14 10:05
您可以通过ObsClient.ListVersions列举多版本对象。
该接口可设置的参数如下:
参数 | 作用 |
---|---|
ListVersionsInput.Bucket | 桶名。 |
ListVersionsInput.Prefix | 限定返回的对象名必须带有Prefix前缀。 |
ListVersionsInput.KeyMarker | 列举多版本对象的起始位置,返回的对象列表将是对象名按照字典序排序后该参数以后的所有对象。 |
ListVersionsInput.MaxKeys | 列举多版本对象的最大数目,取值范围为1~1000,当超出范围时,按照默认的1000进行处理。 |
ListVersionsInput.Delimiter | 用于对对象名进行分组的字符。对于对象名中包含Delimiter的对象,其对象名(如果请求中指定了Prefix,则此处的对象名需要去掉Prefix)中从首字符至第一个Delimiter之间的字符串将作为一个分组并作为CommonPrefix返回。 |
ListVersionsInput.VersionIdMarker | 与Delimiter配合使用,返回的对象列表将是对象名和版本号按照字典序排序后该参数以后的所有对象。 |
说明:
- 如果VersionIdMarker不是KeyMarker的一个版本号,则该参数无效。
- ObsClient.ListVersions返回结果包含多版本对象和对象删除标记。
简单列举
以下代码展示如何简单列举多版本对象,最多返回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.ListVersionsInput{}
- input.Bucket = "bucketname"
- output, err := obsClient.ListVersions(input)
- if err == nil {
- // 获取多版本对象
- for index, val := range output.Versions {
- fmt.Printf("Version[%d]-OwnerId:%s, ETag:%s, Key:%s, VersionId:%s, LastModified:%s, Size:%d\n",
- index, val.Owner.ID, val.ETag, val.Key, val.VersionId, val.LastModified, val.Size)
- }
- // 获取对象删除标记
- for index, val := range output.DeleteMarkers {
- fmt.Printf("DeleteMarker[%d]-OwnerId:%s, OwnerName:%s, Key:%s, VersionId:%s, LastModified:%s\n",
- index, val.Owner.ID, val.Owner.DisplayName, val.Key, val.VersionId, val.LastModified)
- }
- } else if obsError, ok := err.(obs.ObsError); ok {
- fmt.Printf("Code:%s\n", obsError.Code)
- fmt.Printf("Message:%s\n", obsError.Message)
- }
- }
说明:
- 每次至多返回1000个多版本对象,如果指定桶包含的对象数量大于1000,则返回结果中ListVersionsOutput.IsTruncated为true表明本次没有返回全部对象,并可通过ListVersionsOutput.NextKeyMarker和ListVersionsOutput.NextVersionIdMarker获取下次列举的起始位置。
- 如果想获取指定桶包含的所有多版本对象,可以采用分页列举的方式。
指定数目列举
以下代码展示如何指定数目列举多版本对象:
- // 引入依赖包
- 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.ListVersionsInput{}
- input.Bucket = "bucketname"
- // 列举100个多版本对象
- input.MaxKeys = 100
- output, err := obsClient.ListVersions(input)
- if err == nil {
- // 获取多版本对象
- for index, val := range output.Versions {
- fmt.Printf("Version[%d]-OwnerId:%s, ETag:%s, Key:%s, VersionId:%s, LastModified:%s, Size:%d\n",
- index, val.Owner.ID, val.ETag, val.Key, val.VersionId, val.LastModified, val.Size)
- }
- // 获取对象删除标记
- for index, val := range output.DeleteMarkers {
- fmt.Printf("DeleteMarker[%d]-OwnerId:%s, Key:%s, VersionId:%s, LastModified:%s\n",
- index, val.Owner.ID, val.Key, val.VersionId, val.LastModified)
- }
- } 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.ListVersionsInput{}
- input.Bucket = "bucketname"
- // 设置列举带有prefix前缀的100个多版本对象
- input.MaxKeys = 100
- input.Prefix = "prefix"
- output, err := obsClient.ListVersions(input)
- if err == nil {
- // 获取多版本对象
- for index, val := range output.Versions {
- fmt.Printf("Version[%d]-OwnerId:%s, ETag:%s, Key:%s, VersionId:%s, LastModified:%s, Size:%d\n",
- index, val.Owner.ID, val.ETag, val.Key, val.VersionId, val.LastModified, val.Size)
- }
- // 获取对象删除标记
- for index, val := range output.DeleteMarkers {
- fmt.Printf("DeleteMarker[%d]-OwnerId:%s, Key:%s, VersionId:%s, LastModified:%s\n",
- index, val.Owner.ID, val.Key, val.VersionId, val.LastModified)
- }
- } 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://yourdomainname"
- // 创建ObsClient结构体
- var obsClient, _ = obs.New(ak, sk, endpoint)
- func main() {
- input := &obs.ListVersionsInput{}
- input.Bucket = "bucketname"
- // 设置列举对象名字典序在"test"之后的100个多版本对象
- input.MaxKeys = 100
- input.Marker = "test"
- output, err := obsClient.ListVersions(input)
- if err == nil {
- // 获取多版本对象
- for index, val := range output.Versions {
- fmt.Printf("Version[%d]-OwnerId:%s, ETag:%s, Key:%s, VersionId:%s, LastModified:%s, Size:%d\n",
- index, val.Owner.ID, val.Owner.DisplayName, val.ETag, val.Key, val.VersionId, val.LastModified, val.Size)
- }
- // 获取对象删除标记
- for index, val := range output.DeleteMarkers {
- fmt.Printf("DeleteMarker[%d]-OwnerId:%s, Key:%s, VersionId:%s, LastModified:%s\n",
- index, val.Owner.ID, val.Key, val.VersionId, val.LastModified)
- }
- } 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.ListVersionsInput{}
- input.Bucket = "bucketname"
- input.MaxKeys = 100
- index := 1
- for {
- output, err := obsClient.ListVersions(input)
- if err == nil {
- fmt.Printf("Page:%d", index)
- index++
- // 获取多版本对象
- for index, val := range output.Versions {
- fmt.Printf("Version[%d]-OwnerId:%s, ETag:%s, Key:%s, VersionId:%s, LastModified:%s, Size:%d\n",
- index, val.Owner.ID, val.ETag, val.Key, val.VersionId, val.LastModified, val.Size)
- }
- // 获取对象删除标记
- for index, val := range output.DeleteMarkers {
- fmt.Printf("DeleteMarker[%d]-OwnerId:%s, Key:%s, VersionId:%s, LastModified:%s\n",
- index, val.Owner.ID, val.Key, val.VersionId, val.LastModified)
- }
- if output.IsTruncated {
- input.KeyMarker = output.NextKeyMarker
- input.VersionIdMarker = output.NextVersionIdMarker
- } 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.ListVersionsInput{}
- input.Bucket = "bucketname"
- input.MaxKeys = 100
- // 设置根目录"dir/"
- input.Prefix = "dir/"
- for {
- output, err := obsClient.ListVersions(input)
- if err == nil {
- // 获取多版本对象
- for index, val := range output.Versions {
- fmt.Printf("Version[%d]-OwnerId:%s, ETag:%s, Key:%s, VersionId:%s, LastModified:%s, Size:%d\n",
- index, val.Owner.ID, val.ETag, val.Key, val.VersionId, val.LastModified, val.Size)
- }
- // 获取对象删除标记
- for index, val := range output.DeleteMarkers {
- fmt.Printf("DeleteMarker[%d]-OwnerId:%s, Key:%s, VersionId:%s, LastModified:%s\n",
- index, val.Owner.ID, val.Key, val.VersionId, val.LastModified)
- }
- if output.IsTruncated {
- input.KeyMarker = output.NextKeyMarker
- input.VersionIdMarker = output.NextVersionIdMarker
- } 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 listVersionsByPrefix(output *obs.ListVersionsOutput) {
- input := &obs.ListVersionsInput{}
- input.Bucket = "bucketname"
- // 设置文件夹分隔符
- input.Delimiter = "/"
- for _, commonPrefix := range output.CommonPrefixes {
- input.Prefix = commonPrefix
- output, err := obsClient.ListVersions(input)
- if err == nil {
- fmt.Printf("Objects in folder [%s]:\n", commonPrefix)
- // 获取多版本对象
- for index, val := range output.Versions {
- fmt.Printf("Version[%d]-OwnerId:%s, ETag:%s, Key:%s, VersionId:%s, LastModified:%s, Size:%d\n",
- index, val.Owner.ID, val.ETag, val.Key, val.VersionId, val.LastModified, val.Size)
- }
- // 获取对象删除标记
- for index, val := range output.DeleteMarkers {
- fmt.Printf("DeleteMarker[%d]-OwnerId:%s, Key:%s, VersionId:%s, LastModified:%s\n",
- index, val.Owner.ID, val.Key, val.VersionId, val.LastModified)
- }
- listVersionsByPrefix(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.ListVersionsInput{}
- input.Bucket = "bucketname"
- // 设置文件夹分隔符
- input.Delimiter = "/"
- output, err := obsClient.ListVersions(input)
- if err == nil {
- fmt.Println("Objects in the root directory:")
- // 获取多版本对象
- for index, val := range output.Versions {
- fmt.Printf("Version[%d]-OwnerId:%s, ETag:%s, Key:%s, VersionId:%s, LastModified:%s, Size:%d\n",
- index, val.Owner.ID, val.ETag, val.Key, val.VersionId, val.LastModified, val.Size)
- }
- // 获取对象删除标记
- for index, val := range output.DeleteMarkers {
- fmt.Printf("DeleteMarker[%d]-OwnerId:%s, Key:%s, VersionId:%s, LastModified:%s\n",
- index, val.Owner.ID, val.Key, val.VersionId, val.LastModified)
- }
- listVersionsByPrefix(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总是为“/”。
- 每次递归的返回结果中ListVersionsOutput.Versions包含的是文件夹中的多版本对象;ListVersionsOutput.DeleteMarkers包含的是文件夹中的删除标记;ListVersionsOutput.CommonPrefixes包含的是文件夹的子文件夹。
父主题:多版本控制