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.

198 lines
4.8 KiB

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