2020-07-03 13:52:04 +00:00
|
|
|
package layer
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2021-05-20 10:14:17 +00:00
|
|
|
"errors"
|
2021-08-10 08:19:09 +00:00
|
|
|
"fmt"
|
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-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 {
|
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
|
2021-07-20 12:40:38 +00:00
|
|
|
FetchOwner bool
|
2021-07-18 13:40:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
allObjectParams struct {
|
2021-07-29 08:14:14 +00:00
|
|
|
Bucket *BucketInfo
|
|
|
|
Delimiter string
|
|
|
|
Prefix string
|
2021-07-18 13:40:19 +00:00
|
|
|
}
|
2020-07-03 13:52:04 +00:00
|
|
|
)
|
|
|
|
|
2021-08-10 08:58:40 +00:00
|
|
|
const (
|
|
|
|
versionsDelAttr = "S3-Versions-del"
|
|
|
|
versionsAddAttr = "S3-Versions-add"
|
|
|
|
)
|
|
|
|
|
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-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
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
|
|
|
2021-08-10 08:19:09 +00:00
|
|
|
if p.Object == bktVersionSettingsObject {
|
|
|
|
return nil, fmt.Errorf("trying put bucket settings object")
|
|
|
|
}
|
|
|
|
|
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
|
2021-08-10 08:19:09 +00:00
|
|
|
}
|
|
|
|
if bkt, err = n.GetBucketInfo(ctx, p.Bucket); err != nil {
|
2020-07-03 13:52:04 +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-10 08:19:09 +00:00
|
|
|
lastVersionInfo, err := n.headLastVersion(ctx, bkt, p.Object)
|
|
|
|
if err != nil && !apiErrors.IsS3Error(err, apiErrors.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)
|
2021-08-10 08:19:09 +00:00
|
|
|
var idsToDeleteArr []*object.ID
|
|
|
|
if lastVersionInfo != nil {
|
2021-08-10 08:58:40 +00:00
|
|
|
if versioningEnabled {
|
|
|
|
versionsAddedStr := lastVersionInfo.Headers[versionsAddAttr]
|
|
|
|
if len(versionsAddedStr) != 0 {
|
|
|
|
versionsAddedStr += ","
|
|
|
|
}
|
|
|
|
versionsAddedStr += lastVersionInfo.ID().String()
|
|
|
|
addedVersions := object.NewAttribute()
|
|
|
|
addedVersions.SetKey(versionsAddAttr)
|
|
|
|
addedVersions.SetValue(versionsAddedStr)
|
|
|
|
attributes = append(attributes, addedVersions)
|
|
|
|
if delVersions := lastVersionInfo.Headers[versionsDelAttr]; len(delVersions) > 0 {
|
|
|
|
deletedVersions := object.NewAttribute()
|
|
|
|
deletedVersions.SetKey(versionsDelAttr)
|
|
|
|
deletedVersions.SetValue(delVersions)
|
|
|
|
attributes = append(attributes, deletedVersions)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
versionsDeletedStr := lastVersionInfo.Headers[versionsDelAttr]
|
|
|
|
if len(versionsDeletedStr) != 0 {
|
|
|
|
versionsDeletedStr += ","
|
|
|
|
}
|
|
|
|
versionsDeletedStr += lastVersionInfo.ID().String()
|
|
|
|
deletedVersions := object.NewAttribute()
|
|
|
|
deletedVersions.SetKey(versionsDelAttr)
|
|
|
|
deletedVersions.SetValue(versionsDeletedStr)
|
2021-08-10 08:19:09 +00:00
|
|
|
|
2021-08-10 08:58:40 +00:00
|
|
|
attributes = append(attributes, deletedVersions)
|
|
|
|
idsToDeleteArr = append(idsToDeleteArr, lastVersionInfo.ID())
|
|
|
|
}
|
2021-08-10 08:19:09 +00:00
|
|
|
}
|
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
|
|
|
|
}
|
|
|
|
|
2021-08-10 11:19:26 +00:00
|
|
|
if err = n.objCache.Put(addr, *meta); err != nil {
|
|
|
|
n.log.Error("couldn't cache an object", zap.Error(err))
|
|
|
|
}
|
|
|
|
|
2021-08-10 08:19:09 +00:00
|
|
|
objInfo := &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(),
|
2021-08-10 08:19:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, id := range idsToDeleteArr {
|
|
|
|
addr := object.NewAddress()
|
|
|
|
addr.SetObjectID(id)
|
|
|
|
addr.SetContainerID(bkt.CID)
|
|
|
|
|
|
|
|
if err = n.objectDelete(ctx, addr); err != nil {
|
|
|
|
n.log.Warn("couldn't delete object",
|
|
|
|
zap.Stringer("version id", id),
|
|
|
|
zap.Error(err))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return objInfo, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *layer) headLastVersion(ctx context.Context, bkt *BucketInfo, objectName string) (*ObjectInfo, error) {
|
|
|
|
ids, err := n.objectSearch(ctx, &findParams{cid: bkt.CID, val: objectName})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(ids) == 0 {
|
|
|
|
return nil, api.GetAPIError(api.ErrNoSuchKey)
|
|
|
|
}
|
|
|
|
|
|
|
|
infos := make([]*object.Object, 0, len(ids))
|
|
|
|
for _, id := range ids {
|
|
|
|
addr := object.NewAddress()
|
|
|
|
addr.SetContainerID(bkt.CID)
|
|
|
|
addr.SetObjectID(id)
|
|
|
|
meta, err := n.objectHead(ctx, addr)
|
|
|
|
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
|
|
|
|
}
|
|
|
|
infos = append(infos, meta)
|
|
|
|
}
|
|
|
|
|
|
|
|
sort.Slice(infos, func(i, j int) bool {
|
|
|
|
return infos[i].CreationEpoch() < infos[j].CreationEpoch() || (infos[i].CreationEpoch() == infos[j].CreationEpoch() && infos[i].ID().String() < infos[j].ID().String())
|
|
|
|
})
|
|
|
|
|
|
|
|
return objectInfoFromMeta(bkt, infos[len(infos)-1], "", ""), nil
|
2021-08-10 08:58:40 +00:00
|
|
|
//versionsToDeleteStr, ok := lastVersionInfo.Headers[versionsAddAttr]
|
|
|
|
//versionsDeletedStr := lastVersionInfo.Headers[versionsDelAttr]
|
2021-08-10 08:19:09 +00:00
|
|
|
//idsToDeleteArr := []*object.ID{lastVersionInfo.ID()}
|
|
|
|
//if ok {
|
|
|
|
// // for versioning mode only
|
|
|
|
// idsToDelete := strings.Split(versionsToDeleteStr, ",")
|
|
|
|
// for _, idStr := range idsToDelete {
|
|
|
|
// oid := object.NewID()
|
|
|
|
// if err = oid.Parse(idStr); err != nil {
|
|
|
|
// n.log.Warn("couldn't parse object id versions list",
|
|
|
|
// zap.String("versions id", versionsToDeleteStr),
|
|
|
|
// zap.Error(err))
|
|
|
|
// break
|
|
|
|
// }
|
|
|
|
// idsToDeleteArr = append(idsToDeleteArr, oid)
|
|
|
|
// }
|
|
|
|
//}
|
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-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
|
|
|
|
allObjects []*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
|
|
|
|
allObjects []*ObjectInfo
|
|
|
|
)
|
|
|
|
|
|
|
|
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-07-29 08:14:14 +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-07-29 08:14:14 +00:00
|
|
|
func (n *layer) listSortedObjectsFromNeoFS(ctx context.Context, p allObjectParams) ([]*ObjectInfo, error) {
|
2021-07-18 13:40:19 +00:00
|
|
|
var (
|
|
|
|
err error
|
|
|
|
ids []*object.ID
|
|
|
|
uniqNames = make(map[string]bool)
|
|
|
|
)
|
|
|
|
|
2021-07-22 08:57:37 +00:00
|
|
|
if ids, err = n.objectSearch(ctx, &findParams{cid: p.Bucket.CID}); err != nil {
|
2021-07-18 13:40:19 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
objects := make([]*ObjectInfo, 0, len(ids))
|
|
|
|
|
|
|
|
for _, id := range ids {
|
|
|
|
addr := object.NewAddress()
|
|
|
|
addr.SetObjectID(id)
|
2021-07-22 08:57:37 +00:00
|
|
|
addr.SetContainerID(p.Bucket.CID)
|
2021-07-18 13:40:19 +00:00
|
|
|
|
|
|
|
meta, err := n.objectHead(ctx, addr)
|
|
|
|
if err != nil {
|
|
|
|
n.log.Warn("could not fetch object meta", zap.Error(err))
|
|
|
|
continue
|
|
|
|
}
|
2021-07-22 08:57:37 +00:00
|
|
|
if oi := objectInfoFromMeta(p.Bucket, meta, p.Prefix, p.Delimiter); oi != nil {
|
2021-07-18 13:40:19 +00:00
|
|
|
// use only unique dir names
|
|
|
|
if _, ok := uniqNames[oi.Name]; ok {
|
|
|
|
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
|
|
|
|
}
|
2021-07-28 13:28:05 +00:00
|
|
|
|
2021-07-29 08:14:14 +00:00
|
|
|
func trimAfterObjectName(startAfter string, objects []*ObjectInfo) []*ObjectInfo {
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
func trimAfterObjectID(id string, objects []*ObjectInfo) []*ObjectInfo {
|
2021-07-29 08:14:14 +00:00
|
|
|
if len(objects) != 0 && objects[len(objects)-1].id.String() == id {
|
|
|
|
return []*ObjectInfo{}
|
|
|
|
}
|
2021-07-28 13:28:05 +00:00
|
|
|
for i, obj := range objects {
|
|
|
|
if obj.ID().String() == id {
|
2021-07-29 08:14:14 +00:00
|
|
|
return objects[i+1:]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func triageObjects(allObjects []*ObjectInfo) (prefixes []string, objects []*ObjectInfo) {
|
|
|
|
for _, ov := range allObjects {
|
|
|
|
if ov.isDir {
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *layer) listAllObjects(ctx context.Context, p ListObjectsParamsCommon) ([]*ObjectInfo, error) {
|
|
|
|
var (
|
|
|
|
err error
|
|
|
|
bkt *BucketInfo
|
|
|
|
cacheKey cacheOptions
|
|
|
|
allObjects []*ObjectInfo
|
|
|
|
)
|
|
|
|
|
|
|
|
if bkt, err = n.GetBucketInfo(ctx, p.Bucket); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if cacheKey, err = createKey(ctx, bkt.CID, p.Prefix, p.Delimiter); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-08-10 12:23:55 +00:00
|
|
|
allObjects = n.listObjCache.Get(cacheKey)
|
2021-07-29 08:14:14 +00:00
|
|
|
|
|
|
|
if allObjects == nil {
|
|
|
|
allObjects, err = n.listSortedObjectsFromNeoFS(ctx, allObjectParams{
|
|
|
|
Bucket: bkt,
|
|
|
|
Prefix: p.Prefix,
|
|
|
|
Delimiter: p.Delimiter,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// putting to cache a copy of allObjects because allObjects can be modified further
|
2021-08-10 12:23:55 +00:00
|
|
|
n.listObjCache.Put(cacheKey, append([]*ObjectInfo(nil), allObjects...))
|
2021-07-29 08:14:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return allObjects, nil
|
2021-07-28 13:28:05 +00:00
|
|
|
}
|
2021-08-10 08:58:40 +00:00
|
|
|
|
|
|
|
func (n *layer) isVersioningEnabled(ctx context.Context, bktInfo *BucketInfo) bool {
|
|
|
|
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
|
|
|
|
}
|