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.

128 lines
2.8 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
6 years ago
7 years ago
  1. package s3api
  2. import (
  3. "context"
  4. "fmt"
  5. "net/http"
  6. "os"
  7. "time"
  8. "github.com/chrislusf/seaweedfs/weed/glog"
  9. "github.com/chrislusf/seaweedfs/weed/pb/filer_pb"
  10. "github.com/gorilla/mux"
  11. )
  12. var (
  13. OS_UID = uint32(os.Getuid())
  14. OS_GID = uint32(os.Getgid())
  15. )
  16. func (s3a *S3ApiServer) ListBucketsHandler(w http.ResponseWriter, r *http.Request) {
  17. var response ListAllMyBucketsResponse
  18. entries, err := s3a.list(s3a.option.BucketsPath)
  19. if err != nil {
  20. writeErrorResponse(w, ErrInternalError, r.URL)
  21. return
  22. }
  23. var buckets []ListAllMyBucketsEntry
  24. for _, entry := range entries {
  25. if entry.IsDirectory {
  26. buckets = append(buckets, ListAllMyBucketsEntry{
  27. Name: entry.Name,
  28. CreationDate: time.Unix(entry.Attributes.Crtime, 0),
  29. })
  30. }
  31. }
  32. response = ListAllMyBucketsResponse{
  33. ListAllMyBucketsResponse: ListAllMyBucketsResult{
  34. Owner: CanonicalUser{
  35. ID: "",
  36. DisplayName: "",
  37. },
  38. Buckets: ListAllMyBucketsList{
  39. Bucket: buckets,
  40. },
  41. },
  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. ctx := context.Background()
  60. // delete collection
  61. deleteCollectionRequest := &filer_pb.DeleteCollectionRequest{
  62. Collection: bucket,
  63. }
  64. glog.V(1).Infof("delete collection: %v", deleteCollectionRequest)
  65. if _, err := client.DeleteCollection(ctx, deleteCollectionRequest); err != nil {
  66. return fmt.Errorf("delete collection %s: %v", bucket, err)
  67. }
  68. return nil
  69. })
  70. err = s3a.rm(s3a.option.BucketsPath, bucket, true, false, true)
  71. if err != nil {
  72. writeErrorResponse(w, ErrInternalError, r.URL)
  73. return
  74. }
  75. writeResponse(w, http.StatusNoContent, nil, mimeNone)
  76. }
  77. func (s3a *S3ApiServer) HeadBucketHandler(w http.ResponseWriter, r *http.Request) {
  78. vars := mux.Vars(r)
  79. bucket := vars["bucket"]
  80. err := s3a.withFilerClient(func(client filer_pb.SeaweedFilerClient) error {
  81. request := &filer_pb.LookupDirectoryEntryRequest{
  82. Directory: s3a.option.BucketsPath,
  83. Name: bucket,
  84. }
  85. glog.V(1).Infof("lookup bucket: %v", request)
  86. if _, err := client.LookupDirectoryEntry(context.Background(), request); err != nil {
  87. return fmt.Errorf("lookup bucket %s/%s: %v", s3a.option.BucketsPath, bucket, err)
  88. }
  89. return nil
  90. })
  91. if err != nil {
  92. writeErrorResponse(w, ErrNoSuchBucket, r.URL)
  93. return
  94. }
  95. writeSuccessResponseEmpty(w)
  96. }