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.

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