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.

173 lines
4.3 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
5 years ago
4 years ago
6 years ago
4 years ago
5 years ago
5 years ago
4 years ago
7 years ago
5 years ago
7 years ago
5 years ago
7 years ago
7 years ago
4 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 id, ok := entry.Extended[xhttp.AmzIdentityId]; ok {
  33. if identityId != string(id) {
  34. continue
  35. }
  36. }
  37. buckets = append(buckets, &s3.Bucket{
  38. Name: aws.String(entry.Name),
  39. CreationDate: aws.Time(time.Unix(entry.Attributes.Crtime, 0).UTC()),
  40. })
  41. }
  42. }
  43. response = ListAllMyBucketsResult{
  44. Owner: &s3.Owner{
  45. ID: aws.String(identityId),
  46. DisplayName: aws.String(identityId),
  47. },
  48. Buckets: buckets,
  49. }
  50. writeSuccessResponseXML(w, encodeResponse(response))
  51. }
  52. func (s3a *S3ApiServer) PutBucketHandler(w http.ResponseWriter, r *http.Request) {
  53. bucket, _ := getBucketAndObject(r)
  54. // avoid duplicated buckets
  55. errCode := s3err.ErrNone
  56. if err := s3a.WithFilerClient(func(client filer_pb.SeaweedFilerClient) error {
  57. if resp, err := client.CollectionList(context.Background(), &filer_pb.CollectionListRequest{
  58. IncludeEcVolumes: true,
  59. IncludeNormalVolumes: true,
  60. }); err != nil {
  61. glog.Errorf("list collection: %v", err)
  62. return fmt.Errorf("list collections: %v", err)
  63. } else {
  64. for _, c := range resp.Collections {
  65. if bucket == c.Name {
  66. errCode = s3err.ErrBucketAlreadyExists
  67. break
  68. }
  69. }
  70. }
  71. return nil
  72. }); err != nil {
  73. writeErrorResponse(w, s3err.ErrInternalError, r.URL)
  74. return
  75. }
  76. if errCode != s3err.ErrNone {
  77. writeErrorResponse(w, errCode, r.URL)
  78. return
  79. }
  80. fn := func(entry *filer_pb.Entry) {
  81. if identityId := r.Header.Get(xhttp.AmzIdentityId); identityId != "" {
  82. if entry.Extended == nil {
  83. entry.Extended = make(map[string][]byte)
  84. }
  85. entry.Extended[xhttp.AmzIdentityId] = []byte(identityId)
  86. }
  87. }
  88. // create the folder for bucket, but lazily create actual collection
  89. if err := s3a.mkdir(s3a.option.BucketsPath, bucket, fn); err != nil {
  90. glog.Errorf("PutBucketHandler mkdir: %v", err)
  91. writeErrorResponse(w, s3err.ErrInternalError, r.URL)
  92. return
  93. }
  94. writeSuccessResponseEmpty(w)
  95. }
  96. func (s3a *S3ApiServer) DeleteBucketHandler(w http.ResponseWriter, r *http.Request) {
  97. bucket, _ := getBucketAndObject(r)
  98. err := s3a.WithFilerClient(func(client filer_pb.SeaweedFilerClient) error {
  99. // delete collection
  100. deleteCollectionRequest := &filer_pb.DeleteCollectionRequest{
  101. Collection: bucket,
  102. }
  103. glog.V(1).Infof("delete collection: %v", deleteCollectionRequest)
  104. if _, err := client.DeleteCollection(context.Background(), deleteCollectionRequest); err != nil {
  105. return fmt.Errorf("delete collection %s: %v", bucket, err)
  106. }
  107. return nil
  108. })
  109. err = s3a.rm(s3a.option.BucketsPath, bucket, false, true)
  110. if err != nil {
  111. writeErrorResponse(w, s3err.ErrInternalError, r.URL)
  112. return
  113. }
  114. writeResponse(w, http.StatusNoContent, nil, mimeNone)
  115. }
  116. func (s3a *S3ApiServer) HeadBucketHandler(w http.ResponseWriter, r *http.Request) {
  117. bucket, _ := getBucketAndObject(r)
  118. err := s3a.WithFilerClient(func(client filer_pb.SeaweedFilerClient) error {
  119. request := &filer_pb.LookupDirectoryEntryRequest{
  120. Directory: s3a.option.BucketsPath,
  121. Name: bucket,
  122. }
  123. glog.V(1).Infof("lookup bucket: %v", request)
  124. if _, err := filer_pb.LookupEntry(client, request); err != nil {
  125. if err == filer_pb.ErrNotFound {
  126. return filer_pb.ErrNotFound
  127. }
  128. return fmt.Errorf("lookup bucket %s/%s: %v", s3a.option.BucketsPath, bucket, err)
  129. }
  130. return nil
  131. })
  132. if err != nil {
  133. writeErrorResponse(w, s3err.ErrNoSuchBucket, r.URL)
  134. return
  135. }
  136. writeSuccessResponseEmpty(w)
  137. }