seaweedfs/weed/s3api/s3api_object_handlers.go

321 lines
8.1 KiB
Go
Raw Normal View History

package s3api
import (
2018-09-12 08:00:57 +00:00
"crypto/md5"
2018-07-22 00:39:10 +00:00
"encoding/json"
2020-02-25 22:38:36 +00:00
"encoding/xml"
"fmt"
2018-07-22 08:15:11 +00:00
"io"
"io/ioutil"
2018-07-22 00:39:10 +00:00
"net/http"
2018-07-23 08:15:59 +00:00
"strings"
2018-09-12 07:46:12 +00:00
"github.com/gorilla/mux"
2018-09-12 07:46:12 +00:00
"github.com/chrislusf/seaweedfs/weed/glog"
"github.com/chrislusf/seaweedfs/weed/pb/filer_pb"
weed_server "github.com/chrislusf/seaweedfs/weed/server"
2020-02-14 17:46:36 +00:00
"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
2020-07-26 19:58:58 +00:00
bucket, object := getBucketAndObject(r)
_, err := validateContentMd5(r.Header)
if err != nil {
writeErrorResponse(w, ErrInvalidDigest, r.URL)
return
}
rAuthType := getRequestAuthType(r)
dataReader := r.Body
2020-02-10 01:42:17 +00:00
var s3ErrCode ErrorCode
2020-07-26 19:06:45 +00:00
switch rAuthType {
case authTypeStreamingSigned:
2020-02-10 01:42:17 +00:00
dataReader, s3ErrCode = s3a.iam.newSignV4ChunkedReader(r)
2020-07-26 19:06:45 +00:00
case authTypeSignedV2, authTypePresignedV2:
_, s3ErrCode = s3a.iam.isReqAuthenticatedV2(r)
case authTypePresigned, authTypeSigned:
_, s3ErrCode = s3a.iam.reqSignatureV4Verify(r)
2020-02-10 01:42:17 +00:00
}
if s3ErrCode != ErrNone {
writeErrorResponse(w, s3ErrCode, r.URL)
return
}
2020-02-14 17:09:15 +00:00
defer dataReader.Close()
2020-07-27 16:58:42 +00:00
if strings.HasSuffix(object, "/") {
if err := s3a.mkdir(s3a.option.BucketsPath, bucket+object, nil); err != nil {
writeErrorResponse(w, ErrInternalError, r.URL)
return
}
} else {
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)
}
2018-07-22 01:49:47 +00:00
func (s3a *S3ApiServer) GetObjectHandler(w http.ResponseWriter, r *http.Request) {
2020-07-26 19:58:58 +00:00
bucket, object := getBucketAndObject(r)
2018-09-20 05:01:41 +00:00
2018-07-23 08:15:59 +00:00
if strings.HasSuffix(r.URL.Path, "/") {
writeErrorResponse(w, ErrNotImplemented, r.URL)
return
}
2018-09-20 05:01:41 +00:00
destUrl := fmt.Sprintf("http://%s%s/%s%s",
s3a.option.Filer, s3a.option.BucketsPath, bucket, object)
2018-07-22 01:49:47 +00:00
2019-01-02 10:38:32 +00:00
s3a.proxyToFiler(w, r, destUrl, passThroughResponse)
2018-07-22 01:49:47 +00:00
}
func (s3a *S3ApiServer) HeadObjectHandler(w http.ResponseWriter, r *http.Request) {
2020-07-26 19:58:58 +00:00
bucket, object := getBucketAndObject(r)
2018-09-20 05:01:41 +00:00
destUrl := fmt.Sprintf("http://%s%s/%s%s",
s3a.option.Filer, s3a.option.BucketsPath, bucket, object)
2018-07-22 01:49:47 +00:00
2019-01-02 10:38:32 +00:00
s3a.proxyToFiler(w, r, destUrl, passThroughResponse)
2018-07-22 01:49:47 +00:00
}
func (s3a *S3ApiServer) DeleteObjectHandler(w http.ResponseWriter, r *http.Request) {
2020-07-26 19:58:58 +00:00
bucket, object := getBucketAndObject(r)
2018-09-20 05:01:41 +00:00
destUrl := fmt.Sprintf("http://%s%s/%s%s?recursive=true",
s3a.option.Filer, s3a.option.BucketsPath, bucket, object)
2018-07-22 01:49:47 +00:00
s3a.proxyToFiler(w, r, destUrl, func(proxyResponse *http.Response, w http.ResponseWriter) {
for k, v := range proxyResponse.Header {
2018-07-22 02:12:44 +00:00
w.Header()[k] = v
}
2020-06-11 16:00:34 +00:00
w.WriteHeader(http.StatusNoContent)
2018-07-22 02:12:44 +00:00
})
2018-07-22 01:49:47 +00:00
}
2020-07-26 19:58:58 +00:00
// / ObjectIdentifier carries key name for the object to delete.
2020-02-25 22:38:36 +00:00
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) {
2020-02-25 22:38:36 +00:00
2020-07-26 19:58:58 +00:00
bucket, _ := getBucketAndObject(r)
2020-02-25 22:38:36 +00:00
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 deletedObjects []ObjectIdentifier
var deleteErrors []DeleteError
2020-03-23 06:52:55 +00:00
s3a.WithFilerClient(func(client filer_pb.SeaweedFilerClient) error {
for _, object := range deleteObjects.Objects {
lastSeparator := strings.LastIndex(object.ObjectName, "/")
parentDirectoryPath, entryName, isDeleteData, isRecursive := "/", object.ObjectName, true, true
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)
err := doDeleteEntry(client, parentDirectoryPath, entryName, isDeleteData, isRecursive)
if err == nil {
deletedObjects = append(deletedObjects, object)
} else {
deleteErrors = append(deleteErrors, DeleteError{
Code: "",
Message: err.Error(),
Key: object.ObjectName,
})
}
2020-02-25 22:38:36 +00:00
}
return nil
2020-02-25 22:38:36 +00:00
})
deleteResp := DeleteObjectsResponse{}
if !deleteObjects.Quiet {
deleteResp.DeletedObjects = deletedObjects
}
deleteResp.Errors = deleteErrors
writeSuccessResponseXML(w, encodeResponse(deleteResp))
}
2020-06-11 17:53:25 +00:00
func (s3a *S3ApiServer) proxyToFiler(w http.ResponseWriter, r *http.Request, destUrl string, responseFn func(proxyResponse *http.Response, w http.ResponseWriter)) {
2018-07-22 01:49:47 +00: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)
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
}
2020-02-14 17:46:36 +00:00
defer util.CloseResponse(resp)
2018-07-22 01:49:47 +00:00
2018-07-22 02:12:44 +00:00
responseFn(resp, w)
2020-02-14 17:46:36 +00:00
2018-07-22 02:12:44 +00:00
}
2020-06-11 17:53:25 +00:00
func passThroughResponse(proxyResponse *http.Response, w http.ResponseWriter) {
for k, v := range proxyResponse.Header {
2018-07-22 01:49:47 +00:00
w.Header()[k] = v
}
2020-06-11 17:53:25 +00:00
w.WriteHeader(proxyResponse.StatusCode)
io.Copy(w, proxyResponse.Body)
2018-07-22 01:49:47 +00:00
}
2020-02-14 17:09:15 +00:00
func (s3a *S3ApiServer) putToFiler(r *http.Request, uploadUrl string, dataReader io.Reader) (etag string, code ErrorCode) {
2018-09-12 07:46:12 +00:00
hash := md5.New()
var body = io.TeeReader(dataReader, hash)
2018-09-12 07:46:12 +00:00
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()
2018-09-12 07:46:12 +00:00
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
}
2018-09-12 07:46:12 +00:00
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))
2018-09-12 07:46:12 +00:00
return "", ErrInternalError
}
if ret.Error != "" {
glog.Errorf("upload to filer error: %v", ret.Error)
2018-09-12 07:46:12 +00:00
return "", ErrInternalError
}
return etag, ErrNone
}
func setEtag(w http.ResponseWriter, etag string) {
if etag != "" {
2018-09-12 07:46:12 +00:00
if strings.HasPrefix(etag, "\"") {
w.Header().Set("ETag", etag)
} else {
w.Header().Set("ETag", "\""+etag+"\"")
}
}
}
2020-07-26 19:58:58 +00:00
func getBucketAndObject(r *http.Request) (bucket, object string) {
vars := mux.Vars(r)
bucket = vars["bucket"]
object = vars["object"]
2018-09-12 07:46:12 +00:00
if !strings.HasPrefix(object, "/") {
object = "/" + object
}
2020-07-26 19:58:58 +00:00
return
2018-09-12 07:46:12 +00:00
}