删除多版本对象
更新时间: 2019-03-14 10:05
删除单个多版本对象
您可以通过ObsClient.DeleteObject接口传入版本号(VersionId)删除多版本对象,示例代码如下:
- // 引入依赖包
- 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.DeleteObjectInput{}
- input.Bucket = "bucketname"
- input.Key = "objectkey"
- input.VersionId = "versionid"
- output, err := obsClient.DeleteObject(input)
- if err == nil {
- fmt.Printf("RequestId:%s\n", output.RequestId)
- } else if obsError, ok := err.(obs.ObsError); ok {
- fmt.Printf("Code:%s\n", obsError.Code)
- fmt.Printf("Message:%s\n", obsError.Message)
- }
- }
批量删除多版本对象
您可以通过ObsClient.DeleteObjects接口传入每个待删除对象的版本号(VersionId)批量删除多版本对象,示例代码如下:
- // 引入依赖包
- 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.DeleteObjectsInput{}
- input.Bucket = "bucketname"
- // 设置为verbose模式
- input.Quiet = false
- var objects [3]obs.ObjectToDelete
- objects[0] = obs.ObjectToDelete{Key: "key1", VersionId: "version1"}
- objects[1] = obs.ObjectToDelete{Key: "key2", VersionId: "version1"}
- objects[2] = obs.ObjectToDelete{Key: "key3", VersionId: "version1"}
- input.Objects = objects[:]
- output, err := obsClient.DeleteObjects(input)
- if err == nil {
- // 获取删除成功的对象
- for index, deleted := range output.Deleteds {
- fmt.Printf("Deleted[%d]-Key:%s, VersionId:%s\n", index, deleted.Key, deleted.VersionId)
- }
- // 获取删除失败的对象
- for index, err := range output.Errors {
- fmt.Printf("Error[%d]-Key:%s, Code:%s\n", index, err.Key, err.Code)
- }
- } else if obsError, ok := err.(obs.ObsError); ok {
- fmt.Printf("Code:%s\n", obsError.Code)
- fmt.Printf("Message:%s\n", obsError.Message)
- }
- }
父主题:多版本控制