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.7 KiB

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