2020-07-03 13:52:04 +00:00
|
|
|
package layer
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2021-05-20 10:14:17 +00:00
|
|
|
"errors"
|
2020-07-03 13:52:04 +00:00
|
|
|
"io"
|
2020-10-24 13:09:22 +00:00
|
|
|
"net/url"
|
2021-07-18 13:40:19 +00:00
|
|
|
"sort"
|
2020-10-23 00:12:37 +00:00
|
|
|
"strconv"
|
2020-07-03 13:52:04 +00:00
|
|
|
"time"
|
|
|
|
|
2021-05-26 16:48:27 +00:00
|
|
|
"github.com/nspcc-dev/neofs-api-go/pkg/client"
|
2021-06-04 13:01:42 +00:00
|
|
|
cid "github.com/nspcc-dev/neofs-api-go/pkg/container/id"
|
2020-10-19 01:04:37 +00:00
|
|
|
"github.com/nspcc-dev/neofs-api-go/pkg/object"
|
2021-05-18 11:10:08 +00:00
|
|
|
"github.com/nspcc-dev/neofs-s3-gw/api"
|
2021-07-18 13:40:19 +00:00
|
|
|
"go.uber.org/zap"
|
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
|
2021-06-04 13:01:42 +00:00
|
|
|
cid *cid.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
|
2021-06-24 11:10:00 +00:00
|
|
|
*object.Range
|
2020-08-03 11:48:33 +00:00
|
|
|
|
2020-11-24 07:01:38 +00:00
|
|
|
offset int64
|
|
|
|
length int64
|
|
|
|
address *object.Address
|
2020-07-03 13:52:04 +00:00
|
|
|
}
|
2021-07-18 13:40:19 +00:00
|
|
|
|
|
|
|
// ListObjectsParamsCommon contains common parameters for ListObjectsV1 and ListObjectsV2.
|
|
|
|
ListObjectsParamsCommon struct {
|
|
|
|
Bucket string
|
|
|
|
Delimiter string
|
|
|
|
Encode string
|
|
|
|
MaxKeys int
|
|
|
|
Prefix string
|
|
|
|
}
|
|
|
|
|
|
|
|
// ListObjectsParamsV1 contains params for ListObjectsV1.
|
|
|
|
ListObjectsParamsV1 struct {
|
|
|
|
ListObjectsParamsCommon
|
|
|
|
Marker string
|
|
|
|
}
|
|
|
|
|
|
|
|
// ListObjectsParamsV2 contains params for ListObjectsV2.
|
|
|
|
ListObjectsParamsV2 struct {
|
|
|
|
ListObjectsParamsCommon
|
|
|
|
ContinuationToken string
|
|
|
|
StartAfter string
|
|
|
|
}
|
|
|
|
|
|
|
|
allObjectParams struct {
|
|
|
|
Bucket string
|
|
|
|
Delimiter string
|
|
|
|
Prefix string
|
|
|
|
StartAfter string
|
|
|
|
}
|
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) {
|
2021-05-26 16:48:27 +00:00
|
|
|
var opts object.SearchFilters
|
|
|
|
|
|
|
|
opts.AddRootFilter()
|
2020-10-24 13:09:22 +00:00
|
|
|
|
2020-11-27 12:36:15 +00:00
|
|
|
if filename, err := url.QueryUnescape(p.val); err != nil {
|
2020-08-20 19:33:59 +00:00
|
|
|
return nil, err
|
2020-11-27 12:36:15 +00:00
|
|
|
} else if filename != "" {
|
2021-05-26 16:48:27 +00:00
|
|
|
opts.AddFilter(object.AttributeFileName, filename, object.MatchStringEqual)
|
|
|
|
}
|
2021-06-15 13:59:51 +00:00
|
|
|
return n.pool.SearchObject(ctx, new(client.SearchObjectParams).WithContainerID(p.cid).WithSearchFilters(opts), n.BearerOpt(ctx))
|
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 {
|
2021-07-12 12:04:27 +00:00
|
|
|
return nil, api.GetAPIError(api.ErrNoSuchKey)
|
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-11-24 07:01:38 +00:00
|
|
|
func (n *layer) objectHead(ctx context.Context, address *object.Address) (*object.Object, error) {
|
2021-05-26 16:48:27 +00:00
|
|
|
ops := new(client.ObjectHeaderParams).WithAddress(address).WithAllFields()
|
2021-06-15 13:59:51 +00:00
|
|
|
return n.pool.GetObjectHeader(ctx, ops, n.BearerOpt(ctx))
|
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) {
|
|
|
|
// prepare length/offset writer
|
2020-12-10 15:15:18 +00:00
|
|
|
w := newWriter(p.Writer, p.offset, p.length)
|
2021-05-26 16:48:27 +00:00
|
|
|
ops := new(client.GetObjectParams).WithAddress(p.address).WithPayloadWriter(w)
|
2021-06-15 13:59:51 +00:00
|
|
|
return n.pool.GetObject(ctx, ops, n.BearerOpt(ctx))
|
2020-07-03 13:52:04 +00:00
|
|
|
}
|
|
|
|
|
2021-06-24 11:10:00 +00:00
|
|
|
// objectRange gets object range and writes it into provided io.Writer.
|
|
|
|
func (n *layer) objectRange(ctx context.Context, p *getParams) ([]byte, error) {
|
|
|
|
w := newWriter(p.Writer, p.offset, p.length)
|
|
|
|
ops := new(client.RangeDataParams).WithAddress(p.address).WithDataWriter(w).WithRange(p.Range)
|
|
|
|
return n.pool.ObjectPayloadRangeData(ctx, ops, n.BearerOpt(ctx))
|
|
|
|
}
|
|
|
|
|
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-23 00:12:37 +00:00
|
|
|
bkt *BucketInfo
|
2020-11-27 12:36:15 +00:00
|
|
|
own = n.Owner(ctx)
|
2020-11-24 07:01:38 +00:00
|
|
|
)
|
2020-10-22 00:19:16 +00:00
|
|
|
|
2020-11-27 12:36:15 +00:00
|
|
|
if obj, err = url.QueryUnescape(p.Object); err != nil {
|
2020-10-24 13:09:22 +00:00
|
|
|
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
|
2021-07-12 12:04:27 +00:00
|
|
|
} else if err = n.checkObject(ctx, bkt.CID, p.Object); err != nil {
|
|
|
|
var errExist *api.ObjectAlreadyExists
|
|
|
|
if ok := errors.As(err, &errExist); ok {
|
|
|
|
errExist.Bucket = p.Bucket
|
|
|
|
errExist.Object = p.Object
|
|
|
|
return nil, errExist
|
|
|
|
}
|
|
|
|
|
|
|
|
if !api.IsS3Error(err, api.ErrNoSuchKey) {
|
|
|
|
return nil, err
|
2020-07-03 13:52:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
raw := object.NewRaw()
|
2020-11-27 12:36:15 +00:00
|
|
|
raw.SetOwnerID(own)
|
2020-10-23 00:12:37 +00:00
|
|
|
raw.SetContainerID(bkt.CID)
|
2020-10-19 01:04:37 +00:00
|
|
|
raw.SetAttributes(attributes...)
|
2020-07-03 13:52:04 +00:00
|
|
|
|
2020-11-27 12:36:15 +00:00
|
|
|
r := newDetector(p.Reader)
|
2021-05-26 16:48:27 +00:00
|
|
|
|
|
|
|
ops := new(client.PutObjectParams).WithObject(raw.Object()).WithPayloadReader(r)
|
2021-06-15 08:51:42 +00:00
|
|
|
oid, err := n.pool.PutObject(
|
2021-05-26 16:48:27 +00:00
|
|
|
ctx,
|
|
|
|
ops,
|
2021-06-15 13:59:51 +00:00
|
|
|
n.BearerOpt(ctx),
|
2021-05-26 16:48:27 +00:00
|
|
|
)
|
|
|
|
if err != nil {
|
2020-11-24 07:01:38 +00:00
|
|
|
return nil, err
|
2020-07-03 13:52:04 +00:00
|
|
|
}
|
|
|
|
|
2021-06-29 13:40:26 +00:00
|
|
|
addr := object.NewAddress()
|
|
|
|
addr.SetObjectID(oid)
|
|
|
|
addr.SetContainerID(bkt.CID)
|
|
|
|
meta, err := n.objectHead(ctx, addr)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-10-19 01:04:37 +00:00
|
|
|
return &ObjectInfo{
|
2021-05-26 16:48:27 +00:00
|
|
|
id: oid,
|
2020-10-24 13:09:22 +00:00
|
|
|
|
2020-11-27 12:36:15 +00:00
|
|
|
Owner: own,
|
2020-10-19 01:04:37 +00:00
|
|
|
Bucket: p.Bucket,
|
|
|
|
Name: p.Object,
|
|
|
|
Size: p.Size,
|
|
|
|
Created: time.Now(),
|
|
|
|
Headers: p.Header,
|
2020-11-27 12:36:15 +00:00
|
|
|
ContentType: r.contentType,
|
2021-06-29 13:40:26 +00:00
|
|
|
HashSum: meta.PayloadChecksum().String(),
|
2020-10-19 01:04:37 +00:00
|
|
|
}, 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 {
|
2021-05-26 16:48:27 +00:00
|
|
|
dop := new(client.DeleteObjectParams)
|
|
|
|
dop.WithAddress(address)
|
2021-06-15 13:59:51 +00:00
|
|
|
return n.pool.DeleteObject(ctx, dop, n.BearerOpt(ctx))
|
2020-07-03 13:52:04 +00:00
|
|
|
}
|
2021-07-18 13:40:19 +00:00
|
|
|
|
|
|
|
// ListObjectsV1 returns objects in a bucket for requests of Version 1.
|
|
|
|
func (n *layer) ListObjectsV1(ctx context.Context, p *ListObjectsParamsV1) (*ListObjectsInfoV1, error) {
|
|
|
|
var (
|
|
|
|
err error
|
|
|
|
result ListObjectsInfoV1
|
|
|
|
)
|
|
|
|
|
|
|
|
if p.MaxKeys == 0 {
|
|
|
|
return &result, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
allObjects, err := n.listSortedAllObjects(ctx, allObjectParams{
|
|
|
|
Bucket: p.Bucket,
|
|
|
|
Prefix: p.Prefix,
|
|
|
|
Delimiter: p.Delimiter,
|
|
|
|
StartAfter: p.Marker,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(allObjects) > p.MaxKeys {
|
|
|
|
result.IsTruncated = true
|
|
|
|
|
|
|
|
nextObject := allObjects[p.MaxKeys-1]
|
|
|
|
result.NextMarker = nextObject.Name
|
|
|
|
|
|
|
|
allObjects = allObjects[:p.MaxKeys]
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, ov := range allObjects {
|
|
|
|
if ov.isDir {
|
|
|
|
result.Prefixes = append(result.Prefixes, ov.Name)
|
|
|
|
} else {
|
|
|
|
result.Objects = append(result.Objects, ov)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return &result, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// ListObjectsV2 returns objects in a bucket for requests of Version 2.
|
|
|
|
func (n *layer) ListObjectsV2(ctx context.Context, p *ListObjectsParamsV2) (*ListObjectsInfoV2, error) {
|
|
|
|
var (
|
|
|
|
err error
|
|
|
|
result ListObjectsInfoV2
|
|
|
|
allObjects []*ObjectInfo
|
|
|
|
)
|
|
|
|
|
|
|
|
if p.MaxKeys == 0 {
|
|
|
|
return &result, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if p.ContinuationToken != "" {
|
|
|
|
// find cache with continuation token
|
|
|
|
} else {
|
|
|
|
allObjects, err = n.listSortedAllObjects(ctx, allObjectParams{
|
|
|
|
Bucket: p.Bucket,
|
|
|
|
Prefix: p.Prefix,
|
|
|
|
Delimiter: p.Delimiter,
|
|
|
|
StartAfter: p.StartAfter,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(allObjects) > p.MaxKeys {
|
|
|
|
result.IsTruncated = true
|
|
|
|
|
|
|
|
allObjects = allObjects[:p.MaxKeys]
|
|
|
|
// add creating of cache here
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, ov := range allObjects {
|
|
|
|
if ov.isDir {
|
|
|
|
result.Prefixes = append(result.Prefixes, ov.Name)
|
|
|
|
} else {
|
|
|
|
result.Objects = append(result.Objects, ov)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return &result, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *layer) listSortedAllObjects(ctx context.Context, p allObjectParams) ([]*ObjectInfo, error) {
|
|
|
|
var (
|
|
|
|
err error
|
|
|
|
bkt *BucketInfo
|
|
|
|
ids []*object.ID
|
|
|
|
uniqNames = make(map[string]bool)
|
|
|
|
)
|
|
|
|
|
|
|
|
if bkt, err = n.GetBucketInfo(ctx, p.Bucket); err != nil {
|
|
|
|
return nil, err
|
|
|
|
} else if ids, err = n.objectSearch(ctx, &findParams{cid: bkt.CID}); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
objects := make([]*ObjectInfo, 0, len(ids))
|
|
|
|
|
|
|
|
for _, id := range ids {
|
|
|
|
addr := object.NewAddress()
|
|
|
|
addr.SetObjectID(id)
|
|
|
|
addr.SetContainerID(bkt.CID)
|
|
|
|
|
|
|
|
meta, err := n.objectHead(ctx, addr)
|
|
|
|
if err != nil {
|
|
|
|
n.log.Warn("could not fetch object meta", zap.Error(err))
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if oi := objectInfoFromMeta(bkt, meta, p.Prefix, p.Delimiter); oi != nil {
|
|
|
|
// use only unique dir names
|
|
|
|
if _, ok := uniqNames[oi.Name]; ok {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if len(p.StartAfter) > 0 && oi.Name <= p.StartAfter {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
uniqNames[oi.Name] = oi.isDir
|
|
|
|
|
|
|
|
objects = append(objects, oi)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
sort.Slice(objects, func(i, j int) bool {
|
|
|
|
return objects[i].Name < objects[j].Name
|
|
|
|
})
|
|
|
|
|
|
|
|
return objects, nil
|
|
|
|
}
|