forked from TrueCloudLab/frostfs-s3-gw
Refactoring api/layer
- don't require token.BearerToken - simplify method layer.objectPut - add method to check that object exists - add method to fetch Owner (from context or sdk client) Signed-off-by: Evgeniy Kulikov <kim@nspcc.ru>
This commit is contained in:
parent
31d3e55489
commit
f4263d50a7
3 changed files with 47 additions and 32 deletions
|
@ -5,10 +5,8 @@ import (
|
||||||
"strconv"
|
"strconv"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
sdk "github.com/nspcc-dev/cdn-neofs-sdk"
|
|
||||||
"github.com/nspcc-dev/neofs-api-go/pkg/container"
|
"github.com/nspcc-dev/neofs-api-go/pkg/container"
|
||||||
"github.com/nspcc-dev/neofs-api-go/pkg/owner"
|
"github.com/nspcc-dev/neofs-api-go/pkg/owner"
|
||||||
"github.com/nspcc-dev/neofs-api-go/pkg/token"
|
|
||||||
"github.com/nspcc-dev/neofs-s3-gate/api"
|
"github.com/nspcc-dev/neofs-s3-gate/api"
|
||||||
"go.uber.org/zap"
|
"go.uber.org/zap"
|
||||||
)
|
)
|
||||||
|
@ -79,19 +77,12 @@ func (n *layer) containerInfo(ctx context.Context, cid *container.ID) (*BucketIn
|
||||||
func (n *layer) containerList(ctx context.Context) ([]*BucketInfo, error) {
|
func (n *layer) containerList(ctx context.Context) ([]*BucketInfo, error) {
|
||||||
var (
|
var (
|
||||||
err error
|
err error
|
||||||
tkn *token.BearerToken
|
own = n.Owner(ctx)
|
||||||
|
res []*container.ID
|
||||||
rid = api.GetRequestID(ctx)
|
rid = api.GetRequestID(ctx)
|
||||||
)
|
)
|
||||||
|
|
||||||
if tkn, err = sdk.BearerToken(ctx); err != nil {
|
if res, err = n.cli.Container().List(ctx, own); err != nil {
|
||||||
n.log.Error("could not receive bearer token",
|
|
||||||
zap.String("request_id", rid),
|
|
||||||
zap.Error(err))
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
res, err := n.cli.Container().List(ctx, tkn.Issuer())
|
|
||||||
if err != nil {
|
|
||||||
n.log.Error("could not fetch container",
|
n.log.Error("could not fetch container",
|
||||||
zap.String("request_id", rid),
|
zap.String("request_id", rid),
|
||||||
zap.Error(err))
|
zap.Error(err))
|
||||||
|
|
|
@ -10,7 +10,9 @@ import (
|
||||||
sdk "github.com/nspcc-dev/cdn-neofs-sdk"
|
sdk "github.com/nspcc-dev/cdn-neofs-sdk"
|
||||||
"github.com/nspcc-dev/cdn-neofs-sdk/creds/neofs"
|
"github.com/nspcc-dev/cdn-neofs-sdk/creds/neofs"
|
||||||
"github.com/nspcc-dev/cdn-neofs-sdk/pool"
|
"github.com/nspcc-dev/cdn-neofs-sdk/pool"
|
||||||
|
"github.com/nspcc-dev/neofs-api-go/pkg/container"
|
||||||
"github.com/nspcc-dev/neofs-api-go/pkg/object"
|
"github.com/nspcc-dev/neofs-api-go/pkg/object"
|
||||||
|
"github.com/nspcc-dev/neofs-api-go/pkg/owner"
|
||||||
"github.com/nspcc-dev/neofs-s3-gate/api"
|
"github.com/nspcc-dev/neofs-s3-gate/api"
|
||||||
"github.com/pkg/errors"
|
"github.com/pkg/errors"
|
||||||
"go.uber.org/zap"
|
"go.uber.org/zap"
|
||||||
|
@ -79,6 +81,11 @@ type (
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
ErrObjectExists = errors.New("object exists")
|
||||||
|
ErrObjectNotExists = errors.New("object not exists")
|
||||||
|
)
|
||||||
|
|
||||||
// NewGatewayLayer creates instance of layer. It checks credentials
|
// NewGatewayLayer creates instance of layer. It checks credentials
|
||||||
// and establishes gRPC connection with node.
|
// and establishes gRPC connection with node.
|
||||||
func NewLayer(log *zap.Logger, cli sdk.Client) Client {
|
func NewLayer(log *zap.Logger, cli sdk.Client) Client {
|
||||||
|
@ -88,6 +95,15 @@ func NewLayer(log *zap.Logger, cli sdk.Client) Client {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Owner returns owner id from BearerToken (context) or from client owner.
|
||||||
|
func (n *layer) Owner(ctx context.Context) *owner.ID {
|
||||||
|
if tkn, err := sdk.BearerToken(ctx); err != nil && tkn != nil {
|
||||||
|
return tkn.Issuer()
|
||||||
|
}
|
||||||
|
|
||||||
|
return n.cli.Owner()
|
||||||
|
}
|
||||||
|
|
||||||
// Get NeoFS Object by refs.Address (should be used by auth.Center)
|
// Get NeoFS Object by refs.Address (should be used by auth.Center)
|
||||||
func (n *layer) Get(ctx context.Context, address *object.Address) (*object.Object, error) {
|
func (n *layer) Get(ctx context.Context, address *object.Address) (*object.Object, error) {
|
||||||
return n.cli.Object().Get(ctx, address)
|
return n.cli.Object().Get(ctx, address)
|
||||||
|
@ -245,6 +261,20 @@ func (n *layer) GetObject(ctx context.Context, p *GetObjectParams) error {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (n *layer) checkObject(ctx context.Context, cid *container.ID, filename string) error {
|
||||||
|
var err error
|
||||||
|
|
||||||
|
if _, err = n.objectFindID(ctx, &findParams{cid: cid, val: filename}); err == nil {
|
||||||
|
return ErrObjectExists
|
||||||
|
} else if state, ok := status.FromError(err); !ok || state == nil {
|
||||||
|
return err
|
||||||
|
} else if state.Code() == codes.NotFound {
|
||||||
|
return ErrObjectNotExists
|
||||||
|
}
|
||||||
|
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
// GetObjectInfo returns meta information about the object.
|
// GetObjectInfo returns meta information about the object.
|
||||||
func (n *layer) GetObjectInfo(ctx context.Context, bucketName, filename string) (*ObjectInfo, error) {
|
func (n *layer) GetObjectInfo(ctx context.Context, bucketName, filename string) (*ObjectInfo, error) {
|
||||||
var (
|
var (
|
||||||
|
@ -255,8 +285,10 @@ func (n *layer) GetObjectInfo(ctx context.Context, bucketName, filename string)
|
||||||
)
|
)
|
||||||
|
|
||||||
if bkt, err = n.GetBucketInfo(ctx, bucketName); err != nil {
|
if bkt, err = n.GetBucketInfo(ctx, bucketName); err != nil {
|
||||||
|
n.log.Error("could not fetch bucket info", zap.Error(err))
|
||||||
return nil, err
|
return nil, err
|
||||||
} else if oid, err = n.objectFindID(ctx, &findParams{cid: bkt.CID, val: filename}); err != nil {
|
} else if oid, err = n.objectFindID(ctx, &findParams{cid: bkt.CID, val: filename}); err != nil {
|
||||||
|
n.log.Error("could not find object id", zap.Error(err))
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -265,6 +297,7 @@ func (n *layer) GetObjectInfo(ctx context.Context, bucketName, filename string)
|
||||||
addr.SetContainerID(bkt.CID)
|
addr.SetContainerID(bkt.CID)
|
||||||
|
|
||||||
if meta, err = n.objectHead(ctx, addr); err != nil {
|
if meta, err = n.objectHead(ctx, addr); err != nil {
|
||||||
|
n.log.Error("could not fetch object head", zap.Error(err))
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -2,10 +2,8 @@ package layer
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bufio"
|
"bufio"
|
||||||
"bytes"
|
|
||||||
"context"
|
"context"
|
||||||
"io"
|
"io"
|
||||||
"net/http"
|
|
||||||
"net/url"
|
"net/url"
|
||||||
"strconv"
|
"strconv"
|
||||||
"time"
|
"time"
|
||||||
|
@ -13,7 +11,6 @@ import (
|
||||||
sdk "github.com/nspcc-dev/cdn-neofs-sdk"
|
sdk "github.com/nspcc-dev/cdn-neofs-sdk"
|
||||||
"github.com/nspcc-dev/neofs-api-go/pkg/container"
|
"github.com/nspcc-dev/neofs-api-go/pkg/container"
|
||||||
"github.com/nspcc-dev/neofs-api-go/pkg/object"
|
"github.com/nspcc-dev/neofs-api-go/pkg/object"
|
||||||
"github.com/nspcc-dev/neofs-api-go/pkg/token"
|
|
||||||
"github.com/nspcc-dev/neofs-s3-gate/api"
|
"github.com/nspcc-dev/neofs-s3-gate/api"
|
||||||
"github.com/pkg/errors"
|
"github.com/pkg/errors"
|
||||||
"google.golang.org/grpc/codes"
|
"google.golang.org/grpc/codes"
|
||||||
|
@ -41,15 +38,11 @@ func (n *layer) objectSearch(ctx context.Context, p *findParams) ([]*object.ID,
|
||||||
sdk.SearchRootObjects(),
|
sdk.SearchRootObjects(),
|
||||||
}
|
}
|
||||||
|
|
||||||
filename, err := url.QueryUnescape(p.val)
|
if filename, err := url.QueryUnescape(p.val); err != nil {
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
} else if filename != "" {
|
||||||
|
|
||||||
if p.val != "" {
|
|
||||||
opts = append(opts, sdk.SearchByFilename(filename))
|
opts = append(opts, sdk.SearchByFilename(filename))
|
||||||
}
|
}
|
||||||
|
|
||||||
return n.cli.Object().Search(ctx, p.cid, opts...)
|
return n.cli.Object().Search(ctx, p.cid, opts...)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -88,18 +81,18 @@ func (n *layer) objectPut(ctx context.Context, p *PutObjectParams) (*ObjectInfo,
|
||||||
err error
|
err error
|
||||||
obj string
|
obj string
|
||||||
bkt *BucketInfo
|
bkt *BucketInfo
|
||||||
brt *token.BearerToken
|
own = n.Owner(ctx)
|
||||||
|
|
||||||
address *object.Address
|
address *object.Address
|
||||||
)
|
)
|
||||||
|
|
||||||
if brt, err = sdk.BearerToken(ctx); err != nil {
|
if obj, err = url.QueryUnescape(p.Object); err != nil {
|
||||||
return nil, err
|
|
||||||
} else if obj, err = url.QueryUnescape(p.Object); err != nil {
|
|
||||||
return nil, err
|
return nil, err
|
||||||
} else if bkt, err = n.GetBucketInfo(ctx, p.Bucket); err != nil {
|
} else if bkt, err = n.GetBucketInfo(ctx, p.Bucket); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
} else if _, err = n.objectFindID(ctx, &findParams{cid: bkt.CID, val: p.Object}); err == nil {
|
} else if err = n.checkObject(ctx, bkt.CID, p.Object); err != nil && err != ErrObjectNotExists {
|
||||||
|
return nil, err
|
||||||
|
} else if err == ErrObjectExists {
|
||||||
return nil, &api.ObjectAlreadyExists{
|
return nil, &api.ObjectAlreadyExists{
|
||||||
Bucket: p.Bucket,
|
Bucket: p.Bucket,
|
||||||
Object: p.Object,
|
Object: p.Object,
|
||||||
|
@ -128,14 +121,12 @@ func (n *layer) objectPut(ctx context.Context, p *PutObjectParams) (*ObjectInfo,
|
||||||
attributes = append(attributes, ua)
|
attributes = append(attributes, ua)
|
||||||
}
|
}
|
||||||
|
|
||||||
b := new(bytes.Buffer)
|
|
||||||
|
|
||||||
raw := object.NewRaw()
|
raw := object.NewRaw()
|
||||||
raw.SetOwnerID(brt.Issuer())
|
raw.SetOwnerID(own)
|
||||||
raw.SetContainerID(bkt.CID)
|
raw.SetContainerID(bkt.CID)
|
||||||
raw.SetAttributes(attributes...)
|
raw.SetAttributes(attributes...)
|
||||||
|
|
||||||
r := io.TeeReader(p.Reader, b)
|
r := newDetector(p.Reader)
|
||||||
if address, err = n.cli.Object().Put(ctx, raw.Object(), sdk.WithPutReader(r)); err != nil {
|
if address, err = n.cli.Object().Put(ctx, raw.Object(), sdk.WithPutReader(r)); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
@ -143,13 +134,13 @@ func (n *layer) objectPut(ctx context.Context, p *PutObjectParams) (*ObjectInfo,
|
||||||
return &ObjectInfo{
|
return &ObjectInfo{
|
||||||
id: address.ObjectID(),
|
id: address.ObjectID(),
|
||||||
|
|
||||||
|
Owner: own,
|
||||||
Bucket: p.Bucket,
|
Bucket: p.Bucket,
|
||||||
Name: p.Object,
|
Name: p.Object,
|
||||||
Size: p.Size,
|
Size: p.Size,
|
||||||
Created: time.Now(),
|
Created: time.Now(),
|
||||||
ContentType: http.DetectContentType(b.Bytes()),
|
|
||||||
Owner: brt.Issuer(),
|
|
||||||
Headers: p.Header,
|
Headers: p.Header,
|
||||||
|
ContentType: r.contentType,
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue