2018-07-22 01:39:02 +08:00
|
|
|
package s3api
|
|
|
|
|
|
|
|
import (
|
2018-09-12 16:00:57 +08:00
|
|
|
"crypto/md5"
|
2018-07-22 08:39:10 +08:00
|
|
|
"encoding/json"
|
2018-07-22 01:39:02 +08:00
|
|
|
"fmt"
|
2018-07-22 16:15:11 +08:00
|
|
|
"io"
|
2018-07-22 01:39:02 +08:00
|
|
|
"io/ioutil"
|
2018-07-22 08:39:10 +08:00
|
|
|
"net/http"
|
2018-07-23 16:15:59 +08:00
|
|
|
"strings"
|
2018-09-12 15:46:12 +08:00
|
|
|
|
|
|
|
"github.com/chrislusf/seaweedfs/weed/glog"
|
|
|
|
"github.com/chrislusf/seaweedfs/weed/server"
|
2018-09-12 16:00:57 +08:00
|
|
|
"github.com/gorilla/mux"
|
2018-07-22 01:39:02 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
client *http.Client
|
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
client = &http.Client{Transport: &http.Transport{
|
|
|
|
MaxIdleConnsPerHost: 1024,
|
|
|
|
}}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s3a *S3ApiServer) PutObjectHandler(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
|
|
|
// http://docs.aws.amazon.com/AmazonS3/latest/dev/UploadingObjects.html
|
|
|
|
|
|
|
|
vars := mux.Vars(r)
|
|
|
|
bucket := vars["bucket"]
|
2018-09-12 15:46:12 +08:00
|
|
|
object := getObject(vars)
|
2018-07-22 01:39:02 +08:00
|
|
|
|
|
|
|
_, err := validateContentMd5(r.Header)
|
|
|
|
if err != nil {
|
|
|
|
writeErrorResponse(w, ErrInvalidDigest, r.URL)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-09-03 05:20:47 +08:00
|
|
|
rAuthType := getRequestAuthType(r)
|
|
|
|
dataReader := r.Body
|
2018-09-04 07:47:00 +08:00
|
|
|
if rAuthType == authTypeStreamingSigned {
|
2018-09-03 05:20:47 +08:00
|
|
|
dataReader = newSignV4ChunkedReader(r)
|
|
|
|
}
|
|
|
|
|
2018-09-12 15:46:12 +08:00
|
|
|
uploadUrl := fmt.Sprintf("http://%s%s/%s%s?collection=%s",
|
2018-07-22 01:39:02 +08:00
|
|
|
s3a.option.Filer, s3a.option.BucketsPath, bucket, object, bucket)
|
|
|
|
|
2018-09-04 15:42:44 +08:00
|
|
|
etag, errCode := s3a.putToFiler(r, uploadUrl, dataReader)
|
2018-07-22 01:39:02 +08:00
|
|
|
|
2018-09-04 15:42:44 +08:00
|
|
|
if errCode != ErrNone {
|
|
|
|
writeErrorResponse(w, errCode, r.URL)
|
2018-07-22 01:39:02 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-09-04 15:42:44 +08:00
|
|
|
setEtag(w, etag)
|
2018-07-22 01:39:02 +08:00
|
|
|
|
|
|
|
writeSuccessResponseEmpty(w)
|
|
|
|
}
|
2018-07-22 09:49:47 +08:00
|
|
|
|
|
|
|
func (s3a *S3ApiServer) GetObjectHandler(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
2018-09-20 13:01:41 +08:00
|
|
|
vars := mux.Vars(r)
|
|
|
|
bucket := vars["bucket"]
|
2018-11-09 13:41:02 +08:00
|
|
|
object := getObject(vars)
|
2018-09-20 13:01:41 +08:00
|
|
|
|
2018-07-23 16:15:59 +08:00
|
|
|
if strings.HasSuffix(r.URL.Path, "/") {
|
|
|
|
writeErrorResponse(w, ErrNotImplemented, r.URL)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-09-20 13:01:41 +08:00
|
|
|
destUrl := fmt.Sprintf("http://%s%s/%s%s",
|
2018-11-09 13:41:02 +08:00
|
|
|
s3a.option.Filer, s3a.option.BucketsPath, bucket, object)
|
2018-07-22 09:49:47 +08:00
|
|
|
|
2019-01-02 18:38:32 +08:00
|
|
|
s3a.proxyToFiler(w, r, destUrl, passThroughResponse)
|
2018-07-22 09:49:47 +08:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s3a *S3ApiServer) HeadObjectHandler(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
2018-09-20 13:01:41 +08:00
|
|
|
vars := mux.Vars(r)
|
|
|
|
bucket := vars["bucket"]
|
2018-11-09 13:41:02 +08:00
|
|
|
object := getObject(vars)
|
2018-09-20 13:01:41 +08:00
|
|
|
|
|
|
|
destUrl := fmt.Sprintf("http://%s%s/%s%s",
|
2018-11-09 13:41:02 +08:00
|
|
|
s3a.option.Filer, s3a.option.BucketsPath, bucket, object)
|
2018-07-22 09:49:47 +08:00
|
|
|
|
2019-01-02 18:38:32 +08:00
|
|
|
s3a.proxyToFiler(w, r, destUrl, passThroughResponse)
|
2018-07-22 09:49:47 +08:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s3a *S3ApiServer) DeleteObjectHandler(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
2018-09-20 13:01:41 +08:00
|
|
|
vars := mux.Vars(r)
|
|
|
|
bucket := vars["bucket"]
|
2018-11-09 13:41:02 +08:00
|
|
|
object := getObject(vars)
|
2018-09-20 13:01:41 +08:00
|
|
|
|
|
|
|
destUrl := fmt.Sprintf("http://%s%s/%s%s",
|
2018-11-09 13:41:02 +08:00
|
|
|
s3a.option.Filer, s3a.option.BucketsPath, bucket, object)
|
2018-07-22 09:49:47 +08:00
|
|
|
|
2018-07-22 10:12:44 +08:00
|
|
|
s3a.proxyToFiler(w, r, destUrl, func(proxyResonse *http.Response, w http.ResponseWriter) {
|
|
|
|
for k, v := range proxyResonse.Header {
|
|
|
|
w.Header()[k] = v
|
|
|
|
}
|
|
|
|
w.WriteHeader(http.StatusNoContent)
|
|
|
|
})
|
2018-07-22 09:49:47 +08:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2018-09-04 15:42:44 +08:00
|
|
|
// DeleteMultipleObjectsHandler - Delete multiple objects
|
|
|
|
func (s3a *S3ApiServer) DeleteMultipleObjectsHandler(w http.ResponseWriter, r *http.Request) {
|
|
|
|
// TODO
|
|
|
|
writeErrorResponse(w, ErrNotImplemented, r.URL)
|
|
|
|
}
|
|
|
|
|
2018-07-22 10:12:44 +08:00
|
|
|
func (s3a *S3ApiServer) proxyToFiler(w http.ResponseWriter, r *http.Request, destUrl string, responseFn func(proxyResonse *http.Response, w http.ResponseWriter)) {
|
2018-07-22 09:49:47 +08:00
|
|
|
|
|
|
|
glog.V(2).Infof("s3 proxying %s to %s", r.Method, destUrl)
|
|
|
|
|
|
|
|
proxyReq, err := http.NewRequest(r.Method, destUrl, r.Body)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
glog.Errorf("NewRequest %s: %v", destUrl, err)
|
|
|
|
writeErrorResponse(w, ErrInternalError, r.URL)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
proxyReq.Header.Set("Host", s3a.option.Filer)
|
|
|
|
proxyReq.Header.Set("X-Forwarded-For", r.RemoteAddr)
|
2018-11-09 13:41:02 +08:00
|
|
|
proxyReq.Header.Set("Etag-MD5", "True")
|
2018-07-22 09:49:47 +08:00
|
|
|
|
|
|
|
for header, values := range r.Header {
|
|
|
|
for _, value := range values {
|
|
|
|
proxyReq.Header.Add(header, value)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
resp, postErr := client.Do(proxyReq)
|
|
|
|
|
|
|
|
if postErr != nil {
|
|
|
|
glog.Errorf("post to filer: %v", postErr)
|
|
|
|
writeErrorResponse(w, ErrInternalError, r.URL)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
2018-07-22 10:12:44 +08:00
|
|
|
responseFn(resp, w)
|
|
|
|
}
|
2019-01-02 18:38:32 +08:00
|
|
|
func passThroughResponse(proxyResonse *http.Response, w http.ResponseWriter) {
|
2018-07-22 10:12:44 +08:00
|
|
|
for k, v := range proxyResonse.Header {
|
2018-07-22 09:49:47 +08:00
|
|
|
w.Header()[k] = v
|
|
|
|
}
|
2018-07-22 10:12:44 +08:00
|
|
|
w.WriteHeader(proxyResonse.StatusCode)
|
|
|
|
io.Copy(w, proxyResonse.Body)
|
2018-07-22 09:49:47 +08:00
|
|
|
}
|
2018-09-04 15:42:44 +08:00
|
|
|
|
|
|
|
func (s3a *S3ApiServer) putToFiler(r *http.Request, uploadUrl string, dataReader io.ReadCloser) (etag string, code ErrorCode) {
|
|
|
|
|
2018-09-12 15:46:12 +08:00
|
|
|
hash := md5.New()
|
|
|
|
var body io.Reader = io.TeeReader(dataReader, hash)
|
|
|
|
|
|
|
|
proxyReq, err := http.NewRequest("PUT", uploadUrl, body)
|
2018-09-04 15:42:44 +08:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
glog.Errorf("NewRequest %s: %v", uploadUrl, err)
|
|
|
|
return "", ErrInternalError
|
|
|
|
}
|
|
|
|
|
|
|
|
proxyReq.Header.Set("Host", s3a.option.Filer)
|
|
|
|
proxyReq.Header.Set("X-Forwarded-For", r.RemoteAddr)
|
|
|
|
|
|
|
|
for header, values := range r.Header {
|
|
|
|
for _, value := range values {
|
|
|
|
proxyReq.Header.Add(header, value)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
resp, postErr := client.Do(proxyReq)
|
|
|
|
|
2018-09-12 15:46:12 +08:00
|
|
|
dataReader.Close()
|
|
|
|
|
2018-09-04 15:42:44 +08:00
|
|
|
if postErr != nil {
|
|
|
|
glog.Errorf("post to filer: %v", postErr)
|
|
|
|
return "", ErrInternalError
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
2018-09-12 15:46:12 +08:00
|
|
|
etag = fmt.Sprintf("%x", hash.Sum(nil))
|
2018-09-04 15:42:44 +08:00
|
|
|
|
|
|
|
resp_body, ra_err := ioutil.ReadAll(resp.Body)
|
|
|
|
if ra_err != nil {
|
|
|
|
glog.Errorf("upload to filer response read: %v", ra_err)
|
|
|
|
return etag, ErrInternalError
|
|
|
|
}
|
2018-09-12 15:46:12 +08:00
|
|
|
var ret weed_server.FilerPostResult
|
2018-09-04 15:42:44 +08:00
|
|
|
unmarshal_err := json.Unmarshal(resp_body, &ret)
|
|
|
|
if unmarshal_err != nil {
|
|
|
|
glog.Errorf("failing to read upload to %s : %v", uploadUrl, string(resp_body))
|
2018-09-12 15:46:12 +08:00
|
|
|
return "", ErrInternalError
|
2018-09-04 15:42:44 +08:00
|
|
|
}
|
|
|
|
if ret.Error != "" {
|
|
|
|
glog.Errorf("upload to filer error: %v", ret.Error)
|
2018-09-12 15:46:12 +08:00
|
|
|
return "", ErrInternalError
|
2018-09-04 15:42:44 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return etag, ErrNone
|
|
|
|
}
|
|
|
|
|
|
|
|
func setEtag(w http.ResponseWriter, etag string) {
|
|
|
|
if etag != "" {
|
2018-09-12 15:46:12 +08:00
|
|
|
if strings.HasPrefix(etag, "\"") {
|
|
|
|
w.Header().Set("ETag", etag)
|
|
|
|
} else {
|
|
|
|
w.Header().Set("ETag", "\""+etag+"\"")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func getObject(vars map[string]string) string {
|
|
|
|
object := vars["object"]
|
|
|
|
if !strings.HasPrefix(object, "/") {
|
|
|
|
object = "/" + object
|
|
|
|
}
|
|
|
|
return object
|
|
|
|
}
|