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.

191 lines
4.7 KiB

7 years ago
7 years ago
7 years ago
6 years ago
4 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
4 years ago
4 years ago
6 years ago
4 years ago
4 years ago
4 years ago
7 years ago
4 years ago
7 years ago
4 years ago
4 years ago
7 years ago
7 years ago
  1. package s3api
  2. import (
  3. "context"
  4. "encoding/xml"
  5. "fmt"
  6. "math"
  7. "net/http"
  8. "time"
  9. xhttp "github.com/chrislusf/seaweedfs/weed/s3api/http"
  10. "github.com/chrislusf/seaweedfs/weed/s3api/s3err"
  11. "github.com/aws/aws-sdk-go/aws"
  12. "github.com/aws/aws-sdk-go/service/s3"
  13. "github.com/chrislusf/seaweedfs/weed/glog"
  14. "github.com/chrislusf/seaweedfs/weed/pb/filer_pb"
  15. )
  16. type ListAllMyBucketsResult struct {
  17. XMLName xml.Name `xml:"http://s3.amazonaws.com/doc/2006-03-01/ ListAllMyBucketsResult"`
  18. Owner *s3.Owner
  19. Buckets []*s3.Bucket `xml:"Buckets>Bucket"`
  20. }
  21. func (s3a *S3ApiServer) ListBucketsHandler(w http.ResponseWriter, r *http.Request) {
  22. var response ListAllMyBucketsResult
  23. entries, _, err := s3a.list(s3a.option.BucketsPath, "", "", false, math.MaxInt32)
  24. if err != nil {
  25. writeErrorResponse(w, s3err.ErrInternalError, r.URL)
  26. return
  27. }
  28. identityId := r.Header.Get(xhttp.AmzIdentityId)
  29. var buckets []*s3.Bucket
  30. for _, entry := range entries {
  31. if entry.IsDirectory {
  32. if !s3a.hasAccess(r, entry) {
  33. continue
  34. }
  35. buckets = append(buckets, &s3.Bucket{
  36. Name: aws.String(entry.Name),
  37. CreationDate: aws.Time(time.Unix(entry.Attributes.Crtime, 0).UTC()),
  38. })
  39. }
  40. }
  41. response = ListAllMyBucketsResult{
  42. Owner: &s3.Owner{
  43. ID: aws.String(identityId),
  44. DisplayName: aws.String(identityId),
  45. },
  46. Buckets: buckets,
  47. }
  48. writeSuccessResponseXML(w, encodeResponse(response))
  49. }
  50. func (s3a *S3ApiServer) PutBucketHandler(w http.ResponseWriter, r *http.Request) {
  51. bucket, _ := getBucketAndObject(r)
  52. // avoid duplicated buckets
  53. errCode := s3err.ErrNone
  54. if err := s3a.WithFilerClient(func(client filer_pb.SeaweedFilerClient) error {
  55. if resp, err := client.CollectionList(context.Background(), &filer_pb.CollectionListRequest{
  56. IncludeEcVolumes: true,
  57. IncludeNormalVolumes: true,
  58. }); err != nil {
  59. glog.Errorf("list collection: %v", err)
  60. return fmt.Errorf("list collections: %v", err)
  61. } else {
  62. for _, c := range resp.Collections {
  63. if bucket == c.Name {
  64. errCode = s3err.ErrBucketAlreadyExists
  65. break
  66. }
  67. }
  68. }
  69. return nil
  70. }); err != nil {
  71. writeErrorResponse(w, s3err.ErrInternalError, r.URL)
  72. return
  73. }
  74. if exist, err := s3a.exists(s3a.option.BucketsPath, bucket, true); err == nil && exist {
  75. errCode = s3err.ErrBucketAlreadyExists
  76. }
  77. if errCode != s3err.ErrNone {
  78. writeErrorResponse(w, errCode, r.URL)
  79. return
  80. }
  81. fn := func(entry *filer_pb.Entry) {
  82. if identityId := r.Header.Get(xhttp.AmzIdentityId); identityId != "" {
  83. if entry.Extended == nil {
  84. entry.Extended = make(map[string][]byte)
  85. }
  86. entry.Extended[xhttp.AmzIdentityId] = []byte(identityId)
  87. }
  88. }
  89. // create the folder for bucket, but lazily create actual collection
  90. if err := s3a.mkdir(s3a.option.BucketsPath, bucket, fn); err != nil {
  91. glog.Errorf("PutBucketHandler mkdir: %v", err)
  92. writeErrorResponse(w, s3err.ErrInternalError, r.URL)
  93. return
  94. }
  95. writeSuccessResponseEmpty(w)
  96. }
  97. func (s3a *S3ApiServer) DeleteBucketHandler(w http.ResponseWriter, r *http.Request) {
  98. bucket, _ := getBucketAndObject(r)
  99. if err := s3a.checkBucket(r, bucket); err != s3err.ErrNone {
  100. writeErrorResponse(w, err, r.URL)
  101. return
  102. }
  103. err := s3a.WithFilerClient(func(client filer_pb.SeaweedFilerClient) error {
  104. // delete collection
  105. deleteCollectionRequest := &filer_pb.DeleteCollectionRequest{
  106. Collection: bucket,
  107. }
  108. glog.V(1).Infof("delete collection: %v", deleteCollectionRequest)
  109. if _, err := client.DeleteCollection(context.Background(), deleteCollectionRequest); err != nil {
  110. return fmt.Errorf("delete collection %s: %v", bucket, err)
  111. }
  112. return nil
  113. })
  114. err = s3a.rm(s3a.option.BucketsPath, bucket, false, true)
  115. if err != nil {
  116. writeErrorResponse(w, s3err.ErrInternalError, r.URL)
  117. return
  118. }
  119. writeResponse(w, http.StatusNoContent, nil, mimeNone)
  120. }
  121. func (s3a *S3ApiServer) HeadBucketHandler(w http.ResponseWriter, r *http.Request) {
  122. bucket, _ := getBucketAndObject(r)
  123. if err := s3a.checkBucket(r, bucket); err != s3err.ErrNone {
  124. writeErrorResponse(w, err, r.URL)
  125. return
  126. }
  127. writeSuccessResponseEmpty(w)
  128. }
  129. func (s3a *S3ApiServer) checkBucket(r *http.Request, bucket string) s3err.ErrorCode {
  130. entry, err := s3a.getEntry(s3a.option.BucketsPath, bucket)
  131. if entry == nil || err == filer_pb.ErrNotFound {
  132. return s3err.ErrNoSuchBucket
  133. }
  134. if !s3a.hasAccess(r, entry) {
  135. return s3err.ErrAccessDenied
  136. }
  137. return s3err.ErrNone
  138. }
  139. func (s3a *S3ApiServer) hasAccess(r *http.Request, entry *filer_pb.Entry) bool {
  140. isAdmin := r.Header.Get(xhttp.AmzIsAdmin) != ""
  141. if isAdmin {
  142. return true
  143. }
  144. if entry.Extended == nil {
  145. return true
  146. }
  147. identityId := r.Header.Get(xhttp.AmzIdentityId)
  148. if id, ok := entry.Extended[xhttp.AmzIdentityId]; ok {
  149. if identityId != string(id) {
  150. return false
  151. }
  152. }
  153. return true
  154. }