From 1d98c4ecc27cbac505a2e9c0c8b8e33a284adf17 Mon Sep 17 00:00:00 2001 From: Evgeniy Kulikov Date: Thu, 20 Aug 2020 02:36:46 +0300 Subject: [PATCH] Implement GetObject handler Signed-off-by: Evgeniy Kulikov --- api/handler/get.go | 69 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 69 insertions(+) create mode 100644 api/handler/get.go diff --git a/api/handler/get.go b/api/handler/get.go new file mode 100644 index 00000000..a2f528f2 --- /dev/null +++ b/api/handler/get.go @@ -0,0 +1,69 @@ +package handler + +import ( + "net/http" + "strconv" + + "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) GetObjectHandler(w http.ResponseWriter, r *http.Request) { + var ( + err error + inf *layer.ObjectInfo + + req = mux.Vars(r) + bkt = req["bucket"] + obj = req["object"] + rid = api.GetRequestID(r.Context()) + ) + + params := &layer.GetObjectParams{ + Bucket: bkt, + Object: obj, + Writer: w, + } + + if inf, err = h.obj.GetObjectInfo(r.Context(), bkt, obj); err != nil { + h.log.Error("could not find 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 + } + + params.Length = inf.Size + + if err = h.obj.GetObject(r.Context(), params); err != nil { + h.log.Error("could not get 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 + } + + w.Header().Set("Content-Type", inf.ContentType) + w.Header().Set("Content-Length", strconv.FormatInt(inf.Size, 10)) + + w.Header().Set("Last-Modified", inf.Created.Format(http.TimeFormat)) + +}