123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319 |
- package s3api
- import (
- "crypto/md5"
- "encoding/json"
- "encoding/xml"
- "fmt"
- "io"
- "io/ioutil"
- "net/http"
- "strings"
- "github.com/gorilla/mux"
- "github.com/chrislusf/seaweedfs/weed/glog"
- "github.com/chrislusf/seaweedfs/weed/server"
- "github.com/chrislusf/seaweedfs/weed/util"
- )
- 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"]
- object := getObject(vars)
- _, err := validateContentMd5(r.Header)
- if err != nil {
- writeErrorResponse(w, ErrInvalidDigest, r.URL)
- return
- }
- rAuthType := getRequestAuthType(r)
- dataReader := r.Body
- var s3ErrCode ErrorCode
- if rAuthType == authTypeStreamingSigned {
- dataReader, s3ErrCode = s3a.iam.newSignV4ChunkedReader(r)
- }
- if s3ErrCode != ErrNone {
- writeErrorResponse(w, s3ErrCode, r.URL)
- return
- }
- defer dataReader.Close()
- uploadUrl := fmt.Sprintf("http://%s%s/%s%s", s3a.option.Filer, s3a.option.BucketsPath, bucket, object)
- etag, errCode := s3a.putToFiler(r, uploadUrl, dataReader)
- if errCode != ErrNone {
- writeErrorResponse(w, errCode, r.URL)
- return
- }
- setEtag(w, etag)
- writeSuccessResponseEmpty(w)
- }
- func (s3a *S3ApiServer) GetObjectHandler(w http.ResponseWriter, r *http.Request) {
- vars := mux.Vars(r)
- bucket := vars["bucket"]
- object := getObject(vars)
- if strings.HasSuffix(r.URL.Path, "/") {
- writeErrorResponse(w, ErrNotImplemented, r.URL)
- return
- }
- destUrl := fmt.Sprintf("http://%s%s/%s%s",
- s3a.option.Filer, s3a.option.BucketsPath, bucket, object)
- s3a.proxyToFiler(w, r, destUrl, passThroughResponse)
- }
- func (s3a *S3ApiServer) HeadObjectHandler(w http.ResponseWriter, r *http.Request) {
- vars := mux.Vars(r)
- bucket := vars["bucket"]
- object := getObject(vars)
- destUrl := fmt.Sprintf("http://%s%s/%s%s",
- s3a.option.Filer, s3a.option.BucketsPath, bucket, object)
- s3a.proxyToFiler(w, r, destUrl, passThroughResponse)
- }
- func (s3a *S3ApiServer) DeleteObjectHandler(w http.ResponseWriter, r *http.Request) {
- vars := mux.Vars(r)
- bucket := vars["bucket"]
- object := getObject(vars)
- destUrl := fmt.Sprintf("http://%s%s/%s%s",
- s3a.option.Filer, s3a.option.BucketsPath, bucket, object)
- 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)
- })
- }
- /// ObjectIdentifier carries key name for the object to delete.
- type ObjectIdentifier struct {
- ObjectName string `xml:"Key"`
- }
- // DeleteObjectsRequest - xml carrying the object key names which needs to be deleted.
- type DeleteObjectsRequest struct {
- // Element to enable quiet mode for the request
- Quiet bool
- // List of objects to be deleted
- Objects []ObjectIdentifier `xml:"Object"`
- }
- // DeleteError structure.
- type DeleteError struct {
- Code string
- Message string
- Key string
- }
- // DeleteObjectsResponse container for multiple object deletes.
- type DeleteObjectsResponse struct {
- XMLName xml.Name `xml:"http://s3.amazonaws.com/doc/2006-03-01/ DeleteResult" json:"-"`
- // Collection of all deleted objects
- DeletedObjects []ObjectIdentifier `xml:"Deleted,omitempty"`
- // Collection of errors deleting certain objects.
- Errors []DeleteError `xml:"Error,omitempty"`
- }
- // DeleteMultipleObjectsHandler - Delete multiple objects
- func (s3a *S3ApiServer) DeleteMultipleObjectsHandler(w http.ResponseWriter, r *http.Request) {
- vars := mux.Vars(r)
- bucket := vars["bucket"]
- deleteXMLBytes, err := ioutil.ReadAll(r.Body)
- if err != nil {
- writeErrorResponse(w, ErrInternalError, r.URL)
- return
- }
- deleteObjects := &DeleteObjectsRequest{}
- if err := xml.Unmarshal(deleteXMLBytes, deleteObjects); err != nil {
- writeErrorResponse(w, ErrMalformedXML, r.URL)
- return
- }
- var index int
- var deletedObjects []ObjectIdentifier
- var deleteErrors []DeleteError
- s3a.streamRemove(deleteObjects.Quiet, func() (finished bool, parentDirectoryPath string, entryName string, isDeleteData, isRecursive bool) {
- if index >= len(deleteObjects.Objects) {
- finished = true
- return
- }
- object := deleteObjects.Objects[index]
- lastSeparator := strings.LastIndex(object.ObjectName, "/")
- parentDirectoryPath, entryName, isDeleteData, isRecursive = "/", object.ObjectName, true, false
- if lastSeparator > 0 && lastSeparator+1 < len(object.ObjectName) {
- entryName = object.ObjectName[lastSeparator+1:]
- parentDirectoryPath = "/" + object.ObjectName[:lastSeparator]
- }
- parentDirectoryPath = fmt.Sprintf("%s/%s%s", s3a.option.BucketsPath, bucket, parentDirectoryPath)
- return
- }, func(err string) {
- object := deleteObjects.Objects[index]
- if err == "" {
- deletedObjects = append(deletedObjects, object)
- } else {
- deleteErrors = append(deleteErrors, DeleteError{
- Code: "",
- Message: err,
- Key: object.ObjectName,
- })
- }
- index++
- })
- deleteResp := DeleteObjectsResponse{}
- if !deleteObjects.Quiet {
- deleteResp.DeletedObjects = deletedObjects
- }
- deleteResp.Errors = deleteErrors
- writeSuccessResponseXML(w, encodeResponse(deleteResp))
- }
- func (s3a *S3ApiServer) proxyToFiler(w http.ResponseWriter, r *http.Request, destUrl string, responseFn func(proxyResonse *http.Response, w http.ResponseWriter)) {
- 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)
- 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 util.CloseResponse(resp)
- responseFn(resp, w)
- }
- func passThroughResponse(proxyResonse *http.Response, w http.ResponseWriter) {
- for k, v := range proxyResonse.Header {
- w.Header()[k] = v
- }
- w.WriteHeader(proxyResonse.StatusCode)
- io.Copy(w, proxyResonse.Body)
- }
- func (s3a *S3ApiServer) putToFiler(r *http.Request, uploadUrl string, dataReader io.Reader) (etag string, code ErrorCode) {
- hash := md5.New()
- var body = io.TeeReader(dataReader, hash)
- proxyReq, err := http.NewRequest("PUT", uploadUrl, body)
- 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)
- if postErr != nil {
- glog.Errorf("post to filer: %v", postErr)
- return "", ErrInternalError
- }
- defer resp.Body.Close()
- etag = fmt.Sprintf("%x", hash.Sum(nil))
- 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
- }
- var ret weed_server.FilerPostResult
- unmarshal_err := json.Unmarshal(resp_body, &ret)
- if unmarshal_err != nil {
- glog.Errorf("failing to read upload to %s : %v", uploadUrl, string(resp_body))
- return "", ErrInternalError
- }
- if ret.Error != "" {
- glog.Errorf("upload to filer error: %v", ret.Error)
- return "", ErrInternalError
- }
- return etag, ErrNone
- }
- func setEtag(w http.ResponseWriter, etag string) {
- if etag != "" {
- 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
- }
|