s3api_bucket_handlers.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. package s3api
  2. import (
  3. "context"
  4. "encoding/xml"
  5. "fmt"
  6. "math"
  7. "net/http"
  8. "os"
  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. "github.com/gorilla/mux"
  15. )
  16. var (
  17. OS_UID = uint32(os.Getuid())
  18. OS_GID = uint32(os.Getgid())
  19. )
  20. type ListAllMyBucketsResult struct {
  21. XMLName xml.Name `xml:"http://s3.amazonaws.com/doc/2006-03-01/ ListAllMyBucketsResult"`
  22. Owner *s3.Owner
  23. Buckets []*s3.Bucket `xml:"Buckets>Bucket"`
  24. }
  25. func (s3a *S3ApiServer) ListBucketsHandler(w http.ResponseWriter, r *http.Request) {
  26. var response ListAllMyBucketsResult
  27. entries, err := s3a.list(context.Background(), s3a.option.BucketsPath, "", "", false, math.MaxInt32)
  28. if err != nil {
  29. writeErrorResponse(w, ErrInternalError, r.URL)
  30. return
  31. }
  32. var buckets []*s3.Bucket
  33. for _, entry := range entries {
  34. if entry.IsDirectory {
  35. buckets = append(buckets, &s3.Bucket{
  36. Name: aws.String(entry.Name),
  37. CreationDate: aws.Time(time.Unix(entry.Attributes.Crtime, 0)),
  38. })
  39. }
  40. }
  41. response = ListAllMyBucketsResult{
  42. Owner: &s3.Owner{
  43. ID: aws.String(""),
  44. DisplayName: aws.String(""),
  45. },
  46. Buckets: buckets,
  47. }
  48. writeSuccessResponseXML(w, encodeResponse(response))
  49. }
  50. func (s3a *S3ApiServer) PutBucketHandler(w http.ResponseWriter, r *http.Request) {
  51. vars := mux.Vars(r)
  52. bucket := vars["bucket"]
  53. // create the folder for bucket, but lazily create actual collection
  54. if err := s3a.mkdir(context.Background(), s3a.option.BucketsPath, bucket, nil); err != nil {
  55. writeErrorResponse(w, ErrInternalError, r.URL)
  56. return
  57. }
  58. writeSuccessResponseEmpty(w)
  59. }
  60. func (s3a *S3ApiServer) DeleteBucketHandler(w http.ResponseWriter, r *http.Request) {
  61. vars := mux.Vars(r)
  62. bucket := vars["bucket"]
  63. ctx := context.Background()
  64. err := s3a.withFilerClient(ctx, func(client filer_pb.SeaweedFilerClient) error {
  65. // delete collection
  66. deleteCollectionRequest := &filer_pb.DeleteCollectionRequest{
  67. Collection: bucket,
  68. }
  69. glog.V(1).Infof("delete collection: %v", deleteCollectionRequest)
  70. if _, err := client.DeleteCollection(ctx, deleteCollectionRequest); err != nil {
  71. return fmt.Errorf("delete collection %s: %v", bucket, err)
  72. }
  73. return nil
  74. })
  75. err = s3a.rm(ctx, s3a.option.BucketsPath, bucket, true, false, true)
  76. if err != nil {
  77. writeErrorResponse(w, ErrInternalError, r.URL)
  78. return
  79. }
  80. writeResponse(w, http.StatusNoContent, nil, mimeNone)
  81. }
  82. func (s3a *S3ApiServer) HeadBucketHandler(w http.ResponseWriter, r *http.Request) {
  83. vars := mux.Vars(r)
  84. bucket := vars["bucket"]
  85. ctx := context.Background()
  86. err := s3a.withFilerClient(ctx, func(client filer_pb.SeaweedFilerClient) error {
  87. request := &filer_pb.LookupDirectoryEntryRequest{
  88. Directory: s3a.option.BucketsPath,
  89. Name: bucket,
  90. }
  91. glog.V(1).Infof("lookup bucket: %v", request)
  92. if _, err := client.LookupDirectoryEntry(ctx, request); err != nil {
  93. return fmt.Errorf("lookup bucket %s/%s: %v", s3a.option.BucketsPath, bucket, err)
  94. }
  95. return nil
  96. })
  97. if err != nil {
  98. writeErrorResponse(w, ErrNoSuchBucket, r.URL)
  99. return
  100. }
  101. writeSuccessResponseEmpty(w)
  102. }