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.

176 lines
4.4 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 exist, err := s3a.exists(s3a.option.BucketsPath, bucket, true); err == nil && exist {
  77. errCode = s3err.ErrBucketAlreadyExists
  78. }
  79. if errCode != s3err.ErrNone {
  80. writeErrorResponse(w, errCode, r.URL)
  81. return
  82. }
  83. fn := func(entry *filer_pb.Entry) {
  84. if identityId := r.Header.Get(xhttp.AmzIdentityId); identityId != "" {
  85. if entry.Extended == nil {
  86. entry.Extended = make(map[string][]byte)
  87. }
  88. entry.Extended[xhttp.AmzIdentityId] = []byte(identityId)
  89. }
  90. }
  91. // create the folder for bucket, but lazily create actual collection
  92. if err := s3a.mkdir(s3a.option.BucketsPath, bucket, fn); err != nil {
  93. glog.Errorf("PutBucketHandler mkdir: %v", err)
  94. writeErrorResponse(w, s3err.ErrInternalError, r.URL)
  95. return
  96. }
  97. writeSuccessResponseEmpty(w)
  98. }
  99. func (s3a *S3ApiServer) DeleteBucketHandler(w http.ResponseWriter, r *http.Request) {
  100. bucket, _ := getBucketAndObject(r)
  101. err := s3a.WithFilerClient(func(client filer_pb.SeaweedFilerClient) error {
  102. // delete collection
  103. deleteCollectionRequest := &filer_pb.DeleteCollectionRequest{
  104. Collection: bucket,
  105. }
  106. glog.V(1).Infof("delete collection: %v", deleteCollectionRequest)
  107. if _, err := client.DeleteCollection(context.Background(), deleteCollectionRequest); err != nil {
  108. return fmt.Errorf("delete collection %s: %v", bucket, err)
  109. }
  110. return nil
  111. })
  112. err = s3a.rm(s3a.option.BucketsPath, bucket, false, true)
  113. if err != nil {
  114. writeErrorResponse(w, s3err.ErrInternalError, r.URL)
  115. return
  116. }
  117. writeResponse(w, http.StatusNoContent, nil, mimeNone)
  118. }
  119. func (s3a *S3ApiServer) HeadBucketHandler(w http.ResponseWriter, r *http.Request) {
  120. bucket, _ := getBucketAndObject(r)
  121. err := s3a.WithFilerClient(func(client filer_pb.SeaweedFilerClient) error {
  122. request := &filer_pb.LookupDirectoryEntryRequest{
  123. Directory: s3a.option.BucketsPath,
  124. Name: bucket,
  125. }
  126. glog.V(1).Infof("lookup bucket: %v", request)
  127. if _, err := filer_pb.LookupEntry(client, request); err != nil {
  128. if err == filer_pb.ErrNotFound {
  129. return filer_pb.ErrNotFound
  130. }
  131. return fmt.Errorf("lookup bucket %s/%s: %v", s3a.option.BucketsPath, bucket, err)
  132. }
  133. return nil
  134. })
  135. if err != nil {
  136. writeErrorResponse(w, s3err.ErrNoSuchBucket, r.URL)
  137. return
  138. }
  139. writeSuccessResponseEmpty(w)
  140. }