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.

130 lines
3.0 KiB

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
5 years ago
7 years ago
5 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. "time"
  9. "github.com/aws/aws-sdk-go/aws"
  10. "github.com/aws/aws-sdk-go/service/s3"
  11. "github.com/gorilla/mux"
  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, 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. vars := mux.Vars(r)
  47. bucket := vars["bucket"]
  48. // create the folder for bucket, but lazily create actual collection
  49. if err := s3a.mkdir(s3a.option.BucketsPath, bucket, nil); err != nil {
  50. writeErrorResponse(w, ErrInternalError, r.URL)
  51. return
  52. }
  53. writeSuccessResponseEmpty(w)
  54. }
  55. func (s3a *S3ApiServer) DeleteBucketHandler(w http.ResponseWriter, r *http.Request) {
  56. vars := mux.Vars(r)
  57. bucket := vars["bucket"]
  58. err := s3a.WithFilerClient(func(client filer_pb.SeaweedFilerClient) error {
  59. // delete collection
  60. deleteCollectionRequest := &filer_pb.DeleteCollectionRequest{
  61. Collection: bucket,
  62. }
  63. glog.V(1).Infof("delete collection: %v", deleteCollectionRequest)
  64. if _, err := client.DeleteCollection(context.Background(), deleteCollectionRequest); err != nil {
  65. return fmt.Errorf("delete collection %s: %v", bucket, err)
  66. }
  67. return nil
  68. })
  69. err = s3a.rm(s3a.option.BucketsPath, bucket, false, true)
  70. if err != nil {
  71. writeErrorResponse(w, ErrInternalError, r.URL)
  72. return
  73. }
  74. writeResponse(w, http.StatusNoContent, nil, mimeNone)
  75. }
  76. func (s3a *S3ApiServer) HeadBucketHandler(w http.ResponseWriter, r *http.Request) {
  77. vars := mux.Vars(r)
  78. bucket := vars["bucket"]
  79. err := s3a.WithFilerClient(func(client filer_pb.SeaweedFilerClient) error {
  80. request := &filer_pb.LookupDirectoryEntryRequest{
  81. Directory: s3a.option.BucketsPath,
  82. Name: bucket,
  83. }
  84. glog.V(1).Infof("lookup bucket: %v", request)
  85. if _, err := filer_pb.LookupEntry(client, request); err != nil {
  86. if err == filer_pb.ErrNotFound {
  87. return filer_pb.ErrNotFound
  88. }
  89. return fmt.Errorf("lookup bucket %s/%s: %v", s3a.option.BucketsPath, bucket, err)
  90. }
  91. return nil
  92. })
  93. if err != nil {
  94. writeErrorResponse(w, ErrNoSuchBucket, r.URL)
  95. return
  96. }
  97. writeSuccessResponseEmpty(w)
  98. }