From 079e7a9827cf35e45d26a58d27ea17714e751bcd Mon Sep 17 00:00:00 2001 From: Evgeniy Kulikov Date: Sat, 22 Aug 2020 05:45:48 +0300 Subject: [PATCH] [#28] Implement HeadBucket closes #28 Signed-off-by: Evgeniy Kulikov --- api/handler/head.go | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/api/handler/head.go b/api/handler/head.go index 2d30d9c87..4fcfe5fc0 100644 --- a/api/handler/head.go +++ b/api/handler/head.go @@ -8,6 +8,8 @@ import ( "github.com/nspcc-dev/neofs-s3-gate/api" "github.com/nspcc-dev/neofs-s3-gate/api/layer" "go.uber.org/zap" + "google.golang.org/grpc/codes" + "google.golang.org/grpc/status" ) func (h *handler) HeadObjectHandler(w http.ResponseWriter, r *http.Request) { @@ -45,3 +47,34 @@ func (h *handler) HeadObjectHandler(w http.ResponseWriter, r *http.Request) { w.Header().Set("Last-Modified", inf.Created.Format(http.TimeFormat)) } + +func (h *handler) HeadBucketHandler(w http.ResponseWriter, r *http.Request) { + var ( + req = mux.Vars(r) + bkt = req["bucket"] + rid = api.GetRequestID(r.Context()) + ) + + if _, err := h.obj.GetBucketInfo(r.Context(), bkt); err != nil { + h.log.Error("could not fetch object info", + zap.String("request_id", rid), + zap.String("bucket_name", bkt), + zap.Error(err)) + + code := http.StatusBadRequest + if st, ok := status.FromError(err); ok && st != nil { + switch st.Code() { + case codes.NotFound: + code = http.StatusNotFound + case codes.PermissionDenied: + code = http.StatusForbidden + } + } + + api.WriteResponse(w, code, nil, api.MimeNone) + + return + } + + api.WriteResponse(w, http.StatusOK, nil, api.MimeNone) +}