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.

132 lines
3.0 KiB

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