删除多版本对象

更新时间: 2019-03-14 10:05

删除单个多版本对象

您可以通过ObsClient.DeleteObject接口传入版本号(VersionId)删除多版本对象,示例代码如下:

  1. // 引入依赖包
  2. import (
  3. "fmt"
  4. "obs"
  5. )
  6.  
  7. var ak = "*** Provide your Access Key ***"
  8. var sk = "*** Provide your Secret Key ***"
  9. var endpoint = "https://your-endpoint"
  10.  
  11. // 创建ObsClient结构体
  12. var obsClient, _ = obs.New(ak, sk, endpoint)
  13.  
  14. func main() {
  15. input := &obs.DeleteObjectInput{}
  16. input.Bucket = "bucketname"
  17. input.Key = "objectkey"
  18. input.VersionId = "versionid"
  19.  
  20. output, err := obsClient.DeleteObject(input)
  21. if err == nil {
  22. fmt.Printf("RequestId:%s\n", output.RequestId)
  23. } else if obsError, ok := err.(obs.ObsError); ok {
  24. fmt.Printf("Code:%s\n", obsError.Code)
  25. fmt.Printf("Message:%s\n", obsError.Message)
  26. }
  27. }

批量删除多版本对象

您可以通过ObsClient.DeleteObjects接口传入每个待删除对象的版本号(VersionId)批量删除多版本对象,示例代码如下:

  1. // 引入依赖包
  2. import (
  3. "fmt"
  4. "obs"
  5. )
  6.  
  7. var ak = "*** Provide your Access Key ***"
  8. var sk = "*** Provide your Secret Key ***"
  9. var endpoint = "https://your-endpoint"
  10.  
  11. // 创建ObsClient结构体
  12. var obsClient, _ = obs.New(ak, sk, endpoint)
  13.  
  14. func main() {
  15. input := &obs.DeleteObjectsInput{}
  16. input.Bucket = "bucketname"
  17. // 设置为verbose模式
  18. input.Quiet = false
  19. var objects [3]obs.ObjectToDelete
  20. objects[0] = obs.ObjectToDelete{Key: "key1", VersionId: "version1"}
  21. objects[1] = obs.ObjectToDelete{Key: "key2", VersionId: "version1"}
  22. objects[2] = obs.ObjectToDelete{Key: "key3", VersionId: "version1"}
  23.  
  24. input.Objects = objects[:]
  25.  
  26. output, err := obsClient.DeleteObjects(input)
  27. if err == nil {
  28. // 获取删除成功的对象
  29. for index, deleted := range output.Deleteds {
  30. fmt.Printf("Deleted[%d]-Key:%s, VersionId:%s\n", index, deleted.Key, deleted.VersionId)
  31. }
  32. // 获取删除失败的对象
  33. for index, err := range output.Errors {
  34. fmt.Printf("Error[%d]-Key:%s, Code:%s\n", index, err.Key, err.Code)
  35. }
  36. } else if obsError, ok := err.(obs.ObsError); ok {
  37. fmt.Printf("Code:%s\n", obsError.Code)
  38. fmt.Printf("Message:%s\n", obsError.Message)
  39. }
  40. }

父主题:多版本控制