bucket_inventory.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. package sample
  2. import (
  3. "fmt"
  4. "github.com/aliyun/aliyun-oss-go-sdk/oss"
  5. )
  6. // BucketInventorySample shows how to set, get, list and delete the bucket inventory configuration
  7. func BucketInventorySample() {
  8. // New client
  9. client, err := oss.New(endpoint, accessID, accessKey)
  10. if err != nil {
  11. HandleError(err)
  12. }
  13. // Create the bucket with default parameters
  14. err = client.CreateBucket(bucketName)
  15. if err != nil {
  16. HandleError(err)
  17. }
  18. // the inventory configuration,not use any encryption
  19. bl := true
  20. invConfig := oss.InventoryConfiguration{
  21. Id: "report1",
  22. IsEnabled: &bl,
  23. Prefix: "filterPrefix/",
  24. OSSBucketDestination: oss.OSSBucketDestination{
  25. Format: "CSV",
  26. AccountId: accountID,
  27. RoleArn: stsARN,
  28. Bucket: "acs:oss:::" + bucketName,
  29. Prefix: "prefix1",
  30. },
  31. Frequency: "Daily",
  32. IncludedObjectVersions: "All",
  33. OptionalFields: oss.OptionalFields{
  34. Field: []string{
  35. "Size", "LastModifiedDate", "ETag", "StorageClass", "IsMultipartUploaded", "EncryptionStatus",
  36. },
  37. },
  38. }
  39. // case 1: Set inventory
  40. err = client.SetBucketInventory(bucketName, invConfig)
  41. if err != nil {
  42. HandleError(err)
  43. }
  44. // case 2: Get Bucket inventory
  45. ret, err := client.GetBucketInventory(bucketName, invConfig.Id)
  46. if err != nil {
  47. HandleError(err)
  48. }
  49. fmt.Println("Bucket inventory:", ret)
  50. // case 3: List Bucket inventory
  51. invConfig2 := oss.InventoryConfiguration{
  52. Id: "report2",
  53. IsEnabled: &bl,
  54. Prefix: "filterPrefix/",
  55. OSSBucketDestination: oss.OSSBucketDestination{
  56. Format: "CSV",
  57. AccountId: accountID,
  58. RoleArn: stsARN,
  59. Bucket: "acs:oss:::" + bucketName,
  60. Prefix: "prefix1",
  61. },
  62. Frequency: "Daily",
  63. IncludedObjectVersions: "All",
  64. OptionalFields: oss.OptionalFields{
  65. Field: []string{
  66. "Size", "LastModifiedDate", "ETag", "StorageClass", "IsMultipartUploaded", "EncryptionStatus",
  67. },
  68. },
  69. }
  70. err = client.SetBucketInventory(bucketName, invConfig2)
  71. if err != nil {
  72. HandleError(err)
  73. }
  74. NextContinuationToken := ""
  75. listInvConf, err := client.ListBucketInventory(bucketName, NextContinuationToken)
  76. if err != nil {
  77. HandleError(err)
  78. }
  79. fmt.Println("Bucket inventory list:", listInvConf)
  80. // case 4: Delete Bucket inventory
  81. err = client.DeleteBucketInventory(bucketName, invConfig.Id)
  82. if err != nil {
  83. HandleError(err)
  84. }
  85. // Delete bucket
  86. err = client.DeleteBucket(bucketName)
  87. if err != nil {
  88. HandleError(err)
  89. }
  90. fmt.Println("BucketInventorySample completed")
  91. }