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:
Evgeniy Kulikov 2020-11-27 15:36:15 +03:00
parent 31d3e55489
commit f4263d50a7
3 changed files with 47 additions and 32 deletions

View file

@ -5,10 +5,8 @@ import (
"strconv"
"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/owner"
"github.com/nspcc-dev/neofs-api-go/pkg/token"
"github.com/nspcc-dev/neofs-s3-gate/api"
"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) {
var (
err error
tkn *token.BearerToken
own = n.Owner(ctx)
res []*container.ID
rid = api.GetRequestID(ctx)
)
if tkn, err = sdk.BearerToken(ctx); 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 {
if res, err = n.cli.Container().List(ctx, own); err != nil {
n.log.Error("could not fetch container",
zap.String("request_id", rid),
zap.Error(err))

View file

@ -10,7 +10,9 @@ import (
sdk "github.com/nspcc-dev/cdn-neofs-sdk"
"github.com/nspcc-dev/cdn-neofs-sdk/creds/neofs"
"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/owner"
"github.com/nspcc-dev/neofs-s3-gate/api"
"github.com/pkg/errors"
"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
// and establishes gRPC connection with node.
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)
func (n *layer) Get(ctx context.Context, address *object.Address) (*object.Object, error) {
return n.cli.Object().Get(ctx, address)
@ -245,6 +261,20 @@ func (n *layer) GetObject(ctx context.Context, p *GetObjectParams) error {
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.
func (n *layer) GetObjectInfo(ctx context.Context, bucketName, filename string) (*ObjectInfo, error) {
var (
@ -255,8 +285,10 @@ func (n *layer) GetObjectInfo(ctx context.Context, bucketName, filename string)
)
if bkt, err = n.GetBucketInfo(ctx, bucketName); err != nil {
n.log.Error("could not fetch bucket info", zap.Error(err))
return nil, err
} 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
}
@ -265,6 +297,7 @@ func (n *layer) GetObjectInfo(ctx context.Context, bucketName, filename string)
addr.SetContainerID(bkt.CID)
if meta, err = n.objectHead(ctx, addr); err != nil {
n.log.Error("could not fetch object head", zap.Error(err))
return nil, err
}

View file

@ -2,10 +2,8 @@ package layer
import (
"bufio"
"bytes"
"context"
"io"
"net/http"
"net/url"
"strconv"
"time"
@ -13,7 +11,6 @@ import (
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/object"
"github.com/nspcc-dev/neofs-api-go/pkg/token"
"github.com/nspcc-dev/neofs-s3-gate/api"
"github.com/pkg/errors"
"google.golang.org/grpc/codes"
@ -41,15 +38,11 @@ func (n *layer) objectSearch(ctx context.Context, p *findParams) ([]*object.ID,
sdk.SearchRootObjects(),
}
filename, err := url.QueryUnescape(p.val)
if err != nil {
if filename, err := url.QueryUnescape(p.val); err != nil {
return nil, err
}
if p.val != "" {
} else if filename != "" {
opts = append(opts, sdk.SearchByFilename(filename))
}
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
obj string
bkt *BucketInfo
brt *token.BearerToken
own = n.Owner(ctx)
address *object.Address
)
if brt, err = sdk.BearerToken(ctx); err != nil {
return nil, err
} else if obj, err = url.QueryUnescape(p.Object); err != nil {
if obj, err = url.QueryUnescape(p.Object); err != nil {
return nil, err
} else if bkt, err = n.GetBucketInfo(ctx, p.Bucket); err != nil {
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{
Bucket: p.Bucket,
Object: p.Object,
@ -128,14 +121,12 @@ func (n *layer) objectPut(ctx context.Context, p *PutObjectParams) (*ObjectInfo,
attributes = append(attributes, ua)
}
b := new(bytes.Buffer)
raw := object.NewRaw()
raw.SetOwnerID(brt.Issuer())
raw.SetOwnerID(own)
raw.SetContainerID(bkt.CID)
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 {
return nil, err
}
@ -143,13 +134,13 @@ func (n *layer) objectPut(ctx context.Context, p *PutObjectParams) (*ObjectInfo,
return &ObjectInfo{
id: address.ObjectID(),
Owner: own,
Bucket: p.Bucket,
Name: p.Object,
Size: p.Size,
Created: time.Now(),
ContentType: http.DetectContentType(b.Bytes()),
Owner: brt.Issuer(),
Headers: p.Header,
ContentType: r.contentType,
}, nil
}