2020-07-03 13:52:04 +00:00
|
|
|
package layer
|
|
|
|
|
|
|
|
import (
|
2020-10-19 01:04:37 +00:00
|
|
|
"bytes"
|
2020-07-03 13:52:04 +00:00
|
|
|
"context"
|
|
|
|
"io"
|
2020-10-19 01:04:37 +00:00
|
|
|
"net/http"
|
2020-07-03 13:52:04 +00:00
|
|
|
"time"
|
|
|
|
|
2020-10-19 01:04:37 +00:00
|
|
|
"github.com/nspcc-dev/neofs-api-go/pkg/client"
|
|
|
|
"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-api-go/pkg/token"
|
|
|
|
"github.com/nspcc-dev/neofs-s3-gate/api"
|
|
|
|
"github.com/nspcc-dev/neofs-s3-gate/auth"
|
2020-07-23 08:05:50 +00:00
|
|
|
"github.com/pkg/errors"
|
2020-08-22 02:39:12 +00:00
|
|
|
"google.golang.org/grpc/codes"
|
|
|
|
"google.golang.org/grpc/status"
|
2020-07-03 13:52:04 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type (
|
2020-10-19 01:04:37 +00:00
|
|
|
findParams struct {
|
|
|
|
key string
|
|
|
|
cid *container.ID
|
2020-07-03 13:52:04 +00:00
|
|
|
}
|
2020-08-03 11:48:33 +00:00
|
|
|
|
2020-10-19 01:04:37 +00:00
|
|
|
getParams struct {
|
|
|
|
io.Writer
|
2020-08-03 11:48:33 +00:00
|
|
|
|
2020-10-19 01:04:37 +00:00
|
|
|
addr *object.Address
|
2020-08-03 11:48:33 +00:00
|
|
|
|
2020-10-19 01:04:37 +00:00
|
|
|
offset int64
|
2020-07-03 13:52:04 +00:00
|
|
|
length int64
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2020-10-19 01:04:37 +00:00
|
|
|
func (n *layer) prepareClient(ctx context.Context) (*client.Client, *token.SessionToken, error) {
|
|
|
|
conn, err := n.cli.Connection(ctx)
|
2020-07-03 13:52:04 +00:00
|
|
|
if err != nil {
|
2020-10-19 01:04:37 +00:00
|
|
|
return nil, nil, err
|
2020-07-03 13:52:04 +00:00
|
|
|
}
|
|
|
|
|
2020-10-19 01:04:37 +00:00
|
|
|
tkn, err := n.cli.Token(ctx, conn)
|
2020-07-03 13:52:04 +00:00
|
|
|
if err != nil {
|
2020-10-19 01:04:37 +00:00
|
|
|
return nil, nil, err
|
2020-07-03 13:52:04 +00:00
|
|
|
}
|
|
|
|
|
2020-10-19 01:04:37 +00:00
|
|
|
cli, err := client.New(n.key, client.WithGRPCConnection(conn))
|
2020-07-03 13:52:04 +00:00
|
|
|
if err != nil {
|
2020-10-19 01:04:37 +00:00
|
|
|
return nil, nil, err
|
2020-07-03 13:52:04 +00:00
|
|
|
}
|
|
|
|
|
2020-10-19 01:04:37 +00:00
|
|
|
return cli, tkn, nil
|
2020-07-03 13:52:04 +00:00
|
|
|
}
|
|
|
|
|
2020-10-19 01:04:37 +00:00
|
|
|
// objectSearch returns all available objects by search params.
|
|
|
|
func (n *layer) objectSearch(ctx context.Context, p *findParams) ([]*object.ID, error) {
|
|
|
|
cli, tkn, err := n.prepareClient(ctx)
|
2020-07-03 13:52:04 +00:00
|
|
|
if err != nil {
|
2020-08-20 19:33:59 +00:00
|
|
|
return nil, err
|
2020-07-03 13:52:04 +00:00
|
|
|
}
|
|
|
|
|
2020-10-19 01:04:37 +00:00
|
|
|
filter := object.NewSearchFilters()
|
|
|
|
filter.AddNonLeafFilter()
|
2020-07-03 13:52:04 +00:00
|
|
|
|
2020-10-19 01:04:37 +00:00
|
|
|
sop := new(client.SearchObjectParams)
|
2020-07-03 13:52:04 +00:00
|
|
|
|
2020-10-19 01:04:37 +00:00
|
|
|
if p.cid != nil {
|
|
|
|
filter.AddFilter(object.HdrSysNameCID, p.cid.String(), object.MatchStringEqual)
|
|
|
|
sop.WithContainerID(p.cid)
|
2020-07-03 13:52:04 +00:00
|
|
|
}
|
|
|
|
|
2020-10-19 01:04:37 +00:00
|
|
|
if p.key != "" {
|
|
|
|
filter.AddFilter(AWS3NameHeader, p.key, object.MatchStringEqual)
|
2020-07-03 13:52:04 +00:00
|
|
|
}
|
|
|
|
|
2020-10-19 01:04:37 +00:00
|
|
|
sop.WithSearchFilters(filter)
|
2020-08-20 19:33:59 +00:00
|
|
|
|
2020-10-19 01:04:37 +00:00
|
|
|
return cli.SearchObject(ctx, sop, client.WithSession(tkn))
|
2020-08-20 19:33:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// objectFindID returns object id (uuid) based on it's nice name in s3. If
|
|
|
|
// nice name is uuid compatible, then function returns it.
|
2020-10-19 01:04:37 +00:00
|
|
|
func (n *layer) objectFindID(ctx context.Context, p *findParams) (*object.ID, error) {
|
|
|
|
if result, err := n.objectSearch(ctx, p); err != nil {
|
|
|
|
return nil, err
|
2020-08-20 19:33:59 +00:00
|
|
|
} else if ln := len(result); ln == 0 {
|
2020-10-19 01:04:37 +00:00
|
|
|
return nil, status.Error(codes.NotFound, "object not found")
|
2020-08-20 19:33:59 +00:00
|
|
|
} else if ln == 1 {
|
|
|
|
return result[0], nil
|
2020-07-03 13:52:04 +00:00
|
|
|
}
|
2020-08-20 19:33:59 +00:00
|
|
|
|
2020-10-19 01:04:37 +00:00
|
|
|
return nil, errors.New("several objects with the same name found")
|
2020-07-03 13:52:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// objectHead returns all object's headers.
|
2020-10-19 01:04:37 +00:00
|
|
|
func (n *layer) objectHead(ctx context.Context, addr *object.Address) (*object.Object, error) {
|
|
|
|
cli, tkn, err := n.prepareClient(ctx)
|
2020-07-03 13:52:04 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-10-19 01:04:37 +00:00
|
|
|
ohp := new(client.ObjectHeaderParams)
|
|
|
|
ohp.WithAddress(addr)
|
|
|
|
ohp.WithAllFields()
|
|
|
|
ohp.WithMainFields()
|
2020-07-03 13:52:04 +00:00
|
|
|
|
2020-10-19 01:04:37 +00:00
|
|
|
return cli.GetObjectHeader(ctx, ohp, client.WithSession(tkn))
|
2020-08-05 13:47:09 +00:00
|
|
|
}
|
|
|
|
|
2020-07-03 13:52:04 +00:00
|
|
|
// objectGet and write it into provided io.Reader.
|
2020-10-19 01:04:37 +00:00
|
|
|
func (n *layer) objectGet(ctx context.Context, p *getParams) (*object.Object, error) {
|
|
|
|
cli, tkn, err := n.prepareClient(ctx)
|
2020-07-03 13:52:04 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-10-19 01:04:37 +00:00
|
|
|
// prepare length/offset writer
|
|
|
|
writer := newWriter(p.Writer, p.offset, p.length)
|
2020-07-03 13:52:04 +00:00
|
|
|
|
2020-10-19 01:04:37 +00:00
|
|
|
gop := new(client.GetObjectParams)
|
|
|
|
gop.WithPayloadWriter(writer)
|
2020-07-03 13:52:04 +00:00
|
|
|
|
2020-10-19 01:04:37 +00:00
|
|
|
return cli.GetObject(ctx, gop, client.WithSession(tkn))
|
2020-07-03 13:52:04 +00:00
|
|
|
}
|
|
|
|
|
2020-10-19 01:04:37 +00:00
|
|
|
// objectPut into NeoFS, took payload from io.Reader.
|
|
|
|
func (n *layer) objectPut(ctx context.Context, p *PutObjectParams) (*ObjectInfo, error) {
|
|
|
|
var (
|
|
|
|
err error
|
|
|
|
own *owner.ID
|
|
|
|
brt *token.BearerToken
|
|
|
|
cid = container.NewID()
|
|
|
|
)
|
2020-07-03 13:52:04 +00:00
|
|
|
|
2020-10-19 01:04:37 +00:00
|
|
|
if brt, err = auth.GetBearerToken(ctx); err != nil {
|
2020-07-03 13:52:04 +00:00
|
|
|
return nil, err
|
2020-10-19 01:04:37 +00:00
|
|
|
} else if own, err = GetOwnerID(brt); err != nil {
|
2020-07-03 13:52:04 +00:00
|
|
|
return nil, err
|
2020-10-19 01:04:37 +00:00
|
|
|
} else if err = cid.Parse(p.Bucket); err != nil {
|
2020-07-03 13:52:04 +00:00
|
|
|
return nil, err
|
2020-10-19 01:04:37 +00:00
|
|
|
} else if _, err = n.objectFindID(ctx, &findParams{cid: cid, key: p.Object}); err == nil {
|
|
|
|
return nil, &api.ObjectAlreadyExists{
|
|
|
|
Bucket: p.Bucket,
|
|
|
|
Object: p.Object,
|
2020-07-03 13:52:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-19 01:04:37 +00:00
|
|
|
cli, tkn, err := n.prepareClient(ctx)
|
2020-07-03 13:52:04 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-10-19 01:04:37 +00:00
|
|
|
attributes := make([]*object.Attribute, 0, len(p.Header)+1)
|
2020-07-03 13:52:04 +00:00
|
|
|
|
2020-10-19 01:04:37 +00:00
|
|
|
filename := object.NewAttribute()
|
|
|
|
filename.SetKey(AWS3NameHeader)
|
|
|
|
filename.SetValue(p.Object)
|
2020-07-03 13:52:04 +00:00
|
|
|
|
2020-10-19 01:04:37 +00:00
|
|
|
attributes = append(attributes, filename)
|
2020-07-03 13:52:04 +00:00
|
|
|
|
2020-10-19 01:04:37 +00:00
|
|
|
for k, v := range p.Header {
|
|
|
|
ua := object.NewAttribute()
|
|
|
|
ua.SetKey(k)
|
|
|
|
ua.SetValue(v)
|
2020-07-03 13:52:04 +00:00
|
|
|
|
2020-10-19 01:04:37 +00:00
|
|
|
attributes = append(attributes, ua)
|
2020-07-03 13:52:04 +00:00
|
|
|
}
|
|
|
|
|
2020-10-19 01:04:37 +00:00
|
|
|
b := new(bytes.Buffer)
|
|
|
|
r := io.TeeReader(p.Reader, b)
|
2020-07-03 13:52:04 +00:00
|
|
|
|
2020-10-19 01:04:37 +00:00
|
|
|
raw := object.NewRaw()
|
|
|
|
raw.SetOwnerID(own)
|
|
|
|
raw.SetContainerID(cid)
|
|
|
|
raw.SetAttributes(attributes...)
|
2020-07-03 13:52:04 +00:00
|
|
|
|
2020-10-19 01:04:37 +00:00
|
|
|
pop := new(client.PutObjectParams)
|
|
|
|
pop.WithPayloadReader(r)
|
|
|
|
pop.WithObject(raw.Object())
|
2020-07-03 13:52:04 +00:00
|
|
|
|
2020-10-19 01:04:37 +00:00
|
|
|
if _, err = cli.PutObject(ctx, pop, client.WithSession(tkn)); err != nil {
|
2020-07-03 13:52:04 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-10-19 01:04:37 +00:00
|
|
|
return &ObjectInfo{
|
|
|
|
Bucket: p.Bucket,
|
|
|
|
Name: p.Object,
|
|
|
|
Size: p.Size,
|
|
|
|
Created: time.Now(),
|
|
|
|
ContentType: http.DetectContentType(b.Bytes()),
|
|
|
|
Owner: own,
|
|
|
|
Headers: p.Header,
|
|
|
|
}, nil
|
2020-07-03 13:52:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// objectDelete puts tombstone object into neofs.
|
2020-10-19 01:04:37 +00:00
|
|
|
func (n *layer) objectDelete(ctx context.Context, address *object.Address) error {
|
|
|
|
cli, tkn, err := n.prepareClient(ctx)
|
2020-08-03 11:48:33 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-10-19 01:04:37 +00:00
|
|
|
dob := new(client.DeleteObjectParams)
|
|
|
|
dob.WithAddress(address)
|
2020-07-03 13:52:04 +00:00
|
|
|
|
2020-10-19 01:04:37 +00:00
|
|
|
return cli.DeleteObject(ctx, dob, client.WithSession(tkn))
|
2020-07-03 13:52:04 +00:00
|
|
|
}
|