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.

169 lines
4.1 KiB

7 years ago
7 years ago
7 years ago
7 years ago
  1. package s3api
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "github.com/chrislusf/seaweedfs/weed/glog"
  6. "github.com/gorilla/mux"
  7. "io"
  8. "io/ioutil"
  9. "net/http"
  10. "strings"
  11. )
  12. var (
  13. client *http.Client
  14. )
  15. func init() {
  16. client = &http.Client{Transport: &http.Transport{
  17. MaxIdleConnsPerHost: 1024,
  18. }}
  19. }
  20. type UploadResult struct {
  21. Name string `json:"name,omitempty"`
  22. Size uint32 `json:"size,omitempty"`
  23. Error string `json:"error,omitempty"`
  24. }
  25. func (s3a *S3ApiServer) PutObjectHandler(w http.ResponseWriter, r *http.Request) {
  26. // http://docs.aws.amazon.com/AmazonS3/latest/dev/UploadingObjects.html
  27. vars := mux.Vars(r)
  28. bucket := vars["bucket"]
  29. object := vars["object"]
  30. _, err := validateContentMd5(r.Header)
  31. if err != nil {
  32. writeErrorResponse(w, ErrInvalidDigest, r.URL)
  33. return
  34. }
  35. uploadUrl := fmt.Sprintf("http://%s%s/%s/%s?collection=%s",
  36. s3a.option.Filer, s3a.option.BucketsPath, bucket, object, bucket)
  37. proxyReq, err := http.NewRequest("PUT", uploadUrl, r.Body)
  38. if err != nil {
  39. glog.Errorf("NewRequest %s: %v", uploadUrl, err)
  40. writeErrorResponse(w, ErrInternalError, r.URL)
  41. return
  42. }
  43. proxyReq.Header.Set("Host", s3a.option.Filer)
  44. proxyReq.Header.Set("X-Forwarded-For", r.RemoteAddr)
  45. for header, values := range r.Header {
  46. for _, value := range values {
  47. proxyReq.Header.Add(header, value)
  48. }
  49. }
  50. resp, postErr := client.Do(proxyReq)
  51. if postErr != nil {
  52. glog.Errorf("post to filer: %v", postErr)
  53. writeErrorResponse(w, ErrInternalError, r.URL)
  54. return
  55. }
  56. defer resp.Body.Close()
  57. resp_body, ra_err := ioutil.ReadAll(resp.Body)
  58. if ra_err != nil {
  59. glog.Errorf("upload to filer response read: %v", ra_err)
  60. writeErrorResponse(w, ErrInternalError, r.URL)
  61. return
  62. }
  63. var ret UploadResult
  64. unmarshal_err := json.Unmarshal(resp_body, &ret)
  65. if unmarshal_err != nil {
  66. glog.Errorf("failing to read upload to %s : %v", uploadUrl, string(resp_body))
  67. writeErrorResponse(w, ErrInternalError, r.URL)
  68. return
  69. }
  70. if ret.Error != "" {
  71. glog.Errorf("upload to filer error: %v", ret.Error)
  72. writeErrorResponse(w, ErrInternalError, r.URL)
  73. return
  74. }
  75. writeSuccessResponseEmpty(w)
  76. }
  77. func (s3a *S3ApiServer) GetObjectHandler(w http.ResponseWriter, r *http.Request) {
  78. if strings.HasSuffix(r.URL.Path, "/") {
  79. writeErrorResponse(w, ErrNotImplemented, r.URL)
  80. return
  81. }
  82. destUrl := fmt.Sprintf("http://%s%s%s",
  83. s3a.option.Filer, s3a.option.BucketsPath, r.RequestURI)
  84. s3a.proxyToFiler(w, r, destUrl, passThroghResponse)
  85. }
  86. func (s3a *S3ApiServer) HeadObjectHandler(w http.ResponseWriter, r *http.Request) {
  87. destUrl := fmt.Sprintf("http://%s%s%s",
  88. s3a.option.Filer, s3a.option.BucketsPath, r.RequestURI)
  89. s3a.proxyToFiler(w, r, destUrl, passThroghResponse)
  90. }
  91. func (s3a *S3ApiServer) DeleteObjectHandler(w http.ResponseWriter, r *http.Request) {
  92. destUrl := fmt.Sprintf("http://%s%s%s",
  93. s3a.option.Filer, s3a.option.BucketsPath, r.RequestURI)
  94. s3a.proxyToFiler(w, r, destUrl, func(proxyResonse *http.Response, w http.ResponseWriter) {
  95. for k, v := range proxyResonse.Header {
  96. w.Header()[k] = v
  97. }
  98. w.WriteHeader(http.StatusNoContent)
  99. })
  100. }
  101. func (s3a *S3ApiServer) proxyToFiler(w http.ResponseWriter, r *http.Request, destUrl string, responseFn func(proxyResonse *http.Response, w http.ResponseWriter)) {
  102. glog.V(2).Infof("s3 proxying %s to %s", r.Method, destUrl)
  103. proxyReq, err := http.NewRequest(r.Method, destUrl, r.Body)
  104. if err != nil {
  105. glog.Errorf("NewRequest %s: %v", destUrl, err)
  106. writeErrorResponse(w, ErrInternalError, r.URL)
  107. return
  108. }
  109. proxyReq.Header.Set("Host", s3a.option.Filer)
  110. proxyReq.Header.Set("X-Forwarded-For", r.RemoteAddr)
  111. for header, values := range r.Header {
  112. for _, value := range values {
  113. proxyReq.Header.Add(header, value)
  114. }
  115. }
  116. resp, postErr := client.Do(proxyReq)
  117. if postErr != nil {
  118. glog.Errorf("post to filer: %v", postErr)
  119. writeErrorResponse(w, ErrInternalError, r.URL)
  120. return
  121. }
  122. defer resp.Body.Close()
  123. responseFn(resp, w)
  124. }
  125. func passThroghResponse(proxyResonse *http.Response, w http.ResponseWriter) {
  126. for k, v := range proxyResonse.Header {
  127. w.Header()[k] = v
  128. }
  129. w.WriteHeader(proxyResonse.StatusCode)
  130. io.Copy(w, proxyResonse.Body)
  131. }