2020-08-11 14:37:20 +00:00
|
|
|
package handler
|
|
|
|
|
|
|
|
import (
|
2021-01-14 17:39:48 +00:00
|
|
|
"encoding/xml"
|
2020-08-11 14:37:20 +00:00
|
|
|
"net/http"
|
|
|
|
"strconv"
|
|
|
|
"time"
|
|
|
|
|
2020-10-19 01:05:08 +00:00
|
|
|
"github.com/nspcc-dev/neofs-api-go/pkg/owner"
|
2021-05-18 11:10:08 +00:00
|
|
|
"github.com/nspcc-dev/neofs-s3-gw/api"
|
|
|
|
"github.com/nspcc-dev/neofs-s3-gw/api/layer"
|
2020-08-11 14:37:20 +00:00
|
|
|
"go.uber.org/zap"
|
|
|
|
)
|
|
|
|
|
|
|
|
type listObjectsArgs struct {
|
2021-06-25 12:54:25 +00:00
|
|
|
Bucket string
|
2021-06-29 09:59:33 +00:00
|
|
|
Delimiter string
|
2021-06-25 12:54:25 +00:00
|
|
|
Encode string
|
|
|
|
Marker string
|
|
|
|
StartAfter string
|
|
|
|
MaxKeys int
|
|
|
|
Prefix string
|
|
|
|
APIVersion int
|
2020-08-11 14:37:20 +00:00
|
|
|
}
|
|
|
|
|
2021-05-13 20:25:31 +00:00
|
|
|
// VersioningConfiguration contains VersioningConfiguration XML representation.
|
2021-01-14 17:39:48 +00:00
|
|
|
type VersioningConfiguration struct {
|
|
|
|
XMLName xml.Name `xml:"VersioningConfiguration"`
|
|
|
|
Text string `xml:",chardata"`
|
|
|
|
Xmlns string `xml:"xmlns,attr"`
|
|
|
|
}
|
|
|
|
|
2021-05-13 20:25:31 +00:00
|
|
|
// ListMultipartUploadsResult contains ListMultipartUploadsResult XML representation.
|
2021-01-14 17:39:48 +00:00
|
|
|
type ListMultipartUploadsResult struct {
|
|
|
|
XMLName xml.Name `xml:"ListMultipartUploadsResult"`
|
|
|
|
Text string `xml:",chardata"`
|
|
|
|
Xmlns string `xml:"xmlns,attr"`
|
|
|
|
}
|
|
|
|
|
2020-08-11 14:37:20 +00:00
|
|
|
var maxObjectList = 10000 // Limit number of objects in a listObjectsResponse/listObjectsVersionsResponse.
|
|
|
|
|
2021-05-13 20:25:31 +00:00
|
|
|
// ListBucketsHandler handles bucket listing requests.
|
2020-08-19 23:33:43 +00:00
|
|
|
func (h *handler) ListBucketsHandler(w http.ResponseWriter, r *http.Request) {
|
|
|
|
var (
|
2020-10-19 01:05:08 +00:00
|
|
|
err error
|
|
|
|
own = owner.NewID()
|
2020-08-19 23:33:43 +00:00
|
|
|
res *ListBucketsResponse
|
|
|
|
rid = api.GetRequestID(r.Context())
|
|
|
|
)
|
|
|
|
|
|
|
|
list, err := h.obj.ListBuckets(r.Context())
|
|
|
|
if err != nil {
|
|
|
|
h.log.Error("something went wrong",
|
|
|
|
zap.String("request_id", rid),
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2020-10-23 00:12:37 +00:00
|
|
|
if len(list) > 0 {
|
|
|
|
own = list[0].Owner
|
|
|
|
}
|
|
|
|
|
2020-08-19 23:33:43 +00:00
|
|
|
res = &ListBucketsResponse{
|
|
|
|
Owner: Owner{
|
2020-10-19 01:05:08 +00:00
|
|
|
ID: own.String(),
|
|
|
|
DisplayName: own.String(),
|
2020-08-19 23:33:43 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, item := range list {
|
|
|
|
res.Buckets.Buckets = append(res.Buckets.Buckets, Bucket{
|
|
|
|
Name: item.Name,
|
|
|
|
CreationDate: item.Created.Format(time.RFC3339),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = api.EncodeToResponse(w, res); err != nil {
|
|
|
|
h.log.Error("something went wrong",
|
|
|
|
zap.String("request_id", rid),
|
|
|
|
zap.Error(err))
|
|
|
|
|
|
|
|
api.WriteErrorResponse(r.Context(), w, api.Error{
|
|
|
|
Code: api.GetAPIError(api.ErrInternalError).Code,
|
|
|
|
Description: err.Error(),
|
|
|
|
HTTPStatusCode: http.StatusInternalServerError,
|
|
|
|
}, r.URL)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-22 02:49:00 +00:00
|
|
|
func (h *handler) listObjects(w http.ResponseWriter, r *http.Request) (*listObjectsArgs, *layer.ListObjectsInfo, error) {
|
2020-08-11 14:37:20 +00:00
|
|
|
var (
|
|
|
|
err error
|
|
|
|
arg *listObjectsArgs
|
|
|
|
rid = api.GetRequestID(r.Context())
|
|
|
|
)
|
|
|
|
|
|
|
|
if arg, err = parseListObjectArgs(r); err != nil {
|
|
|
|
h.log.Error("something went wrong",
|
|
|
|
zap.String("request_id", rid),
|
|
|
|
zap.Error(err))
|
|
|
|
|
|
|
|
api.WriteErrorResponse(r.Context(), w, api.Error{
|
|
|
|
Code: api.GetAPIError(api.ErrBadRequest).Code,
|
|
|
|
Description: err.Error(),
|
|
|
|
HTTPStatusCode: http.StatusBadRequest,
|
|
|
|
}, r.URL)
|
|
|
|
|
2020-08-22 02:49:00 +00:00
|
|
|
return nil, nil, err
|
2020-08-11 14:37:20 +00:00
|
|
|
}
|
|
|
|
|
2021-06-25 12:54:25 +00:00
|
|
|
marker := arg.Marker
|
|
|
|
if arg.APIVersion == 2 {
|
|
|
|
marker = arg.StartAfter
|
|
|
|
}
|
|
|
|
|
2020-08-11 14:37:20 +00:00
|
|
|
list, err := h.obj.ListObjects(r.Context(), &layer.ListObjectsParams{
|
|
|
|
Bucket: arg.Bucket,
|
|
|
|
Prefix: arg.Prefix,
|
|
|
|
MaxKeys: arg.MaxKeys,
|
2021-06-29 09:59:33 +00:00
|
|
|
Delimiter: arg.Delimiter,
|
2021-06-25 12:54:25 +00:00
|
|
|
Marker: marker,
|
2021-07-01 10:54:51 +00:00
|
|
|
Version: arg.APIVersion,
|
2020-08-11 14:37:20 +00:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
h.log.Error("something went wrong",
|
|
|
|
zap.String("request_id", rid),
|
|
|
|
zap.Error(err))
|
|
|
|
|
|
|
|
api.WriteErrorResponse(r.Context(), w, api.Error{
|
|
|
|
Code: api.GetAPIError(api.ErrInternalError).Code,
|
|
|
|
Description: err.Error(),
|
|
|
|
HTTPStatusCode: http.StatusInternalServerError,
|
|
|
|
}, r.URL)
|
|
|
|
|
2020-08-22 02:49:00 +00:00
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return arg, list, nil
|
|
|
|
}
|
|
|
|
|
2021-05-13 20:25:31 +00:00
|
|
|
// ListObjectsV1Handler handles objects listing requests for API version 1.
|
2020-08-22 02:49:00 +00:00
|
|
|
func (h *handler) ListObjectsV1Handler(w http.ResponseWriter, r *http.Request) {
|
|
|
|
var rid = api.GetRequestID(r.Context())
|
|
|
|
if arg, list, err := h.listObjects(w, r); err != nil {
|
|
|
|
// error already sent to client
|
2020-08-11 14:37:20 +00:00
|
|
|
return
|
2020-08-22 02:49:00 +00:00
|
|
|
} else if err := api.EncodeToResponse(w, encodeV1(arg, list)); err != nil {
|
|
|
|
h.log.Error("something went wrong",
|
|
|
|
zap.String("request_id", rid),
|
|
|
|
zap.Error(err))
|
|
|
|
|
|
|
|
api.WriteErrorResponse(r.Context(), w, api.Error{
|
|
|
|
Code: api.GetAPIError(api.ErrInternalError).Code,
|
|
|
|
Description: err.Error(),
|
|
|
|
HTTPStatusCode: http.StatusInternalServerError,
|
|
|
|
}, r.URL)
|
2020-08-11 14:37:20 +00:00
|
|
|
}
|
2020-08-22 02:49:00 +00:00
|
|
|
}
|
2020-08-11 14:37:20 +00:00
|
|
|
|
2020-08-22 02:49:00 +00:00
|
|
|
func encodeV1(arg *listObjectsArgs, list *layer.ListObjectsInfo) *ListObjectsResponse {
|
|
|
|
res := &ListObjectsResponse{
|
2020-08-11 14:37:20 +00:00
|
|
|
Name: arg.Bucket,
|
|
|
|
EncodingType: arg.Encode,
|
|
|
|
Marker: arg.Marker,
|
|
|
|
Prefix: arg.Prefix,
|
|
|
|
MaxKeys: arg.MaxKeys,
|
2021-06-29 09:59:33 +00:00
|
|
|
Delimiter: arg.Delimiter,
|
2020-08-11 14:37:20 +00:00
|
|
|
|
|
|
|
IsTruncated: list.IsTruncated,
|
2021-06-25 12:54:25 +00:00
|
|
|
NextMarker: list.NextMarker,
|
2020-08-11 14:37:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// fill common prefixes
|
|
|
|
for i := range list.Prefixes {
|
|
|
|
res.CommonPrefixes = append(res.CommonPrefixes, CommonPrefix{
|
|
|
|
Prefix: list.Prefixes[i],
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// fill contents
|
|
|
|
for _, obj := range list.Objects {
|
|
|
|
res.Contents = append(res.Contents, Object{
|
|
|
|
Key: obj.Name,
|
|
|
|
Size: obj.Size,
|
|
|
|
UserMetadata: obj.Headers,
|
|
|
|
LastModified: obj.Created.Format(time.RFC3339),
|
|
|
|
|
2020-08-22 02:49:00 +00:00
|
|
|
Owner: Owner{
|
|
|
|
ID: obj.Owner.String(),
|
|
|
|
DisplayName: obj.Owner.String(),
|
|
|
|
},
|
|
|
|
|
2021-06-29 13:40:26 +00:00
|
|
|
ETag: obj.HashSum,
|
2020-08-11 14:37:20 +00:00
|
|
|
// StorageClass: "",
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-08-22 02:49:00 +00:00
|
|
|
return res
|
|
|
|
}
|
|
|
|
|
2021-05-13 20:25:31 +00:00
|
|
|
// ListObjectsV2Handler handles objects listing requests for API version 2.
|
2020-08-22 02:49:00 +00:00
|
|
|
func (h *handler) ListObjectsV2Handler(w http.ResponseWriter, r *http.Request) {
|
|
|
|
var rid = api.GetRequestID(r.Context())
|
|
|
|
if arg, list, err := h.listObjects(w, r); err != nil {
|
|
|
|
// error already sent to client
|
|
|
|
return
|
|
|
|
} else if err := api.EncodeToResponse(w, encodeV2(arg, list)); err != nil {
|
2020-08-11 14:37:20 +00:00
|
|
|
h.log.Error("something went wrong",
|
|
|
|
zap.String("request_id", rid),
|
|
|
|
zap.Error(err))
|
|
|
|
|
|
|
|
api.WriteErrorResponse(r.Context(), w, api.Error{
|
2020-08-19 23:33:43 +00:00
|
|
|
Code: api.GetAPIError(api.ErrInternalError).Code,
|
|
|
|
Description: err.Error(),
|
|
|
|
HTTPStatusCode: http.StatusInternalServerError,
|
2020-08-11 14:37:20 +00:00
|
|
|
}, r.URL)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-22 02:49:00 +00:00
|
|
|
func encodeV2(arg *listObjectsArgs, list *layer.ListObjectsInfo) *ListObjectsV2Response {
|
|
|
|
res := &ListObjectsV2Response{
|
|
|
|
Name: arg.Bucket,
|
|
|
|
EncodingType: arg.Encode,
|
|
|
|
Prefix: arg.Prefix,
|
2021-06-25 12:54:25 +00:00
|
|
|
KeyCount: len(list.Objects),
|
2020-08-22 02:49:00 +00:00
|
|
|
MaxKeys: arg.MaxKeys,
|
2021-06-29 09:59:33 +00:00
|
|
|
Delimiter: arg.Delimiter,
|
2021-06-25 12:54:25 +00:00
|
|
|
StartAfter: arg.StartAfter,
|
2020-08-22 02:49:00 +00:00
|
|
|
|
|
|
|
IsTruncated: list.IsTruncated,
|
|
|
|
|
|
|
|
ContinuationToken: arg.Marker,
|
|
|
|
NextContinuationToken: list.NextContinuationToken,
|
|
|
|
}
|
|
|
|
|
|
|
|
// fill common prefixes
|
|
|
|
for i := range list.Prefixes {
|
|
|
|
res.CommonPrefixes = append(res.CommonPrefixes, CommonPrefix{
|
|
|
|
Prefix: list.Prefixes[i],
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// fill contents
|
|
|
|
for _, obj := range list.Objects {
|
|
|
|
res.Contents = append(res.Contents, Object{
|
|
|
|
Key: obj.Name,
|
|
|
|
Size: obj.Size,
|
|
|
|
UserMetadata: obj.Headers,
|
|
|
|
LastModified: obj.Created.Format(time.RFC3339),
|
|
|
|
|
|
|
|
Owner: Owner{
|
|
|
|
ID: obj.Owner.String(),
|
|
|
|
DisplayName: obj.Owner.String(),
|
|
|
|
},
|
|
|
|
|
2021-06-29 13:40:26 +00:00
|
|
|
ETag: obj.HashSum,
|
2020-08-22 02:49:00 +00:00
|
|
|
// StorageClass: "",
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
return res
|
|
|
|
}
|
|
|
|
|
2020-08-11 14:37:20 +00:00
|
|
|
func parseListObjectArgs(r *http.Request) (*listObjectsArgs, error) {
|
|
|
|
var (
|
|
|
|
err error
|
|
|
|
res listObjectsArgs
|
|
|
|
)
|
|
|
|
|
|
|
|
if r.URL.Query().Get("max-keys") == "" {
|
|
|
|
res.MaxKeys = maxObjectList
|
|
|
|
} else if res.MaxKeys, err = strconv.Atoi(r.URL.Query().Get("max-keys")); err != nil {
|
|
|
|
return nil, api.GetAPIError(api.ErrInvalidMaxKeys)
|
|
|
|
}
|
|
|
|
|
|
|
|
res.Prefix = r.URL.Query().Get("prefix")
|
2021-06-25 12:54:25 +00:00
|
|
|
res.Marker = r.URL.Query().Get("marker")
|
2021-06-29 09:59:33 +00:00
|
|
|
res.Delimiter = r.URL.Query().Get("delimiter")
|
2020-08-11 14:37:20 +00:00
|
|
|
res.Encode = r.URL.Query().Get("encoding-type")
|
2021-06-25 12:54:25 +00:00
|
|
|
res.StartAfter = r.URL.Query().Get("start-after")
|
|
|
|
apiVersionStr := r.URL.Query().Get("list-type")
|
|
|
|
|
|
|
|
res.APIVersion = 1
|
|
|
|
if len(apiVersionStr) != 0 {
|
|
|
|
if apiVersion, err := strconv.Atoi(apiVersionStr); err != nil || apiVersion != 2 {
|
|
|
|
return nil, api.GetAPIError(api.ErrIllegalVersioningConfigurationException)
|
|
|
|
}
|
|
|
|
res.APIVersion = 2
|
|
|
|
}
|
2020-08-11 14:37:20 +00:00
|
|
|
|
|
|
|
if info := api.GetReqInfo(r.Context()); info != nil {
|
|
|
|
res.Bucket = info.BucketName
|
|
|
|
}
|
|
|
|
|
|
|
|
return &res, nil
|
|
|
|
}
|
2021-01-14 17:39:48 +00:00
|
|
|
|
2021-05-13 20:25:31 +00:00
|
|
|
// GetBucketVersioningHandler implements bucket versioning getter handler.
|
2021-01-14 17:39:48 +00:00
|
|
|
func (h *handler) GetBucketVersioningHandler(w http.ResponseWriter, r *http.Request) {
|
|
|
|
var (
|
|
|
|
rid = api.GetRequestID(r.Context())
|
|
|
|
res = new(VersioningConfiguration)
|
|
|
|
)
|
|
|
|
|
|
|
|
res.Xmlns = "http://s3.amazonaws.com/doc/2006-03-01/"
|
|
|
|
|
|
|
|
if err := api.EncodeToResponse(w, res); err != nil {
|
|
|
|
h.log.Error("something went wrong",
|
|
|
|
zap.String("request_id", rid),
|
|
|
|
zap.Error(err))
|
|
|
|
|
|
|
|
api.WriteErrorResponse(r.Context(), w, api.Error{
|
|
|
|
Code: api.GetAPIError(api.ErrInternalError).Code,
|
|
|
|
Description: err.Error(),
|
|
|
|
HTTPStatusCode: http.StatusInternalServerError,
|
|
|
|
}, r.URL)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-13 20:25:31 +00:00
|
|
|
// ListMultipartUploadsHandler implements multipart uploads listing handler.
|
2021-01-14 17:39:48 +00:00
|
|
|
func (h *handler) ListMultipartUploadsHandler(w http.ResponseWriter, r *http.Request) {
|
|
|
|
var (
|
|
|
|
rid = api.GetRequestID(r.Context())
|
|
|
|
res = new(ListMultipartUploadsResult)
|
|
|
|
)
|
|
|
|
|
|
|
|
res.Xmlns = "http://s3.amazonaws.com/doc/2006-03-01/"
|
|
|
|
|
|
|
|
if err := api.EncodeToResponse(w, res); err != nil {
|
|
|
|
h.log.Error("something went wrong",
|
|
|
|
zap.String("request_id", rid),
|
|
|
|
zap.Error(err))
|
|
|
|
|
|
|
|
api.WriteErrorResponse(r.Context(), w, api.Error{
|
|
|
|
Code: api.GetAPIError(api.ErrInternalError).Code,
|
|
|
|
Description: err.Error(),
|
|
|
|
HTTPStatusCode: http.StatusInternalServerError,
|
|
|
|
}, r.URL)
|
|
|
|
}
|
|
|
|
}
|