2020-07-03 13:52:04 +00:00
|
|
|
package layer
|
|
|
|
|
|
|
|
import (
|
2020-10-24 13:09:22 +00:00
|
|
|
"bufio"
|
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-10-24 13:09:22 +00:00
|
|
|
"net/url"
|
2020-10-23 00:12:37 +00:00
|
|
|
"strconv"
|
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"
|
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 {
|
2020-10-22 00:19:16 +00:00
|
|
|
val string
|
2020-10-19 01:04:37 +00:00
|
|
|
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) {
|
2020-10-24 13:09:22 +00:00
|
|
|
filename, err := url.QueryUnescape(p.val)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
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 {
|
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()
|
2020-10-23 00:12:37 +00:00
|
|
|
filter.AddRootFilter()
|
2020-07-03 13:52:04 +00:00
|
|
|
|
2020-10-19 01:04:37 +00:00
|
|
|
sop := new(client.SearchObjectParams)
|
2020-10-22 00:19:16 +00:00
|
|
|
sop.WithContainerID(p.cid)
|
2020-07-03 13:52:04 +00:00
|
|
|
|
2020-10-22 00:19:16 +00:00
|
|
|
if p.val != "" {
|
2020-10-24 13:09:22 +00:00
|
|
|
filter.AddFilter(object.AttributeFileName, filename, 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()
|
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
|
2020-10-24 13:09:22 +00:00
|
|
|
b := bufio.NewWriter(p.Writer)
|
|
|
|
w := newWriter(b, p.offset, p.length)
|
|
|
|
writer := newWriter(w, p.offset, p.length)
|
2020-07-03 13:52:04 +00:00
|
|
|
|
2020-10-19 01:04:37 +00:00
|
|
|
gop := new(client.GetObjectParams)
|
2020-10-23 00:12:37 +00:00
|
|
|
gop.WithAddress(p.addr)
|
2020-10-19 01:04:37 +00:00
|
|
|
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
|
2020-10-24 13:09:22 +00:00
|
|
|
obj string
|
2020-10-19 01:04:37 +00:00
|
|
|
own *owner.ID
|
2020-10-24 13:09:22 +00:00
|
|
|
oid *object.ID
|
2020-10-23 00:12:37 +00:00
|
|
|
bkt *BucketInfo
|
2020-10-24 13:09:22 +00:00
|
|
|
// brt *token.BearerToken
|
2020-10-19 01:04:37 +00:00
|
|
|
)
|
2020-07-03 13:52:04 +00:00
|
|
|
|
2020-10-24 13:09:22 +00:00
|
|
|
// if brt, err = auth.GetBearerToken(ctx); err != nil {
|
|
|
|
// return nil, err
|
|
|
|
// }
|
|
|
|
|
|
|
|
// else if own, err = GetOwnerID(brt); err != nil {
|
|
|
|
// return nil, err
|
|
|
|
// }
|
2020-10-22 00:19:16 +00:00
|
|
|
|
|
|
|
_ = own
|
|
|
|
|
2020-10-24 13:09:22 +00:00
|
|
|
if obj, err = url.QueryUnescape(p.Object); err != nil {
|
|
|
|
return nil, err
|
|
|
|
} else if bkt, err = n.GetBucketInfo(ctx, p.Bucket); err != nil {
|
2020-07-03 13:52:04 +00:00
|
|
|
return nil, err
|
2020-10-22 00:19:16 +00:00
|
|
|
} else if _, err = n.objectFindID(ctx, &findParams{cid: bkt.CID, val: p.Object}); err == nil {
|
2020-10-19 01:04:37 +00:00
|
|
|
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-24 13:09:22 +00:00
|
|
|
unix := strconv.FormatInt(time.Now().UTC().Unix(), 10)
|
2020-10-23 00:12:37 +00:00
|
|
|
|
2020-10-19 01:04:37 +00:00
|
|
|
filename := object.NewAttribute()
|
2020-10-23 00:12:37 +00:00
|
|
|
filename.SetKey(object.AttributeFileName)
|
2020-10-24 13:09:22 +00:00
|
|
|
filename.SetValue(obj)
|
2020-07-03 13:52:04 +00:00
|
|
|
|
2020-10-23 00:12:37 +00:00
|
|
|
createdAt := object.NewAttribute()
|
|
|
|
createdAt.SetKey(object.AttributeTimestamp)
|
|
|
|
createdAt.SetValue(unix)
|
|
|
|
|
|
|
|
attributes = append(attributes, filename, createdAt)
|
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()
|
2020-10-23 00:12:37 +00:00
|
|
|
raw.SetOwnerID(tkn.OwnerID()) // should be replaced with BearerToken.Issuer()
|
|
|
|
raw.SetContainerID(bkt.CID)
|
2020-10-19 01:04:37 +00:00
|
|
|
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-24 13:09:22 +00:00
|
|
|
if oid, err = cli.PutObject(ctx, pop, client.WithSession(tkn)); err != nil {
|
2020-10-23 00:12:37 +00:00
|
|
|
return nil, errors.Wrapf(err, "owner_id = %s", tkn.OwnerID())
|
2020-07-03 13:52:04 +00:00
|
|
|
}
|
|
|
|
|
2020-10-19 01:04:37 +00:00
|
|
|
return &ObjectInfo{
|
2020-10-24 13:09:22 +00:00
|
|
|
id: oid,
|
|
|
|
|
2020-10-19 01:04:37 +00:00
|
|
|
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
|
|
|
}
|