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 展示了列举文件的用法,包括默认参数列举、指定参数列举
  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. // 创建Bucket
  18. bucket, err := GetTestBucket(bucketName)
  19. if err != nil {
  20. HandleError(err)
  21. }
  22. // 创建object
  23. err = CreateObjects(bucket, myObjects)
  24. if err != nil {
  25. HandleError(err)
  26. }
  27. // 场景1:使用默认参数参数
  28. lor, err := bucket.ListObjects()
  29. if err != nil {
  30. HandleError(err)
  31. }
  32. fmt.Println("my objects:", getObjectsFormResponse(lor))
  33. // 场景2:指定最大返回数量
  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. // 场景3:返回指定前缀的Bucket
  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. // 场景4:指定从某个之后返回
  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. // 场景5:分页获取所有object,每次返回3个
  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. // 场景6:分页所有获取从某个之后的object,每次返回3个
  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. // 场景7:分页所有获取前缀的object,每次返回2个
  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. // 场景8:prefix和delimiter结合,完成分组功能,ListObjectsResponse.Objects表示不再组中,
  97. // ListObjectsResponse.CommonPrefixes分组结果
  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. // 创建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. // 删除object和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. }