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.

133 lines
3.1 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
7 years ago
7 years ago
  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/gorilla/mux"
  13. "github.com/chrislusf/seaweedfs/weed/glog"
  14. "github.com/chrislusf/seaweedfs/weed/pb/filer_pb"
  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(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(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. err := s3a.withFilerClient(func(client filer_pb.SeaweedFilerClient) error {
  64. // delete collection
  65. deleteCollectionRequest := &filer_pb.DeleteCollectionRequest{
  66. Collection: bucket,
  67. }
  68. glog.V(1).Infof("delete collection: %v", deleteCollectionRequest)
  69. if _, err := client.DeleteCollection(context.Background(), deleteCollectionRequest); err != nil {
  70. return fmt.Errorf("delete collection %s: %v", bucket, err)
  71. }
  72. return nil
  73. })
  74. err = s3a.rm(s3a.option.BucketsPath, bucket, true, false, true)
  75. if err != nil {
  76. writeErrorResponse(w, ErrInternalError, r.URL)
  77. return
  78. }
  79. writeResponse(w, http.StatusNoContent, nil, mimeNone)
  80. }
  81. func (s3a *S3ApiServer) HeadBucketHandler(w http.ResponseWriter, r *http.Request) {
  82. vars := mux.Vars(r)
  83. bucket := vars["bucket"]
  84. err := s3a.withFilerClient(func(client filer_pb.SeaweedFilerClient) error {
  85. request := &filer_pb.LookupDirectoryEntryRequest{
  86. Directory: s3a.option.BucketsPath,
  87. Name: bucket,
  88. }
  89. glog.V(1).Infof("lookup bucket: %v", request)
  90. if resp, err := client.LookupDirectoryEntry(context.Background(), request); err != nil || resp.Entry == nil {
  91. return fmt.Errorf("lookup bucket %s/%s: %v", s3a.option.BucketsPath, bucket, err)
  92. }
  93. return nil
  94. })
  95. if err != nil {
  96. writeErrorResponse(w, ErrNoSuchBucket, r.URL)
  97. return
  98. }
  99. writeSuccessResponseEmpty(w)
  100. }