From 9493f3d281b3cb4197c7c73f65d623dd2e91b033 Mon Sep 17 00:00:00 2001 From: Evgeniy Kulikov Date: Thu, 20 Aug 2020 02:36:17 +0300 Subject: [PATCH] Implement PutObject handler Signed-off-by: Evgeniy Kulikov --- api/handler/put.go | 74 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 74 insertions(+) create mode 100644 api/handler/put.go diff --git a/api/handler/put.go b/api/handler/put.go new file mode 100644 index 00000000..835aa28d --- /dev/null +++ b/api/handler/put.go @@ -0,0 +1,74 @@ +package handler + +import ( + "net/http" + + "github.com/gorilla/mux" + "github.com/nspcc-dev/neofs-s3-gate/api" + "github.com/nspcc-dev/neofs-s3-gate/api/layer" + "go.uber.org/zap" +) + +func (h *handler) PutObjectHandler(w http.ResponseWriter, r *http.Request) { + var ( + err error + req = mux.Vars(r) + bkt = req["bucket"] + obj = req["object"] + rid = api.GetRequestID(r.Context()) + ) + + if _, err := h.obj.GetBucketInfo(r.Context(), bkt); err != nil { + h.log.Error("could not find bucket", + zap.String("request_id", rid), + zap.String("bucket_name", bkt), + zap.Error(err)) + + api.WriteErrorResponse(r.Context(), w, api.Error{ + Code: api.GetAPIError(api.ErrBadRequest).Code, + Description: err.Error(), + HTTPStatusCode: http.StatusBadRequest, + }, r.URL) + + return + } else if _, err = h.obj.GetObjectInfo(r.Context(), bkt, obj); err == nil { + h.log.Error("object exists", + zap.String("request_id", rid), + zap.String("bucket_name", bkt), + zap.String("object_name", obj), + zap.Error(err)) + + api.WriteErrorResponse(r.Context(), w, api.Error{ + Code: api.GetAPIError(api.ErrMethodNotAllowed).Code, + Description: "Object: " + bkt + "#" + obj + " already exists", + HTTPStatusCode: http.StatusBadRequest, + }, r.URL) + + return + } + + params := &layer.PutObjectParams{ + Bucket: bkt, + Object: obj, + Reader: r.Body, + Size: r.ContentLength, + } + + if _, err = h.obj.PutObject(r.Context(), params); err != nil { + h.log.Error("could not upload object", + zap.String("request_id", rid), + zap.String("bucket_name", bkt), + zap.String("object_name", obj), + zap.Error(err)) + + api.WriteErrorResponse(r.Context(), w, api.Error{ + Code: api.GetAPIError(api.ErrInternalError).Code, + Description: err.Error(), + HTTPStatusCode: http.StatusInternalServerError, + }, r.URL) + + return + } + + api.WriteSuccessResponseHeadersOnly(w) +}