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"
|
2021-08-10 10:03:09 +00:00
|
|
|
"strings"
|
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-08-13 14:13:14 +00:00
|
|
|
"github.com/nspcc-dev/neofs-api-go/pkg/owner"
|
2021-08-18 13:48:58 +00:00
|
|
|
"github.com/nspcc-dev/neofs-s3-gw/api"
|
2021-08-27 22:20:40 +00:00
|
|
|
"github.com/nspcc-dev/neofs-s3-gw/api/cache"
|
2021-08-09 08:53:58 +00:00
|
|
|
apiErrors "github.com/nspcc-dev/neofs-s3-gw/api/errors"
|
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 {
|
2021-09-01 16:10:31 +00:00
|
|
|
attr string
|
|
|
|
val string
|
|
|
|
cid *cid.ID
|
|
|
|
prefix string
|
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
|
|
|
|
2021-08-18 13:48:58 +00:00
|
|
|
offset int64
|
|
|
|
length int64
|
|
|
|
cid *cid.ID
|
|
|
|
oid *object.ID
|
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
|
2021-07-20 12:40:38 +00:00
|
|
|
FetchOwner bool
|
2021-07-18 13:40:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
allObjectParams struct {
|
2021-08-27 21:33:50 +00:00
|
|
|
Bucket *api.BucketInfo
|
2021-07-29 08:14:14 +00:00
|
|
|
Delimiter string
|
|
|
|
Prefix string
|
2021-07-18 13:40:19 +00:00
|
|
|
}
|
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-08-13 14:13:14 +00:00
|
|
|
if p.attr == "" {
|
|
|
|
opts.AddFilter(object.AttributeFileName, filename, object.MatchStringEqual)
|
|
|
|
} else {
|
|
|
|
opts.AddFilter(p.attr, filename, object.MatchStringEqual)
|
|
|
|
}
|
2021-05-26 16:48:27 +00:00
|
|
|
}
|
2021-09-01 16:10:31 +00:00
|
|
|
if prefix, err := url.QueryUnescape(p.prefix); err != nil {
|
|
|
|
return nil, err
|
|
|
|
} else if prefix != "" {
|
|
|
|
opts.AddFilter(object.AttributeFileName, prefix, object.MatchCommonPrefix)
|
|
|
|
}
|
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-08-09 08:53:58 +00:00
|
|
|
return nil, apiErrors.GetAPIError(apiErrors.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
|
|
|
}
|
|
|
|
|
2021-08-18 13:48:58 +00:00
|
|
|
func newAddress(cid *cid.ID, oid *object.ID) *object.Address {
|
2021-08-10 10:03:09 +00:00
|
|
|
address := object.NewAddress()
|
|
|
|
address.SetContainerID(cid)
|
|
|
|
address.SetObjectID(oid)
|
2021-08-18 13:48:58 +00:00
|
|
|
return address
|
|
|
|
}
|
|
|
|
|
|
|
|
// objectHead returns all object's headers.
|
|
|
|
func (n *layer) objectHead(ctx context.Context, cid *cid.ID, oid *object.ID) (*object.Object, error) {
|
|
|
|
ops := new(client.ObjectHeaderParams).WithAddress(newAddress(cid, oid)).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-08-18 13:48:58 +00:00
|
|
|
ops := new(client.GetObjectParams).WithAddress(newAddress(p.cid, p.oid)).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)
|
2021-08-18 13:48:58 +00:00
|
|
|
ops := new(client.RangeDataParams).WithAddress(newAddress(p.cid, p.oid)).WithDataWriter(w).WithRange(p.Range)
|
2021-06-24 11:10:00 +00:00
|
|
|
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.
|
2021-08-27 21:33:50 +00:00
|
|
|
func (n *layer) objectPut(ctx context.Context, bkt *api.BucketInfo, p *PutObjectParams) (*api.ObjectInfo, error) {
|
2021-08-13 14:13:14 +00:00
|
|
|
own := n.Owner(ctx)
|
|
|
|
obj, err := url.QueryUnescape(p.Object)
|
|
|
|
if err != nil {
|
2020-10-24 13:09:22 +00:00
|
|
|
return nil, err
|
2021-08-10 08:19:09 +00:00
|
|
|
}
|
2021-07-12 12:04:27 +00:00
|
|
|
|
2021-08-10 08:58:40 +00:00
|
|
|
versioningEnabled := n.isVersioningEnabled(ctx, bkt)
|
2021-08-13 14:13:14 +00:00
|
|
|
versions, err := n.headVersions(ctx, bkt, obj)
|
2021-08-10 08:19:09 +00:00
|
|
|
if err != nil && !apiErrors.IsS3Error(err, apiErrors.ErrNoSuchKey) {
|
|
|
|
return nil, err
|
2020-07-03 13:52:04 +00:00
|
|
|
}
|
2021-08-13 14:13:14 +00:00
|
|
|
idsToDeleteArr := updateCRDT2PSetHeaders(p, versions, versioningEnabled)
|
2020-07-03 13:52:04 +00:00
|
|
|
|
2021-08-18 13:48:58 +00:00
|
|
|
r := p.Reader
|
|
|
|
if len(p.Header[api.ContentType]) == 0 {
|
2021-08-19 06:55:22 +00:00
|
|
|
d := newDetector(r)
|
2021-08-18 13:48:58 +00:00
|
|
|
if contentType, err := d.Detect(); err == nil {
|
|
|
|
p.Header[api.ContentType] = contentType
|
|
|
|
}
|
|
|
|
r = d.MultiReader()
|
|
|
|
}
|
2021-08-13 14:13:14 +00:00
|
|
|
rawObject := formRawObject(p, bkt.CID, own, obj)
|
2021-08-10 08:19:09 +00:00
|
|
|
|
2021-08-13 14:13:14 +00:00
|
|
|
ops := new(client.PutObjectParams).WithObject(rawObject.Object()).WithPayloadReader(r)
|
|
|
|
oid, err := n.pool.PutObject(ctx, ops, n.BearerOpt(ctx))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-08-18 13:48:58 +00:00
|
|
|
if p.Header[versionsDeleteMarkAttr] == delMarkFullObject {
|
|
|
|
if last := versions.getLast(); last != nil {
|
|
|
|
n.objCache.Delete(last.Address())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-13 14:13:14 +00:00
|
|
|
meta, err := n.objectHead(ctx, bkt.CID, oid)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-08-18 13:48:58 +00:00
|
|
|
if err = n.objCache.Put(*meta); err != nil {
|
|
|
|
n.log.Error("couldn't cache an object", zap.Error(err))
|
|
|
|
}
|
|
|
|
|
2021-09-01 16:10:52 +00:00
|
|
|
n.listsCache.CleanCacheEntriesContainingObject(p.Object, bkt.CID)
|
|
|
|
|
2021-08-13 14:13:14 +00:00
|
|
|
for _, id := range idsToDeleteArr {
|
|
|
|
if err = n.objectDelete(ctx, bkt.CID, id); err != nil {
|
|
|
|
n.log.Warn("couldn't delete object",
|
|
|
|
zap.Stringer("version id", id),
|
|
|
|
zap.Error(err))
|
2021-08-10 08:58:40 +00:00
|
|
|
}
|
2021-08-17 08:04:42 +00:00
|
|
|
if !versioningEnabled {
|
|
|
|
if objVersion := versions.getVersion(id); objVersion != nil {
|
|
|
|
if err = n.DeleteObjectTagging(ctx, objVersion); err != nil {
|
|
|
|
n.log.Warn("couldn't delete object tagging",
|
|
|
|
zap.Stringer("version id", id),
|
|
|
|
zap.Error(err))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-08-10 08:19:09 +00:00
|
|
|
}
|
2020-07-03 13:52:04 +00:00
|
|
|
|
2021-08-27 21:33:50 +00:00
|
|
|
return &api.ObjectInfo{
|
|
|
|
ID: oid,
|
|
|
|
CID: bkt.CID,
|
2021-08-13 14:13:14 +00:00
|
|
|
|
|
|
|
Owner: own,
|
|
|
|
Bucket: p.Bucket,
|
|
|
|
Name: p.Object,
|
|
|
|
Size: p.Size,
|
|
|
|
Created: time.Now(),
|
|
|
|
CreationEpoch: meta.CreationEpoch(),
|
|
|
|
Headers: p.Header,
|
2021-08-18 13:48:58 +00:00
|
|
|
ContentType: p.Header[api.ContentType],
|
2021-08-13 14:13:14 +00:00
|
|
|
HashSum: meta.PayloadChecksum().String(),
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func formRawObject(p *PutObjectParams, bktID *cid.ID, own *owner.ID, obj string) *object.RawObject {
|
|
|
|
attributes := make([]*object.Attribute, 0, len(p.Header)+2)
|
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)
|
2021-08-10 12:08:15 +00:00
|
|
|
createdAt.SetValue(strconv.FormatInt(time.Now().UTC().Unix(), 10))
|
2020-10-23 00:12:37 +00:00
|
|
|
|
|
|
|
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)
|
2021-08-13 14:13:14 +00:00
|
|
|
raw.SetContainerID(bktID)
|
2020-10-19 01:04:37 +00:00
|
|
|
raw.SetAttributes(attributes...)
|
2020-07-03 13:52:04 +00:00
|
|
|
|
2021-08-13 14:13:14 +00:00
|
|
|
return raw
|
|
|
|
}
|
2021-05-26 16:48:27 +00:00
|
|
|
|
2021-08-13 14:13:14 +00:00
|
|
|
func updateCRDT2PSetHeaders(p *PutObjectParams, versions *objectVersions, versioningEnabled bool) []*object.ID {
|
|
|
|
var idsToDeleteArr []*object.ID
|
|
|
|
if versions == nil {
|
|
|
|
return idsToDeleteArr
|
2020-07-03 13:52:04 +00:00
|
|
|
}
|
|
|
|
|
2021-08-13 14:13:14 +00:00
|
|
|
if versioningEnabled {
|
|
|
|
if len(versions.addList) != 0 {
|
|
|
|
p.Header[versionsAddAttr] = versions.getAddHeader()
|
|
|
|
}
|
2021-06-29 13:40:26 +00:00
|
|
|
|
2021-08-13 14:13:14 +00:00
|
|
|
deleted := versions.getDelHeader()
|
|
|
|
// p.Header[versionsDelAttr] can be not empty when deleting specific version
|
|
|
|
if delAttr := p.Header[versionsDelAttr]; len(delAttr) != 0 {
|
|
|
|
if len(deleted) != 0 {
|
|
|
|
p.Header[versionsDelAttr] = deleted + "," + delAttr
|
|
|
|
} else {
|
|
|
|
p.Header[versionsDelAttr] = delAttr
|
|
|
|
}
|
|
|
|
} else if len(deleted) != 0 {
|
|
|
|
p.Header[versionsDelAttr] = deleted
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
versionsDeletedStr := versions.getDelHeader()
|
|
|
|
if len(versionsDeletedStr) != 0 {
|
|
|
|
versionsDeletedStr += ","
|
|
|
|
}
|
2021-08-10 11:19:26 +00:00
|
|
|
|
2021-08-18 13:48:58 +00:00
|
|
|
if lastVersion := versions.getLast(); lastVersion != nil {
|
|
|
|
p.Header[versionsDelAttr] = versionsDeletedStr + lastVersion.Version()
|
2021-08-27 21:33:50 +00:00
|
|
|
idsToDeleteArr = append(idsToDeleteArr, lastVersion.ID)
|
2021-08-18 13:48:58 +00:00
|
|
|
} else if len(versionsDeletedStr) != 0 {
|
|
|
|
p.Header[versionsDelAttr] = versionsDeletedStr
|
|
|
|
}
|
2020-10-24 13:09:22 +00:00
|
|
|
|
2021-08-13 14:13:14 +00:00
|
|
|
for _, version := range versions.objects {
|
|
|
|
if contains(versions.delList, version.Version()) {
|
2021-08-27 21:33:50 +00:00
|
|
|
idsToDeleteArr = append(idsToDeleteArr, version.ID)
|
2021-08-13 14:13:14 +00:00
|
|
|
}
|
|
|
|
}
|
2021-08-10 08:19:09 +00:00
|
|
|
}
|
|
|
|
|
2021-08-13 14:13:14 +00:00
|
|
|
return idsToDeleteArr
|
|
|
|
}
|
|
|
|
|
2021-08-27 21:33:50 +00:00
|
|
|
func (n *layer) headLastVersionIfNotDeleted(ctx context.Context, bkt *api.BucketInfo, objectName string) (*api.ObjectInfo, error) {
|
2021-08-19 06:55:22 +00:00
|
|
|
if address := n.namesCache.Get(bkt.Name + "/" + objectName); address != nil {
|
2021-08-18 13:48:58 +00:00
|
|
|
if headInfo := n.objCache.Get(address); headInfo != nil {
|
|
|
|
return objInfoFromMeta(bkt, headInfo), nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-13 14:13:14 +00:00
|
|
|
versions, err := n.headVersions(ctx, bkt, objectName)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2021-08-10 08:19:09 +00:00
|
|
|
}
|
|
|
|
|
2021-08-13 14:13:14 +00:00
|
|
|
lastVersion := versions.getLast()
|
|
|
|
if lastVersion == nil {
|
|
|
|
return nil, apiErrors.GetAPIError(apiErrors.ErrNoSuchKey)
|
|
|
|
}
|
2021-08-18 13:48:58 +00:00
|
|
|
|
2021-08-19 06:55:22 +00:00
|
|
|
if err = n.namesCache.Put(lastVersion.NiceName(), lastVersion.Address()); err != nil {
|
2021-08-18 13:48:58 +00:00
|
|
|
n.log.Warn("couldn't put obj address to head cache",
|
|
|
|
zap.String("obj nice name", lastVersion.NiceName()),
|
|
|
|
zap.Error(err))
|
|
|
|
}
|
|
|
|
|
2021-08-13 14:13:14 +00:00
|
|
|
return lastVersion, nil
|
2021-08-10 08:19:09 +00:00
|
|
|
}
|
|
|
|
|
2021-08-27 21:33:50 +00:00
|
|
|
func (n *layer) headVersions(ctx context.Context, bkt *api.BucketInfo, objectName string) (*objectVersions, error) {
|
2021-08-10 08:19:09 +00:00
|
|
|
ids, err := n.objectSearch(ctx, &findParams{cid: bkt.CID, val: objectName})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(ids) == 0 {
|
2021-08-11 10:02:13 +00:00
|
|
|
return nil, apiErrors.GetAPIError(apiErrors.ErrNoSuchKey)
|
2021-08-10 08:19:09 +00:00
|
|
|
}
|
|
|
|
|
2021-08-16 09:31:50 +00:00
|
|
|
versions := newObjectVersions(objectName)
|
2021-08-10 08:19:09 +00:00
|
|
|
for _, id := range ids {
|
2021-08-10 10:03:09 +00:00
|
|
|
meta, err := n.objectHead(ctx, bkt.CID, id)
|
2021-08-10 08:19:09 +00:00
|
|
|
if err != nil {
|
|
|
|
n.log.Warn("couldn't head object",
|
|
|
|
zap.Stringer("object id", id),
|
|
|
|
zap.Stringer("bucket id", bkt.CID),
|
|
|
|
zap.Error(err))
|
|
|
|
continue
|
|
|
|
}
|
2021-08-18 13:48:58 +00:00
|
|
|
if err = n.objCache.Put(*meta); err != nil {
|
|
|
|
n.log.Warn("couldn't put meta to objects cache",
|
|
|
|
zap.Stringer("object id", id),
|
|
|
|
zap.Stringer("bucket id", bkt.CID),
|
|
|
|
zap.Error(err))
|
|
|
|
}
|
|
|
|
|
2021-08-13 14:13:14 +00:00
|
|
|
if oi := objectInfoFromMeta(bkt, meta, "", ""); oi != nil {
|
|
|
|
if isSystem(oi) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
versions.appendVersion(oi)
|
|
|
|
}
|
2021-08-10 08:19:09 +00:00
|
|
|
}
|
|
|
|
|
2021-08-13 14:13:14 +00:00
|
|
|
return versions, nil
|
2021-08-10 10:03:09 +00:00
|
|
|
}
|
|
|
|
|
2021-08-27 21:33:50 +00:00
|
|
|
func (n *layer) headVersion(ctx context.Context, bkt *api.BucketInfo, versionID string) (*api.ObjectInfo, error) {
|
2021-08-10 12:08:15 +00:00
|
|
|
oid := object.NewID()
|
|
|
|
if err := oid.Parse(versionID); err != nil {
|
2021-08-10 10:03:09 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-08-18 13:48:58 +00:00
|
|
|
if headInfo := n.objCache.Get(newAddress(bkt.CID, oid)); headInfo != nil {
|
|
|
|
return objInfoFromMeta(bkt, headInfo), nil
|
|
|
|
}
|
|
|
|
|
2021-08-10 12:08:15 +00:00
|
|
|
meta, err := n.objectHead(ctx, bkt.CID, oid)
|
|
|
|
if err != nil {
|
|
|
|
if strings.Contains(err.Error(), "not found") {
|
2021-08-11 10:02:13 +00:00
|
|
|
return nil, apiErrors.GetAPIError(apiErrors.ErrNoSuchVersion)
|
2021-08-10 10:03:09 +00:00
|
|
|
}
|
2021-08-10 12:08:15 +00:00
|
|
|
return nil, err
|
2021-08-10 10:03:09 +00:00
|
|
|
}
|
|
|
|
|
2021-08-18 13:48:58 +00:00
|
|
|
objInfo := objectInfoFromMeta(bkt, meta, "", "")
|
|
|
|
if err = n.objCache.Put(*meta); err != nil {
|
|
|
|
n.log.Warn("couldn't put obj to object cache",
|
|
|
|
zap.String("bucket name", objInfo.Bucket),
|
2021-08-27 21:33:50 +00:00
|
|
|
zap.Stringer("bucket cid", objInfo.CID),
|
2021-08-18 13:48:58 +00:00
|
|
|
zap.String("object name", objInfo.Name),
|
2021-08-27 21:33:50 +00:00
|
|
|
zap.Stringer("object id", objInfo.ID),
|
2021-08-18 13:48:58 +00:00
|
|
|
zap.Error(err))
|
|
|
|
}
|
|
|
|
|
|
|
|
return objInfo, nil
|
2020-07-03 13:52:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// objectDelete puts tombstone object into neofs.
|
2021-08-10 12:08:15 +00:00
|
|
|
func (n *layer) objectDelete(ctx context.Context, cid *cid.ID, oid *object.ID) error {
|
2021-08-18 13:48:58 +00:00
|
|
|
address := newAddress(cid, oid)
|
2021-05-26 16:48:27 +00:00
|
|
|
dop := new(client.DeleteObjectParams)
|
|
|
|
dop.WithAddress(address)
|
2021-08-10 11:19:26 +00:00
|
|
|
n.objCache.Delete(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 (
|
2021-07-29 08:14:14 +00:00
|
|
|
err error
|
|
|
|
result ListObjectsInfoV1
|
2021-08-27 21:33:50 +00:00
|
|
|
allObjects []*api.ObjectInfo
|
2021-07-18 13:40:19 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
if p.MaxKeys == 0 {
|
|
|
|
return &result, nil
|
|
|
|
}
|
|
|
|
|
2021-07-29 08:14:14 +00:00
|
|
|
if allObjects, err = n.listAllObjects(ctx, p.ListObjectsParamsCommon); err != nil {
|
2021-07-22 08:57:37 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-07-29 08:14:14 +00:00
|
|
|
if len(allObjects) == 0 {
|
|
|
|
return &result, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if p.Marker != "" {
|
|
|
|
allObjects = trimAfterObjectName(p.Marker, allObjects)
|
2021-07-18 13:40:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if len(allObjects) > p.MaxKeys {
|
|
|
|
result.IsTruncated = true
|
|
|
|
allObjects = allObjects[:p.MaxKeys]
|
2021-07-29 08:14:14 +00:00
|
|
|
result.NextMarker = allObjects[len(allObjects)-1].Name
|
2021-07-18 13:40:19 +00:00
|
|
|
}
|
|
|
|
|
2021-07-29 08:14:14 +00:00
|
|
|
result.Prefixes, result.Objects = triageObjects(allObjects)
|
2021-07-18 13:40:19 +00:00
|
|
|
|
|
|
|
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
|
2021-08-27 21:33:50 +00:00
|
|
|
allObjects []*api.ObjectInfo
|
2021-07-18 13:40:19 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
if p.MaxKeys == 0 {
|
|
|
|
return &result, nil
|
|
|
|
}
|
|
|
|
|
2021-07-29 08:14:14 +00:00
|
|
|
if allObjects, err = n.listAllObjects(ctx, p.ListObjectsParamsCommon); err != nil {
|
2021-07-22 08:57:37 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-07-29 08:14:14 +00:00
|
|
|
if len(allObjects) == 0 {
|
|
|
|
return &result, nil
|
2021-07-28 13:28:05 +00:00
|
|
|
}
|
|
|
|
|
2021-07-18 13:40:19 +00:00
|
|
|
if p.ContinuationToken != "" {
|
2021-07-29 08:14:14 +00:00
|
|
|
allObjects = trimAfterObjectID(p.ContinuationToken, allObjects)
|
2021-07-28 13:28:05 +00:00
|
|
|
}
|
|
|
|
|
2021-07-29 08:14:14 +00:00
|
|
|
if p.StartAfter != "" {
|
|
|
|
allObjects = trimAfterObjectName(p.StartAfter, allObjects)
|
2021-07-18 13:40:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if len(allObjects) > p.MaxKeys {
|
|
|
|
result.IsTruncated = true
|
|
|
|
allObjects = allObjects[:p.MaxKeys]
|
2021-08-27 21:33:50 +00:00
|
|
|
result.NextContinuationToken = allObjects[len(allObjects)-1].ID.String()
|
2021-07-18 13:40:19 +00:00
|
|
|
}
|
|
|
|
|
2021-07-29 08:14:14 +00:00
|
|
|
result.Prefixes, result.Objects = triageObjects(allObjects)
|
|
|
|
|
2021-07-18 13:40:19 +00:00
|
|
|
return &result, nil
|
|
|
|
}
|
|
|
|
|
2021-09-01 16:10:31 +00:00
|
|
|
func (n *layer) listSortedObjects(ctx context.Context, p allObjectParams) ([]*api.ObjectInfo, error) {
|
2021-08-18 13:48:58 +00:00
|
|
|
versions, err := n.getAllObjectsVersions(ctx, p.Bucket, p.Prefix, p.Delimiter)
|
2021-08-13 14:13:14 +00:00
|
|
|
if err != nil {
|
2021-07-18 13:40:19 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-08-27 21:33:50 +00:00
|
|
|
objects := make([]*api.ObjectInfo, 0, len(versions))
|
2021-08-18 13:48:58 +00:00
|
|
|
for _, v := range versions {
|
|
|
|
lastVersion := v.getLast()
|
|
|
|
if lastVersion != nil {
|
|
|
|
objects = append(objects, lastVersion)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
sort.Slice(objects, func(i, j int) bool {
|
|
|
|
return objects[i].Name < objects[j].Name
|
|
|
|
})
|
|
|
|
|
|
|
|
return objects, nil
|
|
|
|
}
|
|
|
|
|
2021-08-27 21:33:50 +00:00
|
|
|
func (n *layer) getAllObjectsVersions(ctx context.Context, bkt *api.BucketInfo, prefix, delimiter string) (map[string]*objectVersions, error) {
|
2021-09-01 16:10:31 +00:00
|
|
|
var err error
|
|
|
|
|
|
|
|
cacheKey := cache.CreateObjectsListCacheKey(bkt.CID, prefix)
|
|
|
|
ids := n.listsCache.Get(cacheKey)
|
|
|
|
|
|
|
|
if ids == nil {
|
|
|
|
ids, err = n.objectSearch(ctx, &findParams{cid: bkt.CID, prefix: prefix})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if err := n.listsCache.Put(cacheKey, ids); err != nil {
|
|
|
|
n.log.Error("couldn't cache list of objects", zap.Error(err))
|
|
|
|
}
|
2021-08-18 13:48:58 +00:00
|
|
|
}
|
2021-07-18 13:40:19 +00:00
|
|
|
|
2021-08-18 13:48:58 +00:00
|
|
|
versions := make(map[string]*objectVersions, len(ids)/2)
|
2021-09-01 16:10:31 +00:00
|
|
|
|
|
|
|
for i := 0; i < len(ids); i++ {
|
|
|
|
obj := n.objectFromObjectsCacheOrNeoFS(ctx, bkt.CID, ids[i])
|
|
|
|
if obj == nil {
|
2021-07-18 13:40:19 +00:00
|
|
|
continue
|
|
|
|
}
|
2021-09-01 16:10:31 +00:00
|
|
|
if oi := objectInfoFromMeta(bkt, obj, prefix, delimiter); oi != nil {
|
2021-08-13 14:13:14 +00:00
|
|
|
if isSystem(oi) {
|
2021-07-18 13:40:19 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2021-08-19 06:55:22 +00:00
|
|
|
objVersions, ok := versions[oi.Name]
|
|
|
|
if !ok {
|
|
|
|
objVersions = newObjectVersions(oi.Name)
|
2021-08-13 14:13:14 +00:00
|
|
|
}
|
2021-08-19 06:55:22 +00:00
|
|
|
objVersions.appendVersion(oi)
|
|
|
|
versions[oi.Name] = objVersions
|
2021-08-13 14:13:14 +00:00
|
|
|
}
|
|
|
|
}
|
2021-07-18 13:40:19 +00:00
|
|
|
|
2021-08-18 13:48:58 +00:00
|
|
|
return versions, nil
|
2021-07-18 13:40:19 +00:00
|
|
|
}
|
2021-07-28 13:28:05 +00:00
|
|
|
|
2021-08-13 14:13:14 +00:00
|
|
|
func getExistedVersions(versions *objectVersions) []string {
|
|
|
|
var res []string
|
|
|
|
for _, add := range versions.addList {
|
|
|
|
if !contains(versions.delList, add) {
|
|
|
|
res = append(res, add)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return res
|
|
|
|
}
|
|
|
|
|
|
|
|
func splitVersions(header string) []string {
|
|
|
|
if len(header) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return strings.Split(header, ",")
|
|
|
|
}
|
|
|
|
|
2021-08-27 21:33:50 +00:00
|
|
|
func isSystem(obj *api.ObjectInfo) bool {
|
2021-08-13 14:13:14 +00:00
|
|
|
return len(obj.Headers[objectSystemAttributeName]) > 0 ||
|
|
|
|
len(obj.Headers[attrVersionsIgnore]) > 0
|
|
|
|
}
|
|
|
|
|
2021-08-27 21:33:50 +00:00
|
|
|
func trimAfterObjectName(startAfter string, objects []*api.ObjectInfo) []*api.ObjectInfo {
|
2021-07-29 08:14:14 +00:00
|
|
|
if len(objects) != 0 && objects[len(objects)-1].Name <= startAfter {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
for i := range objects {
|
|
|
|
if objects[i].Name > startAfter {
|
|
|
|
return objects[i:]
|
2021-07-28 13:28:05 +00:00
|
|
|
}
|
|
|
|
}
|
2021-07-29 08:14:14 +00:00
|
|
|
|
|
|
|
return nil
|
2021-07-28 13:28:05 +00:00
|
|
|
}
|
|
|
|
|
2021-08-27 21:33:50 +00:00
|
|
|
func trimAfterObjectID(id string, objects []*api.ObjectInfo) []*api.ObjectInfo {
|
|
|
|
if len(objects) != 0 && objects[len(objects)-1].ID.String() == id {
|
|
|
|
return []*api.ObjectInfo{}
|
2021-07-29 08:14:14 +00:00
|
|
|
}
|
2021-07-28 13:28:05 +00:00
|
|
|
for i, obj := range objects {
|
2021-08-27 21:33:50 +00:00
|
|
|
if obj.ID.String() == id {
|
2021-07-29 08:14:14 +00:00
|
|
|
return objects[i+1:]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-08-27 21:33:50 +00:00
|
|
|
func triageObjects(allObjects []*api.ObjectInfo) (prefixes []string, objects []*api.ObjectInfo) {
|
2021-07-29 08:14:14 +00:00
|
|
|
for _, ov := range allObjects {
|
2021-08-27 21:33:50 +00:00
|
|
|
if ov.IsDir {
|
2021-07-29 08:14:14 +00:00
|
|
|
prefixes = append(prefixes, ov.Name)
|
|
|
|
} else {
|
|
|
|
objects = append(objects, ov)
|
2021-07-28 13:28:05 +00:00
|
|
|
}
|
|
|
|
}
|
2021-07-29 08:14:14 +00:00
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-08-27 21:33:50 +00:00
|
|
|
func (n *layer) listAllObjects(ctx context.Context, p ListObjectsParamsCommon) ([]*api.ObjectInfo, error) {
|
2021-07-29 08:14:14 +00:00
|
|
|
var (
|
|
|
|
err error
|
2021-08-27 21:33:50 +00:00
|
|
|
bkt *api.BucketInfo
|
|
|
|
allObjects []*api.ObjectInfo
|
2021-07-29 08:14:14 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
if bkt, err = n.GetBucketInfo(ctx, p.Bucket); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-09-01 16:10:31 +00:00
|
|
|
allObjects, err = n.listSortedObjects(ctx, allObjectParams{
|
|
|
|
Bucket: bkt,
|
|
|
|
Prefix: p.Prefix,
|
|
|
|
Delimiter: p.Delimiter,
|
|
|
|
})
|
|
|
|
if err != nil {
|
2021-07-29 08:14:14 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return allObjects, nil
|
2021-07-28 13:28:05 +00:00
|
|
|
}
|
2021-08-10 08:58:40 +00:00
|
|
|
|
2021-08-27 21:33:50 +00:00
|
|
|
func (n *layer) isVersioningEnabled(ctx context.Context, bktInfo *api.BucketInfo) bool {
|
2021-08-10 08:58:40 +00:00
|
|
|
settings, err := n.getBucketSettings(ctx, bktInfo)
|
|
|
|
if err != nil {
|
|
|
|
n.log.Warn("couldn't get versioning settings object", zap.Error(err))
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
return settings.VersioningEnabled
|
|
|
|
}
|
2021-09-01 16:10:31 +00:00
|
|
|
|
|
|
|
func (n *layer) objectFromObjectsCacheOrNeoFS(ctx context.Context, cid *cid.ID, oid *object.ID) *object.Object {
|
|
|
|
var (
|
|
|
|
err error
|
|
|
|
meta = n.objCache.Get(newAddress(cid, oid))
|
|
|
|
)
|
|
|
|
if meta == nil {
|
|
|
|
meta, err = n.objectHead(ctx, cid, oid)
|
|
|
|
if err != nil {
|
|
|
|
n.log.Warn("could not fetch object meta", zap.Error(err))
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
if err = n.objCache.Put(*meta); err != nil {
|
|
|
|
n.log.Error("couldn't cache an object", zap.Error(err))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return meta
|
|
|
|
}
|