2020-08-19 23:36:00 +00:00
|
|
|
package handler
|
|
|
|
|
|
|
|
import (
|
2020-08-22 02:45:15 +00:00
|
|
|
"encoding/xml"
|
2020-08-19 23:36:00 +00:00
|
|
|
"net/http"
|
|
|
|
|
2021-05-18 11:10:08 +00:00
|
|
|
"github.com/nspcc-dev/neofs-s3-gw/api"
|
2021-08-09 08:53:58 +00:00
|
|
|
"github.com/nspcc-dev/neofs-s3-gw/api/errors"
|
2021-06-23 20:25:00 +00:00
|
|
|
"github.com/nspcc-dev/neofs-s3-gw/api/layer"
|
2020-08-19 23:36:00 +00:00
|
|
|
"go.uber.org/zap"
|
2021-08-10 12:08:15 +00:00
|
|
|
"go.uber.org/zap/zapcore"
|
2020-08-19 23:36:00 +00:00
|
|
|
)
|
|
|
|
|
2020-08-22 02:45:15 +00:00
|
|
|
// DeleteObjectsRequest - xml carrying the object key names which needs to be deleted.
|
|
|
|
type DeleteObjectsRequest struct {
|
|
|
|
// Element to enable quiet mode for the request
|
|
|
|
Quiet bool
|
|
|
|
// List of objects to be deleted
|
|
|
|
Objects []ObjectIdentifier `xml:"Object"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// ObjectIdentifier carries key name for the object to delete.
|
|
|
|
type ObjectIdentifier struct {
|
|
|
|
ObjectName string `xml:"Key"`
|
2021-08-10 12:08:15 +00:00
|
|
|
VersionID string `xml:"VersionId,omitempty"`
|
2020-08-22 02:45:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// DeleteError structure.
|
|
|
|
type DeleteError struct {
|
2021-08-19 06:55:22 +00:00
|
|
|
Code string
|
|
|
|
Message string
|
|
|
|
Key string
|
|
|
|
VersionID string `xml:"versionId,omitempty"`
|
2020-08-22 02:45:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// DeleteObjectsResponse container for multiple object deletes.
|
|
|
|
type DeleteObjectsResponse struct {
|
|
|
|
XMLName xml.Name `xml:"http://s3.amazonaws.com/doc/2006-03-01/ DeleteResult" json:"-"`
|
|
|
|
|
|
|
|
// Collection of all deleted objects
|
|
|
|
DeletedObjects []ObjectIdentifier `xml:"Deleted,omitempty"`
|
|
|
|
|
|
|
|
// Collection of errors deleting certain objects.
|
|
|
|
Errors []DeleteError `xml:"Error,omitempty"`
|
|
|
|
}
|
|
|
|
|
2020-08-19 23:36:00 +00:00
|
|
|
func (h *handler) DeleteObjectHandler(w http.ResponseWriter, r *http.Request) {
|
2021-08-05 09:18:52 +00:00
|
|
|
reqInfo := api.GetReqInfo(r.Context())
|
2021-08-10 12:08:15 +00:00
|
|
|
versionedObject := []*layer.VersionedObject{{
|
|
|
|
Name: reqInfo.ObjectName,
|
2021-08-19 06:55:22 +00:00
|
|
|
VersionID: reqInfo.URL.Query().Get(api.QueryVersionID),
|
2021-08-10 12:08:15 +00:00
|
|
|
}}
|
2021-08-05 09:18:52 +00:00
|
|
|
|
2021-08-23 08:19:41 +00:00
|
|
|
if err := h.checkBucketOwner(r, reqInfo.BucketName); err != nil {
|
|
|
|
h.logAndSendError(w, "expected owner doesn't match", reqInfo, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-08-10 12:08:15 +00:00
|
|
|
if errs := h.obj.DeleteObjects(r.Context(), reqInfo.BucketName, versionedObject); len(errs) != 0 && errs[0] != nil {
|
2020-08-19 23:36:00 +00:00
|
|
|
h.log.Error("could not delete object",
|
2021-08-05 09:18:52 +00:00
|
|
|
zap.String("request_id", reqInfo.RequestID),
|
|
|
|
zap.String("bucket_name", reqInfo.BucketName),
|
|
|
|
zap.String("object_name", reqInfo.ObjectName),
|
2021-08-10 12:08:15 +00:00
|
|
|
zap.Error(errs[0]))
|
2020-08-19 23:36:00 +00:00
|
|
|
|
|
|
|
// Ignore delete errors:
|
|
|
|
|
|
|
|
// api.WriteErrorResponse(r.Context(), w, api.Error{
|
|
|
|
// Code: api.GetAPIError(api.ErrInternalError).Code,
|
|
|
|
// Description: err.Error(),
|
|
|
|
// HTTPStatusCode: http.StatusInternalServerError,
|
|
|
|
// }, r.URL)
|
|
|
|
}
|
|
|
|
|
|
|
|
w.WriteHeader(http.StatusNoContent)
|
|
|
|
}
|
2020-08-20 19:35:39 +00:00
|
|
|
|
2021-05-13 19:25:32 +00:00
|
|
|
// DeleteMultipleObjectsHandler handles multiple delete requests.
|
2020-08-20 19:35:39 +00:00
|
|
|
func (h *handler) DeleteMultipleObjectsHandler(w http.ResponseWriter, r *http.Request) {
|
2021-08-05 09:18:52 +00:00
|
|
|
reqInfo := api.GetReqInfo(r.Context())
|
2020-08-22 02:45:15 +00:00
|
|
|
|
|
|
|
// Content-Md5 is requied should be set
|
|
|
|
// http://docs.aws.amazon.com/AmazonS3/latest/API/multiobjectdeleteapi.html
|
|
|
|
if _, ok := r.Header[api.ContentMD5]; !ok {
|
2021-08-09 08:53:58 +00:00
|
|
|
h.logAndSendError(w, "missing Content-MD5", reqInfo, errors.GetAPIError(errors.ErrMissingContentMD5))
|
2020-08-22 02:45:15 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Content-Length is required and should be non-zero
|
|
|
|
// http://docs.aws.amazon.com/AmazonS3/latest/API/multiobjectdeleteapi.html
|
|
|
|
if r.ContentLength <= 0 {
|
2021-08-09 08:53:58 +00:00
|
|
|
h.logAndSendError(w, "missing Content-Length", reqInfo, errors.GetAPIError(errors.ErrMissingContentLength))
|
2020-08-22 02:45:15 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Unmarshal list of keys to be deleted.
|
|
|
|
requested := &DeleteObjectsRequest{}
|
|
|
|
if err := xml.NewDecoder(r.Body).Decode(requested); err != nil {
|
2021-08-05 09:18:52 +00:00
|
|
|
h.logAndSendError(w, "couldn't decode body", reqInfo, err)
|
2020-08-22 02:45:15 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-08-19 06:55:22 +00:00
|
|
|
removed := make(map[string]*layer.VersionedObject)
|
2021-08-10 12:08:15 +00:00
|
|
|
toRemove := make([]*layer.VersionedObject, 0, len(requested.Objects))
|
2020-08-22 02:45:15 +00:00
|
|
|
for _, obj := range requested.Objects {
|
2021-08-10 12:08:15 +00:00
|
|
|
versionedObj := &layer.VersionedObject{
|
|
|
|
Name: obj.ObjectName,
|
|
|
|
VersionID: obj.VersionID,
|
|
|
|
}
|
|
|
|
toRemove = append(toRemove, versionedObj)
|
2021-08-19 06:55:22 +00:00
|
|
|
removed[versionedObj.String()] = versionedObj
|
2020-08-22 02:45:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
response := &DeleteObjectsResponse{
|
|
|
|
Errors: make([]DeleteError, 0, len(toRemove)),
|
|
|
|
DeletedObjects: make([]ObjectIdentifier, 0, len(toRemove)),
|
|
|
|
}
|
|
|
|
|
2021-08-23 08:19:41 +00:00
|
|
|
if err := h.checkBucketOwner(r, reqInfo.BucketName); err != nil {
|
|
|
|
h.logAndSendError(w, "expected owner doesn't match", reqInfo, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-08-10 12:08:15 +00:00
|
|
|
marshaler := zapcore.ArrayMarshalerFunc(func(encoder zapcore.ArrayEncoder) error {
|
|
|
|
for _, obj := range toRemove {
|
|
|
|
encoder.AppendString(obj.String())
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
2021-08-05 09:18:52 +00:00
|
|
|
if errs := h.obj.DeleteObjects(r.Context(), reqInfo.BucketName, toRemove); errs != nil && !requested.Quiet {
|
|
|
|
additional := []zap.Field{
|
2021-08-10 12:08:15 +00:00
|
|
|
zap.Array("objects", marshaler),
|
2021-08-05 09:18:52 +00:00
|
|
|
zap.Errors("errors", errs),
|
|
|
|
}
|
|
|
|
h.logAndSendError(w, "could not delete objects", reqInfo, nil, additional...)
|
2020-08-22 02:45:15 +00:00
|
|
|
|
|
|
|
for _, e := range errs {
|
2021-08-19 06:55:22 +00:00
|
|
|
if err, ok := e.(*errors.ObjectError); ok {
|
2020-08-22 02:45:15 +00:00
|
|
|
code := "BadRequest"
|
2021-08-19 06:55:22 +00:00
|
|
|
if s3err, ok := err.Err.(errors.Error); ok {
|
|
|
|
code = s3err.Code
|
|
|
|
}
|
2020-08-22 02:45:15 +00:00
|
|
|
|
|
|
|
response.Errors = append(response.Errors, DeleteError{
|
2021-08-19 06:55:22 +00:00
|
|
|
Code: code,
|
|
|
|
Message: err.Error(),
|
|
|
|
Key: err.Object,
|
|
|
|
VersionID: err.Version,
|
2020-08-22 02:45:15 +00:00
|
|
|
})
|
|
|
|
|
2021-08-19 06:55:22 +00:00
|
|
|
delete(removed, err.ObjectVersion())
|
2020-08-22 02:45:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-19 06:55:22 +00:00
|
|
|
for _, val := range removed {
|
|
|
|
response.DeletedObjects = append(response.DeletedObjects, ObjectIdentifier{ObjectName: val.Name, VersionID: val.VersionID})
|
2020-08-22 02:45:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if err := api.EncodeToResponse(w, response); err != nil {
|
2021-08-10 12:08:15 +00:00
|
|
|
h.logAndSendError(w, "could not write response", reqInfo, err, zap.Array("objects", marshaler))
|
2020-10-24 13:09:22 +00:00
|
|
|
return
|
2020-08-22 02:45:15 +00:00
|
|
|
}
|
2020-08-20 19:35:39 +00:00
|
|
|
}
|
2021-06-23 20:25:00 +00:00
|
|
|
|
|
|
|
func (h *handler) DeleteBucketHandler(w http.ResponseWriter, r *http.Request) {
|
2021-08-05 09:18:52 +00:00
|
|
|
reqInfo := api.GetReqInfo(r.Context())
|
2021-08-23 08:19:41 +00:00
|
|
|
if err := h.checkBucketOwner(r, reqInfo.BucketName); err != nil {
|
|
|
|
h.logAndSendError(w, "expected owner doesn't match", reqInfo, err)
|
|
|
|
return
|
|
|
|
}
|
2021-08-05 09:18:52 +00:00
|
|
|
if err := h.obj.DeleteBucket(r.Context(), &layer.DeleteBucketParams{Name: reqInfo.BucketName}); err != nil {
|
|
|
|
h.logAndSendError(w, "couldn't delete bucket", reqInfo, err)
|
2021-06-23 20:25:00 +00:00
|
|
|
}
|
|
|
|
}
|