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.

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