Skip to content

Commit

Permalink
Update object endpoints (#1539)
Browse files Browse the repository at this point in the history
If we move `/objects` to `/object` we can get rid of `GET
/listobjects/*prefix"`.
  • Loading branch information
ChrisSchinnerl committed Sep 18, 2024
2 parents a037bfb + bd26046 commit 5abf932
Show file tree
Hide file tree
Showing 20 changed files with 268 additions and 230 deletions.
20 changes: 8 additions & 12 deletions api/object.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,13 +97,19 @@ type (
Metadata ObjectUserMetadata
}

// ObjectsListResponse is the response type for the /bus/objects/list endpoint.
ObjectsListResponse struct {
// ObjectsResponse is the response type for the /bus/objects endpoint.
ObjectsResponse struct {
HasMore bool `json:"hasMore"`
NextMarker string `json:"nextMarker"`
Objects []ObjectMetadata `json:"objects"`
}

// ObjectsRemoveRequest is the request type for the /bus/objects/remove endpoint.
ObjectsRemoveRequest struct {
Bucket string `json:"bucket"`
Prefix string `json:"prefix"`
}

// ObjectsRenameRequest is the request type for the /bus/objects/rename endpoint.
ObjectsRenameRequest struct {
Bucket string `json:"bucket"`
Expand Down Expand Up @@ -190,10 +196,6 @@ type (
Metadata ObjectUserMetadata `json:"metadata"`
}

DeleteObjectOptions struct {
Batch bool
}

HeadObjectOptions struct {
Range *DownloadRange
}
Expand Down Expand Up @@ -281,12 +283,6 @@ func (opts DownloadObjectOptions) ApplyHeaders(h http.Header) {
}
}

func (opts DeleteObjectOptions) Apply(values url.Values) {
if opts.Batch {
values.Set("batch", "true")
}
}

func (opts HeadObjectOptions) Apply(values url.Values) {
}

Expand Down
2 changes: 1 addition & 1 deletion autopilot/autopilot.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ type Bus interface {
ListBuckets(ctx context.Context) ([]api.Bucket, error)

// objects
ListObjects(ctx context.Context, prefix string, opts api.ListObjectOptions) (resp api.ObjectsListResponse, err error)
Objects(ctx context.Context, prefix string, opts api.ListObjectOptions) (resp api.ObjectsResponse, err error)
RefreshHealth(ctx context.Context) error
Slab(ctx context.Context, key object.EncryptionKey) (object.Slab, error)
SlabsForMigration(ctx context.Context, healthCutoff float64, set string, limit int) ([]api.UnhealthySlab, error)
Expand Down
17 changes: 9 additions & 8 deletions bus/bus.go
Original file line number Diff line number Diff line change
Expand Up @@ -228,14 +228,14 @@ type (
DeleteHostSector(ctx context.Context, hk types.PublicKey, root types.Hash256) (int, error)

Bucket(_ context.Context, bucketName string) (api.Bucket, error)
Buckets(_ context.Context) ([]api.Bucket, error)
CreateBucket(_ context.Context, bucketName string, policy api.BucketPolicy) error
DeleteBucket(_ context.Context, bucketName string) error
ListBuckets(_ context.Context) ([]api.Bucket, error)
UpdateBucketPolicy(ctx context.Context, bucketName string, policy api.BucketPolicy) error

CopyObject(ctx context.Context, srcBucket, dstBucket, srcKey, dstKey, mimeType string, metadata api.ObjectUserMetadata) (api.ObjectMetadata, error)
ListObjects(ctx context.Context, bucketName, prefix, substring, delim, sortBy, sortDir, marker string, limit int, slabEncryptionKey object.EncryptionKey) (api.ObjectsListResponse, error)
Object(ctx context.Context, bucketName, key string) (api.Object, error)
Objects(ctx context.Context, bucketName, prefix, substring, delim, sortBy, sortDir, marker string, limit int, slabEncryptionKey object.EncryptionKey) (api.ObjectsResponse, error)
ObjectMetadata(ctx context.Context, bucketName, key string) (api.Object, error)
ObjectsStats(ctx context.Context, opts api.ObjectsStatsOpts) (api.ObjectsStatsResponse, error)
RemoveObject(ctx context.Context, bucketName, key string) error
Expand Down Expand Up @@ -453,12 +453,13 @@ func (b *Bus) Handler() http.Handler {
"POST /multipart/listuploads": b.multipartHandlerListUploadsPOST,
"POST /multipart/listparts": b.multipartHandlerListPartsPOST,

"GET /listobjects/*prefix": b.objectsHandlerGET,
"GET /objects/*key": b.objectHandlerGET,
"PUT /objects/*key": b.objectsHandlerPUT,
"DELETE /objects/*key": b.objectsHandlerDELETE,
"POST /objects/copy": b.objectsCopyHandlerPOST,
"POST /objects/rename": b.objectsRenameHandlerPOST,
"GET /object/*key": b.objectHandlerGET,
"PUT /object/*key": b.objectHandlerPUT,
"DELETE /object/*key": b.objectHandlerDELETE,
"GET /objects/*prefix": b.objectsHandlerGET,
"POST /objects/copy": b.objectsCopyHandlerPOST,
"POST /objects/remove": b.objectsRemoveHandlerPOST,
"POST /objects/rename": b.objectsRenameHandlerPOST,

"GET /params/gouging": b.paramsHandlerGougingGET,
"GET /params/upload": b.paramsHandlerUploadGET,
Expand Down
27 changes: 17 additions & 10 deletions bus/client/objects.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
// AddObject stores the provided object under the given path.
func (c *Client) AddObject(ctx context.Context, bucket, path, contractSet string, o object.Object, opts api.AddObjectOptions) (err error) {
path = api.ObjectKeyEscape(path)
err = c.c.WithContext(ctx).PUT(fmt.Sprintf("/objects/%s", path), api.AddObjectRequest{
err = c.c.WithContext(ctx).PUT(fmt.Sprintf("/object/%s", path), api.AddObjectRequest{
Bucket: bucket,
ContractSet: contractSet,
Object: o,
Expand All @@ -37,15 +37,22 @@ func (c *Client) CopyObject(ctx context.Context, srcBucket, dstBucket, srcKey, d
return
}

// DeleteObject either deletes the object at the given key or if batch=true
// deletes all objects that start with the given key.
func (c *Client) DeleteObject(ctx context.Context, bucket, key string, opts api.DeleteObjectOptions) (err error) {
// DeleteObject deletes the object with given key.
func (c *Client) DeleteObject(ctx context.Context, bucket, key string) (err error) {
values := url.Values{}
values.Set("bucket", bucket)
opts.Apply(values)

key = api.ObjectKeyEscape(key)
err = c.c.WithContext(ctx).DELETE(fmt.Sprintf("/objects/%s?"+values.Encode(), key))
err = c.c.WithContext(ctx).DELETE(fmt.Sprintf("/object/%s?"+values.Encode(), key))
return
}

// RemoveObjects removes objects with given prefix.
func (c *Client) RemoveObjects(ctx context.Context, bucket, prefix string) (err error) {
err = c.c.WithContext(ctx).POST("/objects/remove", api.ObjectsRemoveRequest{
Bucket: bucket,
Prefix: prefix,
}, nil)
return
}

Expand All @@ -58,19 +65,19 @@ func (c *Client) Object(ctx context.Context, bucket, key string, opts api.GetObj
key = api.ObjectKeyEscape(key)
key += "?" + values.Encode()

err = c.c.WithContext(ctx).GET(fmt.Sprintf("/objects/%s", key), &res)
err = c.c.WithContext(ctx).GET(fmt.Sprintf("/object/%s", key), &res)
return
}

// ListObjects lists objects in the given bucket.
func (c *Client) ListObjects(ctx context.Context, prefix string, opts api.ListObjectOptions) (resp api.ObjectsListResponse, err error) {
// Objects lists objects in the given bucket.
func (c *Client) Objects(ctx context.Context, prefix string, opts api.ListObjectOptions) (resp api.ObjectsResponse, err error) {
values := url.Values{}
opts.Apply(values)

prefix = api.ObjectKeyEscape(prefix)
prefix += "?" + values.Encode()

err = c.c.WithContext(ctx).GET(fmt.Sprintf("/listobjects/%s", prefix), &resp)
err = c.c.WithContext(ctx).GET(fmt.Sprintf("/objects/%s", prefix), &resp)
return
}

Expand Down
37 changes: 22 additions & 15 deletions bus/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ func (b *Bus) txpoolBroadcastHandler(jc jape.Context) {
}

func (b *Bus) bucketsHandlerGET(jc jape.Context) {
resp, err := b.ms.ListBuckets(jc.Request.Context())
resp, err := b.ms.Buckets(jc.Request.Context())
if jc.Check("couldn't list buckets", err) != nil {
return
}
Expand Down Expand Up @@ -1155,7 +1155,7 @@ func (b *Bus) objectsHandlerGET(jc jape.Context) {
return
}

resp, err := b.ms.ListObjects(jc.Request.Context(), bucket, jc.PathParam("prefix"), substring, delim, sortBy, sortDir, marker, limit, slabEncryptionKey)
resp, err := b.ms.Objects(jc.Request.Context(), bucket, jc.PathParam("prefix"), substring, delim, sortBy, sortDir, marker, limit, slabEncryptionKey)
if errors.Is(err, api.ErrUnsupportedDelimiter) {
jc.Error(err, http.StatusBadRequest)
return
Expand All @@ -1165,7 +1165,7 @@ func (b *Bus) objectsHandlerGET(jc jape.Context) {
jc.Encode(resp)
}

func (b *Bus) objectsHandlerPUT(jc jape.Context) {
func (b *Bus) objectHandlerPUT(jc jape.Context) {
var aor api.AddObjectRequest
if jc.Decode(&aor) != nil {
return
Expand All @@ -1191,6 +1191,23 @@ func (b *Bus) objectsCopyHandlerPOST(jc jape.Context) {
jc.Encode(om)
}

func (b *Bus) objectsRemoveHandlerPOST(jc jape.Context) {
var orr api.ObjectsRemoveRequest
if jc.Decode(&orr) != nil {
return
} else if orr.Bucket == "" {
jc.Error(api.ErrBucketMissing, http.StatusBadRequest)
return
}

if orr.Prefix == "" {
jc.Error(errors.New("prefix cannot be empty"), http.StatusBadRequest)
return
}

jc.Check("failed to remove objects", b.ms.RemoveObjects(jc.Request.Context(), orr.Bucket, orr.Prefix))
}

func (b *Bus) objectsRenameHandlerPOST(jc jape.Context) {
var orr api.ObjectsRenameRequest
if jc.Decode(&orr) != nil {
Expand Down Expand Up @@ -1222,25 +1239,15 @@ func (b *Bus) objectsRenameHandlerPOST(jc jape.Context) {
}
}

func (b *Bus) objectsHandlerDELETE(jc jape.Context) {
var batch bool
if jc.DecodeForm("batch", &batch) != nil {
return
}
func (b *Bus) objectHandlerDELETE(jc jape.Context) {
var bucket string
if jc.DecodeForm("bucket", &bucket) != nil {
return
} else if bucket == "" {
jc.Error(api.ErrBucketMissing, http.StatusBadRequest)
return
}

var err error
if batch {
err = b.ms.RemoveObjects(jc.Request.Context(), bucket, jc.PathParam("key"))
} else {
err = b.ms.RemoveObject(jc.Request.Context(), bucket, jc.PathParam("key"))
}
err := b.ms.RemoveObject(jc.Request.Context(), bucket, jc.PathParam("key"))
if errors.Is(err, api.ErrObjectNotFound) {
jc.Error(err, http.StatusNotFound)
return
Expand Down
Loading

0 comments on commit 5abf932

Please sign in to comment.