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.

200 lines
5.0 KiB

7 years ago
7 years ago
7 years ago
6 years ago
7 years ago
4 years ago
4 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, delimiter)
  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, delimiter)
  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, delimiter 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. prefix = fmt.Sprintf("%s%s/", dir, entry.Name)
  102. commonPrefixes = append(commonPrefixes, PrefixEntry{
  103. Prefix: prefix,
  104. })
  105. if delimiter != "/" {
  106. response, _ := s3a.listFilerEntries(bucket, prefix, maxKeys, marker, delimiter)
  107. for _, content := range response.Contents {
  108. contents = append(contents, content)
  109. }
  110. }
  111. }
  112. } else {
  113. contents = append(contents, ListEntry{
  114. Key: fmt.Sprintf("%s%s", dir, entry.Name),
  115. LastModified: time.Unix(entry.Attributes.Mtime, 0).UTC(),
  116. ETag: "\"" + filer2.ETag(entry) + "\"",
  117. Size: int64(filer2.TotalSize(entry.Chunks)),
  118. Owner: CanonicalUser{
  119. ID: fmt.Sprintf("%x", entry.Attributes.Uid),
  120. DisplayName: entry.Attributes.UserName,
  121. },
  122. StorageClass: "STANDARD",
  123. })
  124. }
  125. }
  126. response = ListBucketResult{
  127. Name: bucket,
  128. Prefix: originalPrefix,
  129. Marker: marker,
  130. NextMarker: lastEntryName,
  131. MaxKeys: maxKeys,
  132. Delimiter: "/",
  133. IsTruncated: isTruncated,
  134. Contents: contents,
  135. CommonPrefixes: commonPrefixes,
  136. }
  137. glog.V(4).Infof("read directory: %v, found: %v, %+v", request, counter, response)
  138. return nil
  139. })
  140. return
  141. }
  142. func getListObjectsV2Args(values url.Values) (prefix, token, startAfter, delimiter string, fetchOwner bool, maxkeys int) {
  143. prefix = values.Get("prefix")
  144. token = values.Get("continuation-token")
  145. startAfter = values.Get("start-after")
  146. delimiter = values.Get("delimiter")
  147. if values.Get("max-keys") != "" {
  148. maxkeys, _ = strconv.Atoi(values.Get("max-keys"))
  149. } else {
  150. maxkeys = maxObjectListSizeLimit
  151. }
  152. fetchOwner = values.Get("fetch-owner") == "true"
  153. return
  154. }
  155. func getListObjectsV1Args(values url.Values) (prefix, marker, delimiter string, maxkeys int) {
  156. prefix = values.Get("prefix")
  157. marker = values.Get("marker")
  158. delimiter = values.Get("delimiter")
  159. if values.Get("max-keys") != "" {
  160. maxkeys, _ = strconv.Atoi(values.Get("max-keys"))
  161. } else {
  162. maxkeys = maxObjectListSizeLimit
  163. }
  164. return
  165. }