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.

222 lines
5.2 KiB

6 years ago
7 years ago
7 years ago
7 years ago
6 years ago
6 years ago
  1. package s3api
  2. import (
  3. "crypto/md5"
  4. "encoding/json"
  5. "fmt"
  6. "io"
  7. "io/ioutil"
  8. "net/http"
  9. "strings"
  10. "github.com/chrislusf/seaweedfs/weed/glog"
  11. "github.com/chrislusf/seaweedfs/weed/server"
  12. "github.com/gorilla/mux"
  13. )
  14. var (
  15. client *http.Client
  16. )
  17. func init() {
  18. client = &http.Client{Transport: &http.Transport{
  19. MaxIdleConnsPerHost: 1024,
  20. }}
  21. }
  22. func (s3a *S3ApiServer) PutObjectHandler(w http.ResponseWriter, r *http.Request) {
  23. // http://docs.aws.amazon.com/AmazonS3/latest/dev/UploadingObjects.html
  24. vars := mux.Vars(r)
  25. bucket := vars["bucket"]
  26. object := getObject(vars)
  27. _, err := validateContentMd5(r.Header)
  28. if err != nil {
  29. writeErrorResponse(w, ErrInvalidDigest, r.URL)
  30. return
  31. }
  32. rAuthType := getRequestAuthType(r)
  33. dataReader := r.Body
  34. if rAuthType == authTypeStreamingSigned {
  35. dataReader = newSignV4ChunkedReader(r)
  36. }
  37. uploadUrl := fmt.Sprintf("http://%s%s/%s%s?collection=%s",
  38. s3a.option.Filer, s3a.option.BucketsPath, bucket, object, bucket)
  39. etag, errCode := s3a.putToFiler(r, uploadUrl, dataReader)
  40. if errCode != ErrNone {
  41. writeErrorResponse(w, errCode, r.URL)
  42. return
  43. }
  44. setEtag(w, etag)
  45. writeSuccessResponseEmpty(w)
  46. }
  47. func (s3a *S3ApiServer) GetObjectHandler(w http.ResponseWriter, r *http.Request) {
  48. vars := mux.Vars(r)
  49. bucket := vars["bucket"]
  50. if strings.HasSuffix(r.URL.Path, "/") {
  51. writeErrorResponse(w, ErrNotImplemented, r.URL)
  52. return
  53. }
  54. destUrl := fmt.Sprintf("http://%s%s/%s%s",
  55. s3a.option.Filer, s3a.option.BucketsPath, bucket, r.RequestURI)
  56. s3a.proxyToFiler(w, r, destUrl, passThroghResponse)
  57. }
  58. func (s3a *S3ApiServer) HeadObjectHandler(w http.ResponseWriter, r *http.Request) {
  59. vars := mux.Vars(r)
  60. bucket := vars["bucket"]
  61. destUrl := fmt.Sprintf("http://%s%s/%s%s",
  62. s3a.option.Filer, s3a.option.BucketsPath, bucket, r.RequestURI)
  63. s3a.proxyToFiler(w, r, destUrl, passThroghResponse)
  64. }
  65. func (s3a *S3ApiServer) DeleteObjectHandler(w http.ResponseWriter, r *http.Request) {
  66. vars := mux.Vars(r)
  67. bucket := vars["bucket"]
  68. destUrl := fmt.Sprintf("http://%s%s/%s%s",
  69. s3a.option.Filer, s3a.option.BucketsPath, bucket, r.RequestURI)
  70. s3a.proxyToFiler(w, r, destUrl, func(proxyResonse *http.Response, w http.ResponseWriter) {
  71. for k, v := range proxyResonse.Header {
  72. w.Header()[k] = v
  73. }
  74. w.WriteHeader(http.StatusNoContent)
  75. })
  76. }
  77. // DeleteMultipleObjectsHandler - Delete multiple objects
  78. func (s3a *S3ApiServer) DeleteMultipleObjectsHandler(w http.ResponseWriter, r *http.Request) {
  79. // TODO
  80. writeErrorResponse(w, ErrNotImplemented, r.URL)
  81. }
  82. func (s3a *S3ApiServer) proxyToFiler(w http.ResponseWriter, r *http.Request, destUrl string, responseFn func(proxyResonse *http.Response, w http.ResponseWriter)) {
  83. glog.V(2).Infof("s3 proxying %s to %s", r.Method, destUrl)
  84. proxyReq, err := http.NewRequest(r.Method, destUrl, r.Body)
  85. if err != nil {
  86. glog.Errorf("NewRequest %s: %v", destUrl, err)
  87. writeErrorResponse(w, ErrInternalError, r.URL)
  88. return
  89. }
  90. proxyReq.Header.Set("Host", s3a.option.Filer)
  91. proxyReq.Header.Set("X-Forwarded-For", r.RemoteAddr)
  92. for header, values := range r.Header {
  93. for _, value := range values {
  94. proxyReq.Header.Add(header, value)
  95. }
  96. }
  97. resp, postErr := client.Do(proxyReq)
  98. if postErr != nil {
  99. glog.Errorf("post to filer: %v", postErr)
  100. writeErrorResponse(w, ErrInternalError, r.URL)
  101. return
  102. }
  103. defer resp.Body.Close()
  104. responseFn(resp, w)
  105. }
  106. func passThroghResponse(proxyResonse *http.Response, w http.ResponseWriter) {
  107. for k, v := range proxyResonse.Header {
  108. w.Header()[k] = v
  109. }
  110. w.WriteHeader(proxyResonse.StatusCode)
  111. io.Copy(w, proxyResonse.Body)
  112. }
  113. func (s3a *S3ApiServer) putToFiler(r *http.Request, uploadUrl string, dataReader io.ReadCloser) (etag string, code ErrorCode) {
  114. hash := md5.New()
  115. var body io.Reader = io.TeeReader(dataReader, hash)
  116. proxyReq, err := http.NewRequest("PUT", uploadUrl, body)
  117. if err != nil {
  118. glog.Errorf("NewRequest %s: %v", uploadUrl, err)
  119. return "", ErrInternalError
  120. }
  121. proxyReq.Header.Set("Host", s3a.option.Filer)
  122. proxyReq.Header.Set("X-Forwarded-For", r.RemoteAddr)
  123. for header, values := range r.Header {
  124. for _, value := range values {
  125. proxyReq.Header.Add(header, value)
  126. }
  127. }
  128. resp, postErr := client.Do(proxyReq)
  129. dataReader.Close()
  130. if postErr != nil {
  131. glog.Errorf("post to filer: %v", postErr)
  132. return "", ErrInternalError
  133. }
  134. defer resp.Body.Close()
  135. etag = fmt.Sprintf("%x", hash.Sum(nil))
  136. resp_body, ra_err := ioutil.ReadAll(resp.Body)
  137. if ra_err != nil {
  138. glog.Errorf("upload to filer response read: %v", ra_err)
  139. return etag, ErrInternalError
  140. }
  141. var ret weed_server.FilerPostResult
  142. unmarshal_err := json.Unmarshal(resp_body, &ret)
  143. if unmarshal_err != nil {
  144. glog.Errorf("failing to read upload to %s : %v", uploadUrl, string(resp_body))
  145. return "", ErrInternalError
  146. }
  147. if ret.Error != "" {
  148. glog.Errorf("upload to filer error: %v", ret.Error)
  149. return "", ErrInternalError
  150. }
  151. return etag, ErrNone
  152. }
  153. func setEtag(w http.ResponseWriter, etag string) {
  154. if etag != "" {
  155. if strings.HasPrefix(etag, "\"") {
  156. w.Header().Set("ETag", etag)
  157. } else {
  158. w.Header().Set("ETag", "\""+etag+"\"")
  159. }
  160. }
  161. }
  162. func getObject(vars map[string]string) string {
  163. object := vars["object"]
  164. if !strings.HasPrefix(object, "/") {
  165. object = "/" + object
  166. }
  167. return object
  168. }