forked from TrueCloudLab/frostfs-s3-gw
Implement GetObject handler
Signed-off-by: Evgeniy Kulikov <kim@nspcc.ru>
This commit is contained in:
parent
bfc4b8786f
commit
1d98c4ecc2
1 changed files with 69 additions and 0 deletions
69
api/handler/get.go
Normal file
69
api/handler/get.go
Normal file
|
@ -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))
|
||||
|
||||
}
|
Loading…
Reference in a new issue