list_objects.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. package sample
  2. import (
  3. "fmt"
  4. "github.com/aliyun/aliyun-oss-go-sdk/oss"
  5. )
  6. // ListObjectsSample shows the file list, including default and specified parameters.
  7. func ListObjectsSample() {
  8. var myObjects = []Object{
  9. {"my-object-1", ""},
  10. {"my-object-11", ""},
  11. {"my-object-2", ""},
  12. {"my-object-21", ""},
  13. {"my-object-22", ""},
  14. {"my-object-3", ""},
  15. {"my-object-31", ""},
  16. {"my-object-32", ""}}
  17. // Create bucket
  18. bucket, err := GetTestBucket(bucketName)
  19. if err != nil {
  20. HandleError(err)
  21. }
  22. // Create objects
  23. err = CreateObjects(bucket, myObjects)
  24. if err != nil {
  25. HandleError(err)
  26. }
  27. // Case 1: Use default parameters
  28. lor, err := bucket.ListObjects()
  29. if err != nil {
  30. HandleError(err)
  31. }
  32. fmt.Println("my objects:", getObjectsFormResponse(lor))
  33. // Case 2: Specify max keys
  34. lor, err = bucket.ListObjects(oss.MaxKeys(3))
  35. if err != nil {
  36. HandleError(err)
  37. }
  38. fmt.Println("my objects max num:", getObjectsFormResponse(lor))
  39. // Case 3: Specify prefix of objects
  40. lor, err = bucket.ListObjects(oss.Prefix("my-object-2"))
  41. if err != nil {
  42. HandleError(err)
  43. }
  44. fmt.Println("my objects prefix :", getObjectsFormResponse(lor))
  45. // Case 4: Specify the marker
  46. lor, err = bucket.ListObjects(oss.Marker("my-object-22"))
  47. if err != nil {
  48. HandleError(err)
  49. }
  50. fmt.Println("my objects marker :", getObjectsFormResponse(lor))
  51. // Case 5: List object with paging. each page has 3 objects
  52. marker := oss.Marker("")
  53. for {
  54. lor, err = bucket.ListObjects(oss.MaxKeys(3), marker)
  55. if err != nil {
  56. HandleError(err)
  57. }
  58. marker = oss.Marker(lor.NextMarker)
  59. fmt.Println("my objects page :", getObjectsFormResponse(lor))
  60. if !lor.IsTruncated {
  61. break
  62. }
  63. }
  64. // Case 6: List object with paging , marker and max keys; return 3 items each time.
  65. marker = oss.Marker("my-object-22")
  66. for {
  67. lor, err = bucket.ListObjects(oss.MaxKeys(3), marker)
  68. if err != nil {
  69. HandleError(err)
  70. }
  71. marker = oss.Marker(lor.NextMarker)
  72. fmt.Println("my objects marker&page :", getObjectsFormResponse(lor))
  73. if !lor.IsTruncated {
  74. break
  75. }
  76. }
  77. // Case 7: List object with paging , with prefix and max keys; return 2 items each time.
  78. pre := oss.Prefix("my-object-2")
  79. marker = oss.Marker("")
  80. for {
  81. lor, err = bucket.ListObjects(oss.MaxKeys(2), marker, pre)
  82. if err != nil {
  83. HandleError(err)
  84. }
  85. pre = oss.Prefix(lor.Prefix)
  86. marker = oss.Marker(lor.NextMarker)
  87. fmt.Println("my objects prefix&page :", getObjectsFormResponse(lor))
  88. if !lor.IsTruncated {
  89. break
  90. }
  91. }
  92. err = DeleteObjects(bucket, myObjects)
  93. if err != nil {
  94. HandleError(err)
  95. }
  96. // Case 8: Combine the prefix and delimiter for grouping. ListObjectsResponse.Objects is the objects returned.
  97. // ListObjectsResponse.CommonPrefixes is the common prefixes returned.
  98. myObjects = []Object{
  99. {"fun/test.txt", ""},
  100. {"fun/test.jpg", ""},
  101. {"fun/movie/001.avi", ""},
  102. {"fun/movie/007.avi", ""},
  103. {"fun/music/001.mp3", ""},
  104. {"fun/music/001.mp3", ""}}
  105. // Create object
  106. err = CreateObjects(bucket, myObjects)
  107. if err != nil {
  108. HandleError(err)
  109. }
  110. lor, err = bucket.ListObjects(oss.Prefix("fun/"), oss.Delimiter("/"))
  111. if err != nil {
  112. HandleError(err)
  113. }
  114. fmt.Println("my objects prefix :", getObjectsFormResponse(lor),
  115. "common prefixes:", lor.CommonPrefixes)
  116. // Delete object and bucket
  117. err = DeleteTestBucketAndObject(bucketName)
  118. if err != nil {
  119. HandleError(err)
  120. }
  121. fmt.Println("ListObjectsSample completed")
  122. }
  123. func getObjectsFormResponse(lor oss.ListObjectsResult) string {
  124. var output string
  125. for _, object := range lor.Objects {
  126. output += object.Key + " "
  127. }
  128. return output
  129. }