Add delete objects api (#303)

Supports single and multiple objects which needs to be defined by recursive flag.
An object to be deleted needs to be defined by a query parameter, path, since it can be
an object or a folder.
This commit is contained in:
Cesar N
2020-10-01 17:00:32 -07:00
committed by GitHub
parent a42f1ff4ee
commit fcf5d5c9f7
17 changed files with 1129 additions and 48 deletions

View File

@@ -123,6 +123,8 @@ type MCClient interface {
addNotificationConfig(ctx context.Context, arn string, events []string, prefix, suffix string, ignoreExisting bool) *probe.Error
removeNotificationConfig(ctx context.Context, arn string, event string, prefix string, suffix string) *probe.Error
watch(ctx context.Context, options mc.WatchOptions) (*mc.WatchObject, *probe.Error)
remove(ctx context.Context, isIncomplete, isRemoveBucket, isBypass bool, contentCh <-chan *mc.ClientContent) <-chan *probe.Error
list(ctx context.Context, opts mc.ListOptions) <-chan *mc.ClientContent
}
// Interface implementation
@@ -151,6 +153,14 @@ func (c mcClient) setReplication(ctx context.Context, cfg *replication.Config, o
return c.client.SetReplication(ctx, cfg, opts)
}
func (c mcClient) remove(ctx context.Context, isIncomplete, isRemoveBucket, isBypass bool, contentCh <-chan *mc.ClientContent) <-chan *probe.Error {
return c.client.Remove(ctx, isIncomplete, isRemoveBucket, isBypass, contentCh)
}
func (c mcClient) list(ctx context.Context, opts mc.ListOptions) <-chan *mc.ClientContent {
return c.client.List(ctx, opts)
}
// ConsoleCredentials interface with all functions to be implemented
// by mock when testing, it should include all needed consoleCredentials.Login api calls
// that are used within this project.
@@ -274,7 +284,7 @@ func newMinioClient(claims *models.Principal) (*minio.Client, error) {
}
// newS3BucketClient creates a new mc S3Client to talk to the server based on a bucket
func newS3BucketClient(claims *models.Principal, bucketName string) (*mc.S3Client, error) {
func newS3BucketClient(claims *models.Principal, bucketName string, prefix string) (*mc.S3Client, error) {
endpoint := getMinIOServer()
useTLS := getMinIOEndpointIsSecure()
@@ -282,6 +292,10 @@ func newS3BucketClient(claims *models.Principal, bucketName string) (*mc.S3Clien
endpoint += fmt.Sprintf("/%s", bucketName)
}
if strings.TrimSpace(prefix) != "" {
endpoint += fmt.Sprintf("/%s", prefix)
}
if claims == nil {
return nil, fmt.Errorf("the provided credentials are invalid")
}

View File

@@ -380,6 +380,48 @@ func init() {
}
}
}
},
"delete": {
"tags": [
"UserAPI"
],
"summary": "Delete Object",
"operationId": "DeleteObject",
"parameters": [
{
"type": "string",
"name": "bucket_name",
"in": "path",
"required": true
},
{
"type": "string",
"name": "path",
"in": "query",
"required": true
},
{
"type": "string",
"name": "version_id",
"in": "query"
},
{
"type": "boolean",
"name": "recursive",
"in": "query"
}
],
"responses": {
"200": {
"description": "A successful response."
},
"default": {
"description": "Generic error response.",
"schema": {
"$ref": "#/definitions/error"
}
}
}
}
},
"/buckets/{bucket_name}/replication": {
@@ -4620,6 +4662,48 @@ func init() {
}
}
}
},
"delete": {
"tags": [
"UserAPI"
],
"summary": "Delete Object",
"operationId": "DeleteObject",
"parameters": [
{
"type": "string",
"name": "bucket_name",
"in": "path",
"required": true
},
{
"type": "string",
"name": "path",
"in": "query",
"required": true
},
{
"type": "string",
"name": "version_id",
"in": "query"
},
{
"type": "boolean",
"name": "recursive",
"in": "query"
}
],
"responses": {
"200": {
"description": "A successful response."
},
"default": {
"description": "Generic error response.",
"schema": {
"$ref": "#/definitions/error"
}
}
}
}
},
"/buckets/{bucket_name}/replication": {

View File

@@ -114,6 +114,9 @@ func NewConsoleAPI(spec *loads.Document) *ConsoleAPI {
UserAPIDeleteBucketEventHandler: user_api.DeleteBucketEventHandlerFunc(func(params user_api.DeleteBucketEventParams, principal *models.Principal) middleware.Responder {
return middleware.NotImplemented("operation user_api.DeleteBucketEvent has not yet been implemented")
}),
UserAPIDeleteObjectHandler: user_api.DeleteObjectHandlerFunc(func(params user_api.DeleteObjectParams, principal *models.Principal) middleware.Responder {
return middleware.NotImplemented("operation user_api.DeleteObject has not yet been implemented")
}),
UserAPIDeleteRemoteBucketHandler: user_api.DeleteRemoteBucketHandlerFunc(func(params user_api.DeleteRemoteBucketParams, principal *models.Principal) middleware.Responder {
return middleware.NotImplemented("operation user_api.DeleteRemoteBucket has not yet been implemented")
}),
@@ -347,6 +350,8 @@ type ConsoleAPI struct {
UserAPIDeleteBucketHandler user_api.DeleteBucketHandler
// UserAPIDeleteBucketEventHandler sets the operation handler for the delete bucket event operation
UserAPIDeleteBucketEventHandler user_api.DeleteBucketEventHandler
// UserAPIDeleteObjectHandler sets the operation handler for the delete object operation
UserAPIDeleteObjectHandler user_api.DeleteObjectHandler
// UserAPIDeleteRemoteBucketHandler sets the operation handler for the delete remote bucket operation
UserAPIDeleteRemoteBucketHandler user_api.DeleteRemoteBucketHandler
// UserAPIDeleteServiceAccountHandler sets the operation handler for the delete service account operation
@@ -571,6 +576,9 @@ func (o *ConsoleAPI) Validate() error {
if o.UserAPIDeleteBucketEventHandler == nil {
unregistered = append(unregistered, "user_api.DeleteBucketEventHandler")
}
if o.UserAPIDeleteObjectHandler == nil {
unregistered = append(unregistered, "user_api.DeleteObjectHandler")
}
if o.UserAPIDeleteRemoteBucketHandler == nil {
unregistered = append(unregistered, "user_api.DeleteRemoteBucketHandler")
}
@@ -892,6 +900,10 @@ func (o *ConsoleAPI) initHandlerCache() {
if o.handlers["DELETE"] == nil {
o.handlers["DELETE"] = make(map[string]http.Handler)
}
o.handlers["DELETE"]["/buckets/{bucket_name}/objects"] = user_api.NewDeleteObject(o.context, o.UserAPIDeleteObjectHandler)
if o.handlers["DELETE"] == nil {
o.handlers["DELETE"] = make(map[string]http.Handler)
}
o.handlers["DELETE"]["/remote-buckets/{source-bucket-name}/{arn}"] = user_api.NewDeleteRemoteBucket(o.context, o.UserAPIDeleteRemoteBucketHandler)
if o.handlers["DELETE"] == nil {
o.handlers["DELETE"] = make(map[string]http.Handler)

View File

@@ -0,0 +1,90 @@
// Code generated by go-swagger; DO NOT EDIT.
// This file is part of MinIO Console Server
// Copyright (c) 2020 MinIO, Inc.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
//
package user_api
// This file was generated by the swagger tool.
// Editing this file might prove futile when you re-run the generate command
import (
"net/http"
"github.com/go-openapi/runtime/middleware"
"github.com/minio/console/models"
)
// DeleteObjectHandlerFunc turns a function with the right signature into a delete object handler
type DeleteObjectHandlerFunc func(DeleteObjectParams, *models.Principal) middleware.Responder
// Handle executing the request and returning a response
func (fn DeleteObjectHandlerFunc) Handle(params DeleteObjectParams, principal *models.Principal) middleware.Responder {
return fn(params, principal)
}
// DeleteObjectHandler interface for that can handle valid delete object params
type DeleteObjectHandler interface {
Handle(DeleteObjectParams, *models.Principal) middleware.Responder
}
// NewDeleteObject creates a new http.Handler for the delete object operation
func NewDeleteObject(ctx *middleware.Context, handler DeleteObjectHandler) *DeleteObject {
return &DeleteObject{Context: ctx, Handler: handler}
}
/*DeleteObject swagger:route DELETE /buckets/{bucket_name}/objects UserAPI deleteObject
Delete Object
*/
type DeleteObject struct {
Context *middleware.Context
Handler DeleteObjectHandler
}
func (o *DeleteObject) ServeHTTP(rw http.ResponseWriter, r *http.Request) {
route, rCtx, _ := o.Context.RouteInfo(r)
if rCtx != nil {
r = rCtx
}
var Params = NewDeleteObjectParams()
uprinc, aCtx, err := o.Context.Authorize(r, route)
if err != nil {
o.Context.Respond(rw, r, route.Produces, route, err)
return
}
if aCtx != nil {
r = aCtx
}
var principal *models.Principal
if uprinc != nil {
principal = uprinc.(*models.Principal) // this is really a models.Principal, I promise
}
if err := o.Context.BindValidRequest(r, route, &Params); err != nil { // bind params
o.Context.Respond(rw, r, route.Produces, route, err)
return
}
res := o.Handler.Handle(Params, principal) // actually handle the request
o.Context.Respond(rw, r, route.Produces, route, res)
}

View File

@@ -0,0 +1,183 @@
// Code generated by go-swagger; DO NOT EDIT.
// This file is part of MinIO Console Server
// Copyright (c) 2020 MinIO, Inc.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
//
package user_api
// This file was generated by the swagger tool.
// Editing this file might prove futile when you re-run the swagger generate command
import (
"net/http"
"github.com/go-openapi/errors"
"github.com/go-openapi/runtime"
"github.com/go-openapi/runtime/middleware"
"github.com/go-openapi/strfmt"
"github.com/go-openapi/swag"
"github.com/go-openapi/validate"
)
// NewDeleteObjectParams creates a new DeleteObjectParams object
// no default values defined in spec.
func NewDeleteObjectParams() DeleteObjectParams {
return DeleteObjectParams{}
}
// DeleteObjectParams contains all the bound params for the delete object operation
// typically these are obtained from a http.Request
//
// swagger:parameters DeleteObject
type DeleteObjectParams struct {
// HTTP Request Object
HTTPRequest *http.Request `json:"-"`
/*
Required: true
In: path
*/
BucketName string
/*
Required: true
In: query
*/
Path string
/*
In: query
*/
Recursive *bool
/*
In: query
*/
VersionID *string
}
// BindRequest both binds and validates a request, it assumes that complex things implement a Validatable(strfmt.Registry) error interface
// for simple values it will use straight method calls.
//
// To ensure default values, the struct must have been initialized with NewDeleteObjectParams() beforehand.
func (o *DeleteObjectParams) BindRequest(r *http.Request, route *middleware.MatchedRoute) error {
var res []error
o.HTTPRequest = r
qs := runtime.Values(r.URL.Query())
rBucketName, rhkBucketName, _ := route.Params.GetOK("bucket_name")
if err := o.bindBucketName(rBucketName, rhkBucketName, route.Formats); err != nil {
res = append(res, err)
}
qPath, qhkPath, _ := qs.GetOK("path")
if err := o.bindPath(qPath, qhkPath, route.Formats); err != nil {
res = append(res, err)
}
qRecursive, qhkRecursive, _ := qs.GetOK("recursive")
if err := o.bindRecursive(qRecursive, qhkRecursive, route.Formats); err != nil {
res = append(res, err)
}
qVersionID, qhkVersionID, _ := qs.GetOK("version_id")
if err := o.bindVersionID(qVersionID, qhkVersionID, route.Formats); err != nil {
res = append(res, err)
}
if len(res) > 0 {
return errors.CompositeValidationError(res...)
}
return nil
}
// bindBucketName binds and validates parameter BucketName from path.
func (o *DeleteObjectParams) bindBucketName(rawData []string, hasKey bool, formats strfmt.Registry) error {
var raw string
if len(rawData) > 0 {
raw = rawData[len(rawData)-1]
}
// Required: true
// Parameter is provided by construction from the route
o.BucketName = raw
return nil
}
// bindPath binds and validates parameter Path from query.
func (o *DeleteObjectParams) bindPath(rawData []string, hasKey bool, formats strfmt.Registry) error {
if !hasKey {
return errors.Required("path", "query", rawData)
}
var raw string
if len(rawData) > 0 {
raw = rawData[len(rawData)-1]
}
// Required: true
// AllowEmptyValue: false
if err := validate.RequiredString("path", "query", raw); err != nil {
return err
}
o.Path = raw
return nil
}
// bindRecursive binds and validates parameter Recursive from query.
func (o *DeleteObjectParams) bindRecursive(rawData []string, hasKey bool, formats strfmt.Registry) error {
var raw string
if len(rawData) > 0 {
raw = rawData[len(rawData)-1]
}
// Required: false
// AllowEmptyValue: false
if raw == "" { // empty values pass all other validations
return nil
}
value, err := swag.ConvertBool(raw)
if err != nil {
return errors.InvalidType("recursive", "query", "bool", raw)
}
o.Recursive = &value
return nil
}
// bindVersionID binds and validates parameter VersionID from query.
func (o *DeleteObjectParams) bindVersionID(rawData []string, hasKey bool, formats strfmt.Registry) error {
var raw string
if len(rawData) > 0 {
raw = rawData[len(rawData)-1]
}
// Required: false
// AllowEmptyValue: false
if raw == "" { // empty values pass all other validations
return nil
}
o.VersionID = &raw
return nil
}

View File

@@ -0,0 +1,113 @@
// Code generated by go-swagger; DO NOT EDIT.
// This file is part of MinIO Console Server
// Copyright (c) 2020 MinIO, Inc.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
//
package user_api
// This file was generated by the swagger tool.
// Editing this file might prove futile when you re-run the swagger generate command
import (
"net/http"
"github.com/go-openapi/runtime"
"github.com/minio/console/models"
)
// DeleteObjectOKCode is the HTTP code returned for type DeleteObjectOK
const DeleteObjectOKCode int = 200
/*DeleteObjectOK A successful response.
swagger:response deleteObjectOK
*/
type DeleteObjectOK struct {
}
// NewDeleteObjectOK creates DeleteObjectOK with default headers values
func NewDeleteObjectOK() *DeleteObjectOK {
return &DeleteObjectOK{}
}
// WriteResponse to the client
func (o *DeleteObjectOK) WriteResponse(rw http.ResponseWriter, producer runtime.Producer) {
rw.Header().Del(runtime.HeaderContentType) //Remove Content-Type on empty responses
rw.WriteHeader(200)
}
/*DeleteObjectDefault Generic error response.
swagger:response deleteObjectDefault
*/
type DeleteObjectDefault struct {
_statusCode int
/*
In: Body
*/
Payload *models.Error `json:"body,omitempty"`
}
// NewDeleteObjectDefault creates DeleteObjectDefault with default headers values
func NewDeleteObjectDefault(code int) *DeleteObjectDefault {
if code <= 0 {
code = 500
}
return &DeleteObjectDefault{
_statusCode: code,
}
}
// WithStatusCode adds the status to the delete object default response
func (o *DeleteObjectDefault) WithStatusCode(code int) *DeleteObjectDefault {
o._statusCode = code
return o
}
// SetStatusCode sets the status to the delete object default response
func (o *DeleteObjectDefault) SetStatusCode(code int) {
o._statusCode = code
}
// WithPayload adds the payload to the delete object default response
func (o *DeleteObjectDefault) WithPayload(payload *models.Error) *DeleteObjectDefault {
o.Payload = payload
return o
}
// SetPayload sets the payload to the delete object default response
func (o *DeleteObjectDefault) SetPayload(payload *models.Error) {
o.Payload = payload
}
// WriteResponse to the client
func (o *DeleteObjectDefault) WriteResponse(rw http.ResponseWriter, producer runtime.Producer) {
rw.WriteHeader(o._statusCode)
if o.Payload != nil {
payload := o.Payload
if err := producer.Produce(rw, payload); err != nil {
panic(err) // let the recovery middleware deal with this
}
}
}

View File

@@ -0,0 +1,147 @@
// Code generated by go-swagger; DO NOT EDIT.
// This file is part of MinIO Console Server
// Copyright (c) 2020 MinIO, Inc.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
//
package user_api
// This file was generated by the swagger tool.
// Editing this file might prove futile when you re-run the generate command
import (
"errors"
"net/url"
golangswaggerpaths "path"
"strings"
"github.com/go-openapi/swag"
)
// DeleteObjectURL generates an URL for the delete object operation
type DeleteObjectURL struct {
BucketName string
Path string
Recursive *bool
VersionID *string
_basePath string
// avoid unkeyed usage
_ struct{}
}
// WithBasePath sets the base path for this url builder, only required when it's different from the
// base path specified in the swagger spec.
// When the value of the base path is an empty string
func (o *DeleteObjectURL) WithBasePath(bp string) *DeleteObjectURL {
o.SetBasePath(bp)
return o
}
// SetBasePath sets the base path for this url builder, only required when it's different from the
// base path specified in the swagger spec.
// When the value of the base path is an empty string
func (o *DeleteObjectURL) SetBasePath(bp string) {
o._basePath = bp
}
// Build a url path and query string
func (o *DeleteObjectURL) Build() (*url.URL, error) {
var _result url.URL
var _path = "/buckets/{bucket_name}/objects"
bucketName := o.BucketName
if bucketName != "" {
_path = strings.Replace(_path, "{bucket_name}", bucketName, -1)
} else {
return nil, errors.New("bucketName is required on DeleteObjectURL")
}
_basePath := o._basePath
if _basePath == "" {
_basePath = "/api/v1"
}
_result.Path = golangswaggerpaths.Join(_basePath, _path)
qs := make(url.Values)
pathQ := o.Path
if pathQ != "" {
qs.Set("path", pathQ)
}
var recursiveQ string
if o.Recursive != nil {
recursiveQ = swag.FormatBool(*o.Recursive)
}
if recursiveQ != "" {
qs.Set("recursive", recursiveQ)
}
var versionIDQ string
if o.VersionID != nil {
versionIDQ = *o.VersionID
}
if versionIDQ != "" {
qs.Set("version_id", versionIDQ)
}
_result.RawQuery = qs.Encode()
return &_result, nil
}
// Must is a helper function to panic when the url builder returns an error
func (o *DeleteObjectURL) Must(u *url.URL, err error) *url.URL {
if err != nil {
panic(err)
}
if u == nil {
panic("url can't be nil")
}
return u
}
// String returns the string representation of the path with query string
func (o *DeleteObjectURL) String() string {
return o.Must(o.Build()).String()
}
// BuildFull builds a full url with scheme, host, path and query string
func (o *DeleteObjectURL) BuildFull(scheme, host string) (*url.URL, error) {
if scheme == "" {
return nil, errors.New("scheme is required for a full url on DeleteObjectURL")
}
if host == "" {
return nil, errors.New("host is required for a full url on DeleteObjectURL")
}
base, err := o.Build()
if err != nil {
return nil, err
}
base.Scheme = scheme
base.Host = host
return base, nil
}
// StringFull returns the string representation of a complete url
func (o *DeleteObjectURL) StringFull(scheme, host string) string {
return o.Must(o.BuildFull(scheme, host)).String()
}

View File

@@ -132,7 +132,7 @@ func getAddBucketReplicationdResponse(session *models.Principal, bucketName stri
}
maxPrio++
s3Client, err := newS3BucketClient(session, bucketName)
s3Client, err := newS3BucketClient(session, bucketName, "")
if err != nil {
log.Println("error creating S3Client:", err)
return err

View File

@@ -177,7 +177,7 @@ func createBucketEvent(ctx context.Context, client MCClient, arn string, notific
// getCreateBucketEventsResponse calls createBucketEvent to add a bucket event notification
func getCreateBucketEventsResponse(session *models.Principal, bucketName string, eventReq *models.BucketEventRequest) *models.Error {
ctx := context.Background()
s3Client, err := newS3BucketClient(session, bucketName)
s3Client, err := newS3BucketClient(session, bucketName, "")
if err != nil {
return prepareError(err)
}
@@ -212,7 +212,7 @@ func joinNotificationEvents(events []models.NotificationEventType) string {
// getDeleteBucketEventsResponse calls deleteBucketEventNotification() to delete a bucket event notification
func getDeleteBucketEventsResponse(session *models.Principal, bucketName string, arn string, events []models.NotificationEventType, prefix, suffix *string) *models.Error {
ctx := context.Background()
s3Client, err := newS3BucketClient(session, bucketName)
s3Client, err := newS3BucketClient(session, bucketName, "")
if err != nil {
return prepareError(err)
}

View File

@@ -18,15 +18,26 @@ package restapi
import (
"context"
"fmt"
"path/filepath"
"regexp"
"strings"
"time"
"github.com/go-openapi/runtime/middleware"
"github.com/minio/console/models"
"github.com/minio/console/restapi/operations"
"github.com/minio/console/restapi/operations/user_api"
mc "github.com/minio/mc/cmd"
"github.com/minio/minio-go/v7"
)
// enum types
const (
objectStorage = iota // MinIO and S3 compatible cloud storage
fileSystem // POSIX compatible file systems
)
func registerObjectsHandlers(api *operations.ConsoleAPI) {
// list objects
api.UserAPIListObjectsHandler = user_api.ListObjectsHandlerFunc(func(params user_api.ListObjectsParams, session *models.Principal) middleware.Responder {
@@ -36,24 +47,13 @@ func registerObjectsHandlers(api *operations.ConsoleAPI) {
}
return user_api.NewListObjectsOK().WithPayload(resp)
})
}
// listBucketObjects gets an array of objects in a bucket
func listBucketObjects(ctx context.Context, client MinioClient, bucketName string, prefix string, recursive bool) ([]*models.BucketObject, error) {
var objects []*models.BucketObject
for lsObj := range client.listObjects(ctx, bucketName, minio.ListObjectsOptions{Prefix: prefix, Recursive: recursive}) {
if lsObj.Err != nil {
return nil, lsObj.Err
// delete object
api.UserAPIDeleteObjectHandler = user_api.DeleteObjectHandlerFunc(func(params user_api.DeleteObjectParams, session *models.Principal) middleware.Responder {
if err := getDeleteObjectResponse(session, params); err != nil {
return user_api.NewDeleteObjectDefault(int(err.Code)).WithPayload(err)
}
obj := &models.BucketObject{
Name: lsObj.Key,
Size: lsObj.Size,
LastModified: lsObj.LastModified.String(),
ContentType: lsObj.ContentType,
}
objects = append(objects, obj)
}
return objects, nil
return user_api.NewDeleteObjectOK()
})
}
// getListObjectsResponse returns a list of objects
@@ -91,3 +91,214 @@ func getListObjectsResponse(session *models.Principal, params user_api.ListObjec
}
return resp, nil
}
// listBucketObjects gets an array of objects in a bucket
func listBucketObjects(ctx context.Context, client MinioClient, bucketName string, prefix string, recursive bool) ([]*models.BucketObject, error) {
var objects []*models.BucketObject
for lsObj := range client.listObjects(ctx, bucketName, minio.ListObjectsOptions{Prefix: prefix, Recursive: recursive}) {
if lsObj.Err != nil {
return nil, lsObj.Err
}
obj := &models.BucketObject{
Name: lsObj.Key,
Size: lsObj.Size,
LastModified: lsObj.LastModified.String(),
ContentType: lsObj.ContentType,
}
objects = append(objects, obj)
}
return objects, nil
}
// getDeleteObjectResponse returns whether there was an error on deletion of object
func getDeleteObjectResponse(session *models.Principal, params user_api.DeleteObjectParams) *models.Error {
ctx := context.Background()
s3Client, err := newS3BucketClient(session, params.BucketName, params.Path)
if err != nil {
return prepareError(err)
}
// create a mc S3Client interface implementation
// defining the client to be used
mcClient := mcClient{client: s3Client}
var rec bool
var version string
if params.Recursive != nil {
rec = *params.Recursive
}
if params.VersionID != nil {
version = *params.VersionID
}
err = deleteObjects(ctx, mcClient, params.BucketName, params.Path, version, rec)
if err != nil {
return prepareError(err)
}
return nil
}
// deleteObjects deletes either a single object or multiple objects based on recursive flag
func deleteObjects(ctx context.Context, client MCClient, bucket, path string, versionID string, recursive bool) error {
if recursive {
if err := deleteMultipleObjects(ctx, client, recursive); err != nil {
return err
}
} else {
if err := deleteSingleObject(ctx, client, bucket, path, versionID); err != nil {
return err
}
}
return nil
}
// deleteMultipleObjects uses listing before removal, it can list recursively or not,
// Use cases:
// * Remove objects recursively
func deleteMultipleObjects(ctx context.Context, client MCClient, recursive bool) error {
isRemoveBucket := false
isIncomplete := false
isBypass := false
listOpts := mc.ListOptions{IsRecursive: recursive, IsIncomplete: isIncomplete, ShowDir: mc.DirNone}
// TODO: support older Versions
contentCh := make(chan *mc.ClientContent, 1)
errorCh := client.remove(ctx, isIncomplete, isRemoveBucket, isBypass, contentCh)
for content := range client.list(ctx, listOpts) {
if content.Err != nil {
switch content.Err.ToGoError().(type) {
// ignore same as mc
case mc.PathInsufficientPermission:
// Ignore Permission error.
continue
}
close(contentCh)
return content.Err.Cause
}
sent := false
for !sent {
select {
case contentCh <- content:
sent = true
case pErr := <-errorCh:
switch pErr.ToGoError().(type) {
// ignore same as mc
case mc.PathInsufficientPermission:
// Ignore Permission error.
continue
}
close(contentCh)
return pErr.Cause
}
}
}
close(contentCh)
for pErr := range errorCh {
if pErr != nil {
switch pErr.ToGoError().(type) {
// ignore same as mc
case mc.PathInsufficientPermission:
// Ignore Permission error.
continue
}
return pErr.Cause
}
}
return nil
}
func deleteSingleObject(ctx context.Context, client MCClient, bucket, object string, versionID string) error {
targetURL := fmt.Sprintf("%s/%s", bucket, object)
contentCh := make(chan *mc.ClientContent, 1)
contentCh <- &mc.ClientContent{URL: *newClientURL(targetURL), VersionID: versionID}
close(contentCh)
isRemoveBucket := false
isIncomplete := false
isBypass := false
errorCh := client.remove(ctx, isIncomplete, isRemoveBucket, isBypass, contentCh)
for pErr := range errorCh {
if pErr != nil {
switch pErr.ToGoError().(type) {
// ignore same as mc
case mc.PathInsufficientPermission:
// Ignore Permission error.
continue
}
return pErr.Cause
}
}
return nil
}
// newClientURL returns an abstracted URL for filesystems and object storage.
func newClientURL(urlStr string) *mc.ClientURL {
scheme, rest := getScheme(urlStr)
if strings.HasPrefix(rest, "//") {
// if rest has '//' prefix, skip them
var authority string
authority, rest = splitSpecial(rest[2:], "/", false)
if rest == "" {
rest = "/"
}
host := getHost(authority)
if host != "" && (scheme == "http" || scheme == "https") {
return &mc.ClientURL{
Scheme: scheme,
Type: objectStorage,
Host: host,
Path: rest,
SchemeSeparator: "://",
Separator: '/',
}
}
}
return &mc.ClientURL{
Type: fileSystem,
Path: rest,
Separator: filepath.Separator,
}
}
// Maybe rawurl is of the form scheme:path. (Scheme must be [a-zA-Z][a-zA-Z0-9+-.]*)
// If so, return scheme, path; else return "", rawurl.
func getScheme(rawurl string) (scheme, path string) {
urlSplits := strings.Split(rawurl, "://")
if len(urlSplits) == 2 {
scheme, uri := urlSplits[0], "//"+urlSplits[1]
// ignore numbers in scheme
validScheme := regexp.MustCompile("^[a-zA-Z]+$")
if uri != "" {
if validScheme.MatchString(scheme) {
return scheme, uri
}
}
}
return "", rawurl
}
// Assuming s is of the form [s delimiter s].
// If so, return s, [delimiter]s or return s, s if cutdelimiter == true
// If no delimiter found return s, "".
func splitSpecial(s string, delimiter string, cutdelimiter bool) (string, string) {
i := strings.Index(s, delimiter)
if i < 0 {
// if delimiter not found return as is.
return s, ""
}
// if delimiter should be removed, remove it.
if cutdelimiter {
return s[0:i], s[i+len(delimiter):]
}
// return split strings with delimiter
return s[0:i], s[i:]
}
// getHost - extract host from authority string, we do not support ftp style username@ yet.
func getHost(authority string) (host string) {
i := strings.LastIndex(authority, "@")
if i >= 0 {
// TODO support, username@password style userinfo, useful for ftp support.
return
}
return authority
}

View File

@@ -25,17 +25,30 @@ import (
"time"
"github.com/minio/console/models"
mc "github.com/minio/mc/cmd"
"github.com/minio/mc/pkg/probe"
"github.com/minio/minio-go/v7"
)
var minioListObjectsMock func(ctx context.Context, bucket string, opts minio.ListObjectsOptions) <-chan minio.ObjectInfo
var mcListMock func(ctx context.Context, opts mc.ListOptions) <-chan *mc.ClientContent
var mcRemoveMock func(ctx context.Context, isIncomplete, isRemoveBucket, isBypass bool, contentCh <-chan *mc.ClientContent) <-chan *probe.Error
// mock function of listObjects() needed for list objects
func (ac minioClientMock) listObjects(ctx context.Context, bucket string, opts minio.ListObjectsOptions) <-chan minio.ObjectInfo {
return minioListObjectsMock(ctx, bucket, opts)
}
// implements mc.S3Client.List()
func (c s3ClientMock) list(ctx context.Context, opts mc.ListOptions) <-chan *mc.ClientContent {
return mcListMock(ctx, opts)
}
// implements mc.S3Client.Remove()
func (c s3ClientMock) remove(ctx context.Context, isIncomplete, isRemoveBucket, isBypass bool, contentCh <-chan *mc.ClientContent) <-chan *probe.Error {
return mcRemoveMock(ctx, isIncomplete, isRemoveBucket, isBypass, contentCh)
}
func Test_listObjects(t *testing.T) {
ctx := context.Background()
t1 := time.Now()
@@ -160,3 +173,168 @@ func Test_listObjects(t *testing.T) {
})
}
}
func Test_deleteObjects(t *testing.T) {
ctx := context.Background()
client := s3ClientMock{}
type args struct {
bucket string
path string
versionID string
recursive bool
listFunc func(ctx context.Context, opts mc.ListOptions) <-chan *mc.ClientContent
removeFunc func(ctx context.Context, isIncomplete, isRemoveBucket, isBypass bool, contentCh <-chan *mc.ClientContent) <-chan *probe.Error
}
tests := []struct {
test string
args args
wantError error
}{
{
test: "Remove single object",
args: args{
path: "obj.txt",
versionID: "",
recursive: false,
removeFunc: func(ctx context.Context, isIncomplete, isRemoveBucket, isBypass bool, contentCh <-chan *mc.ClientContent) <-chan *probe.Error {
errorCh := make(chan *probe.Error)
go func() {
defer close(errorCh)
for {
return
}
}()
return errorCh
},
},
wantError: nil,
},
{
test: "Error on Remove single object",
args: args{
path: "obj.txt",
versionID: "",
recursive: false,
removeFunc: func(ctx context.Context, isIncomplete, isRemoveBucket, isBypass bool, contentCh <-chan *mc.ClientContent) <-chan *probe.Error {
errorCh := make(chan *probe.Error)
go func() {
defer close(errorCh)
for {
errorCh <- probe.NewError(errors.New("probe error"))
return
}
}()
return errorCh
},
},
wantError: errors.New("probe error"),
},
{
test: "Remove multiple objects",
args: args{
path: "path/",
versionID: "",
recursive: true,
removeFunc: func(ctx context.Context, isIncomplete, isRemoveBucket, isBypass bool, contentCh <-chan *mc.ClientContent) <-chan *probe.Error {
errorCh := make(chan *probe.Error)
go func() {
defer close(errorCh)
for {
return
}
}()
return errorCh
},
listFunc: func(ctx context.Context, opts mc.ListOptions) <-chan *mc.ClientContent {
ch := make(chan *mc.ClientContent)
go func() {
defer close(ch)
for {
ch <- &mc.ClientContent{}
return
}
}()
return ch
},
},
wantError: nil,
},
{
// Description handle error when error happens on list function
// while deleting multiple objects
test: "Error on Remove multiple objects 1",
args: args{
path: "path/",
versionID: "",
recursive: true,
removeFunc: func(ctx context.Context, isIncomplete, isRemoveBucket, isBypass bool, contentCh <-chan *mc.ClientContent) <-chan *probe.Error {
errorCh := make(chan *probe.Error)
go func() {
defer close(errorCh)
for {
return
}
}()
return errorCh
},
listFunc: func(ctx context.Context, opts mc.ListOptions) <-chan *mc.ClientContent {
ch := make(chan *mc.ClientContent)
go func() {
defer close(ch)
for {
ch <- &mc.ClientContent{Err: probe.NewError(errors.New("probe error"))}
return
}
}()
return ch
},
},
wantError: errors.New("probe error"),
},
{
// Description handle error when error happens on remove function
// while deleting multiple objects
test: "Error on Remove multiple objects 2",
args: args{
path: "path/",
versionID: "",
recursive: true,
removeFunc: func(ctx context.Context, isIncomplete, isRemoveBucket, isBypass bool, contentCh <-chan *mc.ClientContent) <-chan *probe.Error {
errorCh := make(chan *probe.Error)
go func() {
defer close(errorCh)
for {
errorCh <- probe.NewError(errors.New("probe error"))
return
}
}()
return errorCh
},
listFunc: func(ctx context.Context, opts mc.ListOptions) <-chan *mc.ClientContent {
ch := make(chan *mc.ClientContent)
go func() {
defer close(ch)
for {
ch <- &mc.ClientContent{}
return
}
}()
return ch
},
},
wantError: errors.New("probe error"),
},
}
for _, tt := range tests {
t.Run(tt.test, func(t *testing.T) {
mcListMock = tt.args.listFunc
mcRemoveMock = tt.args.removeFunc
err := deleteObjects(ctx, client, tt.args.bucket, tt.args.path, tt.args.versionID, tt.args.recursive)
if !reflect.DeepEqual(err, tt.wantError) {
t.Errorf("deleteObjects() error: %v, wantErr: %v", err, tt.wantError)
return
}
})
}
}

View File

@@ -185,7 +185,7 @@ func newWebSocketAdminClient(conn *websocket.Conn, autClaims *models.Principal)
func newWebSocketS3Client(conn *websocket.Conn, claims *models.Principal, bucketName string) (*wsS3Client, error) {
// Only start Websocket Interaction after user has been
// authenticated with MinIO
s3Client, err := newS3BucketClient(claims, bucketName)
s3Client, err := newS3BucketClient(claims, bucketName, "")
if err != nil {
log.Println("error creating S3Client:", err)
conn.WriteMessage(websocket.CloseMessage, websocket.FormatCloseMessage(websocket.CloseNormalClosure, ""))