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.

155 lines
3.8 KiB

4 years ago
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
6 years ago
4 years ago
4 years ago
5 years ago
4 years ago
7 years ago
4 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. "github.com/chrislusf/seaweedfs/weed/s3api/s3err"
  7. "math"
  8. "net/http"
  9. "time"
  10. "github.com/aws/aws-sdk-go/aws"
  11. "github.com/aws/aws-sdk-go/service/s3"
  12. "github.com/chrislusf/seaweedfs/weed/glog"
  13. "github.com/chrislusf/seaweedfs/weed/pb/filer_pb"
  14. )
  15. type ListAllMyBucketsResult struct {
  16. XMLName xml.Name `xml:"http://s3.amazonaws.com/doc/2006-03-01/ ListAllMyBucketsResult"`
  17. Owner *s3.Owner
  18. Buckets []*s3.Bucket `xml:"Buckets>Bucket"`
  19. }
  20. func (s3a *S3ApiServer) ListBucketsHandler(w http.ResponseWriter, r *http.Request) {
  21. var response ListAllMyBucketsResult
  22. entries, _, err := s3a.list(s3a.option.BucketsPath, "", "", false, math.MaxInt32)
  23. if err != nil {
  24. writeErrorResponse(w, s3err.ErrInternalError, r.URL)
  25. return
  26. }
  27. var buckets []*s3.Bucket
  28. for _, entry := range entries {
  29. if entry.IsDirectory {
  30. buckets = append(buckets, &s3.Bucket{
  31. Name: aws.String(entry.Name),
  32. CreationDate: aws.Time(time.Unix(entry.Attributes.Crtime, 0).UTC()),
  33. })
  34. }
  35. }
  36. response = ListAllMyBucketsResult{
  37. Owner: &s3.Owner{
  38. ID: aws.String(""),
  39. DisplayName: aws.String(""),
  40. },
  41. Buckets: buckets,
  42. }
  43. writeSuccessResponseXML(w, encodeResponse(response))
  44. }
  45. func (s3a *S3ApiServer) PutBucketHandler(w http.ResponseWriter, r *http.Request) {
  46. bucket, _ := getBucketAndObject(r)
  47. // avoid duplicated buckets
  48. errCode := s3err.ErrNone
  49. if err := s3a.WithFilerClient(func(client filer_pb.SeaweedFilerClient) error {
  50. if resp, err := client.CollectionList(context.Background(), &filer_pb.CollectionListRequest{
  51. IncludeEcVolumes: true,
  52. IncludeNormalVolumes: true,
  53. }); err != nil {
  54. glog.Errorf("list collection: %v", err)
  55. return fmt.Errorf("list collections: %v", err)
  56. }else {
  57. for _, c := range resp.Collections {
  58. if bucket == c.Name {
  59. errCode = s3err.ErrBucketAlreadyExists
  60. break
  61. }
  62. }
  63. }
  64. return nil
  65. }); err != nil {
  66. writeErrorResponse(w, s3err.ErrInternalError, r.URL)
  67. return
  68. }
  69. if errCode != s3err.ErrNone {
  70. writeErrorResponse(w, errCode, r.URL)
  71. return
  72. }
  73. // create the folder for bucket, but lazily create actual collection
  74. if err := s3a.mkdir(s3a.option.BucketsPath, bucket, nil); err != nil {
  75. glog.Errorf("PutBucketHandler mkdir: %v", err)
  76. writeErrorResponse(w, s3err.ErrInternalError, r.URL)
  77. return
  78. }
  79. writeSuccessResponseEmpty(w)
  80. }
  81. func (s3a *S3ApiServer) DeleteBucketHandler(w http.ResponseWriter, r *http.Request) {
  82. bucket, _ := getBucketAndObject(r)
  83. err := s3a.WithFilerClient(func(client filer_pb.SeaweedFilerClient) error {
  84. // delete collection
  85. deleteCollectionRequest := &filer_pb.DeleteCollectionRequest{
  86. Collection: bucket,
  87. }
  88. glog.V(1).Infof("delete collection: %v", deleteCollectionRequest)
  89. if _, err := client.DeleteCollection(context.Background(), deleteCollectionRequest); err != nil {
  90. return fmt.Errorf("delete collection %s: %v", bucket, err)
  91. }
  92. return nil
  93. })
  94. err = s3a.rm(s3a.option.BucketsPath, bucket, false, true)
  95. if err != nil {
  96. writeErrorResponse(w, s3err.ErrInternalError, r.URL)
  97. return
  98. }
  99. writeResponse(w, http.StatusNoContent, nil, mimeNone)
  100. }
  101. func (s3a *S3ApiServer) HeadBucketHandler(w http.ResponseWriter, r *http.Request) {
  102. bucket, _ := getBucketAndObject(r)
  103. err := s3a.WithFilerClient(func(client filer_pb.SeaweedFilerClient) error {
  104. request := &filer_pb.LookupDirectoryEntryRequest{
  105. Directory: s3a.option.BucketsPath,
  106. Name: bucket,
  107. }
  108. glog.V(1).Infof("lookup bucket: %v", request)
  109. if _, err := filer_pb.LookupEntry(client, request); err != nil {
  110. if err == filer_pb.ErrNotFound {
  111. return filer_pb.ErrNotFound
  112. }
  113. return fmt.Errorf("lookup bucket %s/%s: %v", s3a.option.BucketsPath, bucket, err)
  114. }
  115. return nil
  116. })
  117. if err != nil {
  118. writeErrorResponse(w, s3err.ErrNoSuchBucket, r.URL)
  119. return
  120. }
  121. writeSuccessResponseEmpty(w)
  122. }