list_buckets.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. package sample
  2. import (
  3. "fmt"
  4. "github.com/aliyun/aliyun-oss-go-sdk/oss"
  5. )
  6. // ListBucketsSample shows the list bucket, including default and specified parameters.
  7. func ListBucketsSample() {
  8. var myBuckets = []string{
  9. "my-bucket-1",
  10. "my-bucket-11",
  11. "my-bucket-2",
  12. "my-bucket-21",
  13. "my-bucket-22",
  14. "my-bucket-3",
  15. "my-bucket-31",
  16. "my-bucket-32"}
  17. // New client
  18. client, err := oss.New(endpoint, accessID, accessKey)
  19. if err != nil {
  20. HandleError(err)
  21. }
  22. // Remove other bucket
  23. lbr, err := client.ListBuckets()
  24. if err != nil {
  25. HandleError(err)
  26. }
  27. for _, bucket := range lbr.Buckets {
  28. err = client.DeleteBucket(bucket.Name)
  29. if err != nil {
  30. //HandleError(err)
  31. }
  32. }
  33. // Create bucket
  34. for _, bucketName := range myBuckets {
  35. err = client.CreateBucket(bucketName)
  36. if err != nil {
  37. HandleError(err)
  38. }
  39. }
  40. // Case 1: Use default parameter
  41. lbr, err = client.ListBuckets()
  42. if err != nil {
  43. HandleError(err)
  44. }
  45. fmt.Println("my buckets:", lbr.Buckets)
  46. // Case 2: Specify the max keys : 3
  47. lbr, err = client.ListBuckets(oss.MaxKeys(3))
  48. if err != nil {
  49. HandleError(err)
  50. }
  51. fmt.Println("my buckets max num:", lbr.Buckets)
  52. // Case 3: Specify the prefix of buckets.
  53. lbr, err = client.ListBuckets(oss.Prefix("my-bucket-2"))
  54. if err != nil {
  55. HandleError(err)
  56. }
  57. fmt.Println("my buckets prefix :", lbr.Buckets)
  58. // Case 4: Specify the marker to return from a certain one
  59. lbr, err = client.ListBuckets(oss.Marker("my-bucket-22"))
  60. if err != nil {
  61. HandleError(err)
  62. }
  63. fmt.Println("my buckets marker :", lbr.Buckets)
  64. // Case 5: Specify max key and list all buckets with paging, return 3 items each time.
  65. marker := oss.Marker("")
  66. for {
  67. lbr, err = client.ListBuckets(oss.MaxKeys(3), marker)
  68. if err != nil {
  69. HandleError(err)
  70. }
  71. marker = oss.Marker(lbr.NextMarker)
  72. fmt.Println("my buckets page :", lbr.Buckets)
  73. if !lbr.IsTruncated {
  74. break
  75. }
  76. }
  77. // Case 6: List bucket with marker and max key; return 3 items each time.
  78. marker = oss.Marker("my-bucket-22")
  79. for {
  80. lbr, err = client.ListBuckets(oss.MaxKeys(3), marker)
  81. if err != nil {
  82. HandleError(err)
  83. }
  84. marker = oss.Marker(lbr.NextMarker)
  85. fmt.Println("my buckets marker&page :", lbr.Buckets)
  86. if !lbr.IsTruncated {
  87. break
  88. }
  89. }
  90. // Case 7: List bucket with prefix and max key, return 3 items each time.
  91. pre := oss.Prefix("my-bucket-2")
  92. marker = oss.Marker("")
  93. for {
  94. lbr, err = client.ListBuckets(oss.MaxKeys(3), pre, marker)
  95. if err != nil {
  96. HandleError(err)
  97. }
  98. pre = oss.Prefix(lbr.Prefix)
  99. marker = oss.Marker(lbr.NextMarker)
  100. fmt.Println("my buckets prefix&page :", lbr.Buckets)
  101. if !lbr.IsTruncated {
  102. break
  103. }
  104. }
  105. // Delete bucket
  106. for _, bucketName := range myBuckets {
  107. err = client.DeleteBucket(bucketName)
  108. if err != nil {
  109. HandleError(err)
  110. }
  111. }
  112. fmt.Println("ListsBucketSample completed")
  113. }