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.

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