You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

192 lines
4.9 KiB

7 years ago
7 years ago
7 years ago
6 years ago
7 years ago
  1. package s3api
  2. import (
  3. "context"
  4. "fmt"
  5. "net/http"
  6. "net/url"
  7. "path/filepath"
  8. "strconv"
  9. "strings"
  10. "time"
  11. "github.com/chrislusf/seaweedfs/weed/filer2"
  12. "github.com/chrislusf/seaweedfs/weed/glog"
  13. "github.com/chrislusf/seaweedfs/weed/pb/filer_pb"
  14. "github.com/gorilla/mux"
  15. )
  16. const (
  17. maxObjectListSizeLimit = 1000 // Limit number of objects in a listObjectsResponse.
  18. )
  19. func (s3a *S3ApiServer) ListObjectsV2Handler(w http.ResponseWriter, r *http.Request) {
  20. // https://docs.aws.amazon.com/AmazonS3/latest/API/v2-RESTBucketGET.html
  21. // collect parameters
  22. vars := mux.Vars(r)
  23. bucket := vars["bucket"]
  24. glog.V(4).Infof("read v2: %v", vars)
  25. originalPrefix, marker, startAfter, delimiter, _, maxKeys := getListObjectsV2Args(r.URL.Query())
  26. if maxKeys < 0 {
  27. writeErrorResponse(w, ErrInvalidMaxKeys, r.URL)
  28. return
  29. }
  30. if delimiter != "" && delimiter != "/" {
  31. writeErrorResponse(w, ErrNotImplemented, r.URL)
  32. return
  33. }
  34. if marker == "" {
  35. marker = startAfter
  36. }
  37. ctx := context.Background()
  38. response, err := s3a.listFilerEntries(ctx, bucket, originalPrefix, maxKeys, marker)
  39. if err != nil {
  40. writeErrorResponse(w, ErrInternalError, r.URL)
  41. return
  42. }
  43. writeSuccessResponseXML(w, encodeResponse(response))
  44. }
  45. func (s3a *S3ApiServer) ListObjectsV1Handler(w http.ResponseWriter, r *http.Request) {
  46. // https://docs.aws.amazon.com/AmazonS3/latest/API/RESTBucketGET.html
  47. // collect parameters
  48. vars := mux.Vars(r)
  49. bucket := vars["bucket"]
  50. ctx := context.Background()
  51. originalPrefix, marker, delimiter, maxKeys := getListObjectsV1Args(r.URL.Query())
  52. if maxKeys < 0 {
  53. writeErrorResponse(w, ErrInvalidMaxKeys, r.URL)
  54. return
  55. }
  56. if delimiter != "" && delimiter != "/" {
  57. writeErrorResponse(w, ErrNotImplemented, r.URL)
  58. return
  59. }
  60. response, err := s3a.listFilerEntries(ctx, bucket, originalPrefix, maxKeys, marker)
  61. if err != nil {
  62. writeErrorResponse(w, ErrInternalError, r.URL)
  63. return
  64. }
  65. writeSuccessResponseXML(w, encodeResponse(response))
  66. }
  67. func (s3a *S3ApiServer) listFilerEntries(ctx context.Context, bucket, originalPrefix string, maxKeys int, marker string) (response ListBucketResult, err error) {
  68. // convert full path prefix into directory name and prefix for entry name
  69. dir, prefix := filepath.Split(originalPrefix)
  70. if strings.HasPrefix(dir, "/") {
  71. dir = dir[1:]
  72. }
  73. // check filer
  74. err = s3a.withFilerClient(ctx, func(client filer_pb.SeaweedFilerClient) error {
  75. request := &filer_pb.ListEntriesRequest{
  76. Directory: fmt.Sprintf("%s/%s/%s", s3a.option.BucketsPath, bucket, dir),
  77. Prefix: prefix,
  78. Limit: uint32(maxKeys + 1),
  79. StartFromFileName: marker,
  80. InclusiveStartFrom: false,
  81. }
  82. resp, err := client.ListEntries(ctx, request)
  83. if err != nil {
  84. return fmt.Errorf("list buckets: %v", err)
  85. }
  86. var contents []ListEntry
  87. var commonPrefixes []PrefixEntry
  88. var counter int
  89. var lastEntryName string
  90. var isTruncated bool
  91. for _, entry := range resp.Entries {
  92. counter++
  93. if counter > maxKeys {
  94. isTruncated = true
  95. break
  96. }
  97. lastEntryName = entry.Name
  98. if entry.IsDirectory {
  99. if entry.Name != ".uploads" {
  100. commonPrefixes = append(commonPrefixes, PrefixEntry{
  101. Prefix: fmt.Sprintf("%s%s/", dir, entry.Name),
  102. })
  103. }
  104. } else {
  105. contents = append(contents, ListEntry{
  106. Key: fmt.Sprintf("%s%s", dir, entry.Name),
  107. LastModified: time.Unix(entry.Attributes.Mtime, 0),
  108. ETag: "\"" + filer2.ETag(entry.Chunks) + "\"",
  109. Size: int64(filer2.TotalSize(entry.Chunks)),
  110. Owner: CanonicalUser{
  111. ID: fmt.Sprintf("%x", entry.Attributes.Uid),
  112. DisplayName: entry.Attributes.UserName,
  113. },
  114. StorageClass: "STANDARD",
  115. })
  116. }
  117. }
  118. response = ListBucketResult{
  119. Name: bucket,
  120. Prefix: originalPrefix,
  121. Marker: marker,
  122. NextMarker: lastEntryName,
  123. MaxKeys: maxKeys,
  124. Delimiter: "/",
  125. IsTruncated: isTruncated,
  126. Contents: contents,
  127. CommonPrefixes: commonPrefixes,
  128. }
  129. glog.V(4).Infof("read directory: %v, found: %v, %+v", request, counter, response)
  130. return nil
  131. })
  132. return
  133. }
  134. func getListObjectsV2Args(values url.Values) (prefix, token, startAfter, delimiter string, fetchOwner bool, maxkeys int) {
  135. prefix = values.Get("prefix")
  136. token = values.Get("continuation-token")
  137. startAfter = values.Get("start-after")
  138. delimiter = values.Get("delimiter")
  139. if values.Get("max-keys") != "" {
  140. maxkeys, _ = strconv.Atoi(values.Get("max-keys"))
  141. } else {
  142. maxkeys = maxObjectListSizeLimit
  143. }
  144. fetchOwner = values.Get("fetch-owner") == "true"
  145. return
  146. }
  147. func getListObjectsV1Args(values url.Values) (prefix, marker, delimiter string, maxkeys int) {
  148. prefix = values.Get("prefix")
  149. marker = values.Get("marker")
  150. delimiter = values.Get("delimiter")
  151. if values.Get("max-keys") != "" {
  152. maxkeys, _ = strconv.Atoi(values.Get("max-keys"))
  153. } else {
  154. maxkeys = maxObjectListSizeLimit
  155. }
  156. return
  157. }