2021-11-01 08:35:33 +00:00
|
|
|
package internal
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"crypto/ecdsa"
|
2022-02-25 09:20:49 +00:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
2021-11-01 08:35:33 +00:00
|
|
|
|
2022-01-13 15:01:50 +00:00
|
|
|
coreclient "github.com/nspcc-dev/neofs-node/pkg/core/client"
|
2022-05-12 07:22:02 +00:00
|
|
|
"github.com/nspcc-dev/neofs-sdk-go/bearer"
|
2021-11-10 07:08:33 +00:00
|
|
|
"github.com/nspcc-dev/neofs-sdk-go/client"
|
2021-11-06 11:13:04 +00:00
|
|
|
apistatus "github.com/nspcc-dev/neofs-sdk-go/client/status"
|
2021-11-10 07:08:33 +00:00
|
|
|
cid "github.com/nspcc-dev/neofs-sdk-go/container/id"
|
|
|
|
"github.com/nspcc-dev/neofs-sdk-go/object"
|
2022-01-26 12:11:13 +00:00
|
|
|
addressSDK "github.com/nspcc-dev/neofs-sdk-go/object/address"
|
|
|
|
oidSDK "github.com/nspcc-dev/neofs-sdk-go/object/id"
|
2021-11-10 07:08:33 +00:00
|
|
|
"github.com/nspcc-dev/neofs-sdk-go/session"
|
2021-11-01 08:35:33 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type commonPrm struct {
|
2022-01-13 15:01:50 +00:00
|
|
|
cli coreclient.Client
|
2021-11-01 08:35:33 +00:00
|
|
|
|
|
|
|
ctx context.Context
|
|
|
|
|
2022-02-25 09:20:49 +00:00
|
|
|
key *ecdsa.PrivateKey
|
|
|
|
|
|
|
|
tokenSession *session.Token
|
|
|
|
|
2022-05-12 07:22:02 +00:00
|
|
|
tokenBearer *bearer.Token
|
2022-02-25 09:20:49 +00:00
|
|
|
|
|
|
|
local bool
|
2022-03-03 12:30:20 +00:00
|
|
|
|
|
|
|
xHeaders []*session.XHeader
|
2021-11-01 08:35:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// SetClient sets base client for NeoFS API communication.
|
|
|
|
//
|
|
|
|
// Required parameter.
|
2022-01-13 15:01:50 +00:00
|
|
|
func (x *commonPrm) SetClient(cli coreclient.Client) {
|
2021-11-01 08:35:33 +00:00
|
|
|
x.cli = cli
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetContext sets context.Context for network communication.
|
|
|
|
//
|
|
|
|
// Required parameter.
|
|
|
|
func (x *commonPrm) SetContext(ctx context.Context) {
|
|
|
|
x.ctx = ctx
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetPrivateKey sets private key to sign the request(s).
|
|
|
|
//
|
|
|
|
// Required parameter.
|
|
|
|
func (x *commonPrm) SetPrivateKey(key *ecdsa.PrivateKey) {
|
2022-02-25 09:20:49 +00:00
|
|
|
x.key = key
|
2021-11-01 08:35:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// SetSessionToken sets token of the session within which request should be sent.
|
|
|
|
//
|
|
|
|
// By default the request will be sent outside the session.
|
|
|
|
func (x *commonPrm) SetSessionToken(tok *session.Token) {
|
2022-02-25 09:20:49 +00:00
|
|
|
x.tokenSession = tok
|
2021-11-01 08:35:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// SetBearerToken sets bearer token to be attached to the request.
|
|
|
|
//
|
|
|
|
// By default token is not attached to the request.
|
2022-05-12 07:22:02 +00:00
|
|
|
func (x *commonPrm) SetBearerToken(tok *bearer.Token) {
|
2022-02-25 09:20:49 +00:00
|
|
|
x.tokenBearer = tok
|
2021-11-01 08:35:33 +00:00
|
|
|
}
|
|
|
|
|
2022-01-29 13:33:11 +00:00
|
|
|
// SetTTL sets time-to-live call option.
|
|
|
|
func (x *commonPrm) SetTTL(ttl uint32) {
|
2022-02-25 09:20:49 +00:00
|
|
|
x.local = ttl < 2
|
2022-01-29 13:33:11 +00:00
|
|
|
}
|
|
|
|
|
2021-11-01 08:35:33 +00:00
|
|
|
// SetXHeaders sets request X-Headers.
|
|
|
|
//
|
|
|
|
// By default X-Headers will not be attached to the request.
|
2022-03-03 12:30:20 +00:00
|
|
|
func (x *commonPrm) SetXHeaders(hs []*session.XHeader) {
|
|
|
|
x.xHeaders = hs
|
|
|
|
}
|
|
|
|
|
|
|
|
func (x commonPrm) xHeadersPrm() (res []string) {
|
|
|
|
if len(x.xHeaders) > 0 {
|
|
|
|
res = make([]string, len(x.xHeaders)*2)
|
|
|
|
|
|
|
|
for i := range x.xHeaders {
|
|
|
|
res[2*i] = x.xHeaders[i].Key()
|
|
|
|
res[2*i+1] = x.xHeaders[i].Value()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
2021-11-01 08:35:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type readPrmCommon struct {
|
|
|
|
commonPrm
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetNetmapEpoch sets the epoch number to be used to locate the object.
|
|
|
|
//
|
|
|
|
// By default current epoch on the server will be used.
|
2022-02-25 09:20:49 +00:00
|
|
|
func (x *readPrmCommon) SetNetmapEpoch(_ uint64) {
|
|
|
|
// FIXME: (neofs-node#1194) not supported by client
|
2021-11-01 08:35:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetObjectPrm groups parameters of GetObject operation.
|
|
|
|
type GetObjectPrm struct {
|
|
|
|
readPrmCommon
|
|
|
|
|
2022-02-25 09:20:49 +00:00
|
|
|
cliPrm client.PrmObjectGet
|
2021-11-01 08:35:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// SetRawFlag sets raw flag of the request.
|
|
|
|
//
|
|
|
|
// By default request will not be raw.
|
|
|
|
func (x *GetObjectPrm) SetRawFlag() {
|
2022-02-25 09:20:49 +00:00
|
|
|
x.cliPrm.MarkRaw()
|
2021-11-01 08:35:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// SetAddress sets object address.
|
|
|
|
//
|
|
|
|
// Required parameter.
|
2022-01-26 12:11:13 +00:00
|
|
|
func (x *GetObjectPrm) SetAddress(addr *addressSDK.Address) {
|
2022-02-25 09:20:49 +00:00
|
|
|
if id := addr.ContainerID(); id != nil {
|
|
|
|
x.cliPrm.FromContainer(*id)
|
|
|
|
}
|
|
|
|
|
|
|
|
if id := addr.ObjectID(); id != nil {
|
|
|
|
x.cliPrm.ByID(*id)
|
|
|
|
}
|
2021-11-01 08:35:33 +00:00
|
|
|
}
|
|
|
|
|
2022-04-21 11:28:05 +00:00
|
|
|
// GetObjectRes groups the resulting values of GetObject operation.
|
2021-11-01 08:35:33 +00:00
|
|
|
type GetObjectRes struct {
|
2022-02-25 09:20:49 +00:00
|
|
|
obj *object.Object
|
2021-11-01 08:35:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Object returns requested object.
|
|
|
|
func (x GetObjectRes) Object() *object.Object {
|
2022-02-25 09:20:49 +00:00
|
|
|
return x.obj
|
2021-11-01 08:35:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetObject reads the object by address.
|
|
|
|
//
|
|
|
|
// Client, context and key must be set.
|
|
|
|
//
|
2022-04-21 11:28:05 +00:00
|
|
|
// Returns any error which prevented the operation from completing correctly in error return.
|
2021-11-01 08:35:33 +00:00
|
|
|
// Returns:
|
2022-03-17 08:03:58 +00:00
|
|
|
// error of type *object.SplitInfoError if object raw flag is set and requested object is virtual;
|
2022-04-21 11:28:05 +00:00
|
|
|
// error of type *apistatus.ObjectAlreadyRemoved if the requested object is marked to be removed.
|
2022-02-25 09:20:49 +00:00
|
|
|
func GetObject(prm GetObjectPrm) (*GetObjectRes, error) {
|
|
|
|
if prm.tokenSession != nil {
|
|
|
|
prm.cliPrm.WithinSession(*prm.tokenSession)
|
|
|
|
}
|
|
|
|
|
|
|
|
if prm.tokenBearer != nil {
|
|
|
|
prm.cliPrm.WithBearerToken(*prm.tokenBearer)
|
|
|
|
}
|
|
|
|
|
|
|
|
if prm.local {
|
|
|
|
prm.cliPrm.MarkLocal()
|
|
|
|
}
|
|
|
|
|
2022-03-03 12:30:20 +00:00
|
|
|
prm.cliPrm.WithXHeaders(prm.xHeadersPrm()...)
|
|
|
|
|
2022-02-25 09:20:49 +00:00
|
|
|
rdr, err := prm.cli.ObjectGetInit(prm.ctx, prm.cliPrm)
|
2022-03-03 15:01:44 +00:00
|
|
|
if err != nil {
|
2022-02-25 09:20:49 +00:00
|
|
|
return nil, fmt.Errorf("init object reading: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if prm.key != nil {
|
|
|
|
rdr.UseKey(*prm.key)
|
|
|
|
}
|
|
|
|
|
|
|
|
var obj object.Object
|
|
|
|
|
|
|
|
if !rdr.ReadHeader(&obj) {
|
|
|
|
res, err := rdr.Close()
|
|
|
|
if err == nil {
|
|
|
|
// pull out an error from status
|
|
|
|
err = apistatus.ErrFromStatus(res.Status())
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil, fmt.Errorf("read object header: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
buf := make([]byte, obj.PayloadSize())
|
|
|
|
|
|
|
|
_, err = rdr.Read(buf)
|
|
|
|
if err != nil && !errors.Is(err, io.EOF) {
|
|
|
|
return nil, fmt.Errorf("read payload: %w", err)
|
2021-11-06 11:13:04 +00:00
|
|
|
}
|
2021-11-01 08:35:33 +00:00
|
|
|
|
2022-03-03 14:19:05 +00:00
|
|
|
obj.SetPayload(buf)
|
2022-02-25 09:20:49 +00:00
|
|
|
|
|
|
|
return &GetObjectRes{
|
|
|
|
obj: &obj,
|
|
|
|
}, nil
|
2021-11-01 08:35:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// HeadObjectPrm groups parameters of HeadObject operation.
|
|
|
|
type HeadObjectPrm struct {
|
|
|
|
readPrmCommon
|
|
|
|
|
2022-02-25 09:20:49 +00:00
|
|
|
cliPrm client.PrmObjectHead
|
2021-11-01 08:35:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// SetRawFlag sets raw flag of the request.
|
|
|
|
//
|
|
|
|
// By default request will not be raw.
|
|
|
|
func (x *HeadObjectPrm) SetRawFlag() {
|
2022-02-25 09:20:49 +00:00
|
|
|
x.cliPrm.MarkRaw()
|
2021-11-01 08:35:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// SetAddress sets object address.
|
|
|
|
//
|
|
|
|
// Required parameter.
|
2022-01-26 12:11:13 +00:00
|
|
|
func (x *HeadObjectPrm) SetAddress(addr *addressSDK.Address) {
|
2022-02-25 09:20:49 +00:00
|
|
|
if id := addr.ContainerID(); id != nil {
|
|
|
|
x.cliPrm.FromContainer(*id)
|
|
|
|
}
|
|
|
|
|
|
|
|
if id := addr.ObjectID(); id != nil {
|
|
|
|
x.cliPrm.ByID(*id)
|
|
|
|
}
|
2021-11-01 08:35:33 +00:00
|
|
|
}
|
|
|
|
|
2022-04-21 11:28:05 +00:00
|
|
|
// HeadObjectRes groups the resulting values of GetObject operation.
|
2021-11-01 08:35:33 +00:00
|
|
|
type HeadObjectRes struct {
|
2022-02-25 09:20:49 +00:00
|
|
|
hdr *object.Object
|
2021-11-01 08:35:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Header returns requested object header.
|
|
|
|
func (x HeadObjectRes) Header() *object.Object {
|
2022-02-25 09:20:49 +00:00
|
|
|
return x.hdr
|
2021-11-01 08:35:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// HeadObject reads object header by address.
|
|
|
|
//
|
|
|
|
// Client, context and key must be set.
|
|
|
|
//
|
2022-04-21 11:28:05 +00:00
|
|
|
// Returns any error which prevented the operation from completing correctly in error return.
|
2021-11-01 08:35:33 +00:00
|
|
|
// Returns:
|
2022-03-17 08:03:58 +00:00
|
|
|
// error of type *object.SplitInfoError if object raw flag is set and requested object is virtual;
|
2022-04-21 11:28:05 +00:00
|
|
|
// error of type *apistatus.ObjectAlreadyRemoved if the requested object is marked to be removed.
|
2022-02-25 09:20:49 +00:00
|
|
|
func HeadObject(prm HeadObjectPrm) (*HeadObjectRes, error) {
|
|
|
|
if prm.local {
|
|
|
|
prm.cliPrm.MarkLocal()
|
|
|
|
}
|
|
|
|
|
|
|
|
if prm.tokenSession != nil {
|
|
|
|
prm.cliPrm.WithinSession(*prm.tokenSession)
|
|
|
|
}
|
|
|
|
|
|
|
|
if prm.tokenBearer != nil {
|
|
|
|
prm.cliPrm.WithBearerToken(*prm.tokenBearer)
|
|
|
|
}
|
|
|
|
|
2022-03-03 12:30:20 +00:00
|
|
|
prm.cliPrm.WithXHeaders(prm.xHeadersPrm()...)
|
|
|
|
|
2022-02-25 09:20:49 +00:00
|
|
|
cliRes, err := prm.cli.ObjectHead(prm.ctx, prm.cliPrm)
|
2021-11-06 11:13:04 +00:00
|
|
|
if err == nil {
|
|
|
|
// pull out an error from status
|
2022-02-25 09:20:49 +00:00
|
|
|
err = apistatus.ErrFromStatus(cliRes.Status())
|
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("read object header from NeoFS: %w", err)
|
2021-11-06 11:13:04 +00:00
|
|
|
}
|
2021-11-01 08:35:33 +00:00
|
|
|
|
2022-02-25 09:20:49 +00:00
|
|
|
var hdr object.Object
|
|
|
|
|
|
|
|
if !cliRes.ReadHeader(&hdr) {
|
|
|
|
return nil, errors.New("missing object header in the response")
|
|
|
|
}
|
|
|
|
|
|
|
|
return &HeadObjectRes{
|
|
|
|
hdr: &hdr,
|
|
|
|
}, nil
|
2021-11-01 08:35:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// PayloadRangePrm groups parameters of PayloadRange operation.
|
|
|
|
type PayloadRangePrm struct {
|
|
|
|
readPrmCommon
|
|
|
|
|
2022-02-25 09:20:49 +00:00
|
|
|
ln uint64
|
|
|
|
|
|
|
|
cliPrm client.PrmObjectRange
|
2021-11-01 08:35:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// SetRawFlag sets raw flag of the request.
|
|
|
|
//
|
|
|
|
// By default request will not be raw.
|
|
|
|
func (x *PayloadRangePrm) SetRawFlag() {
|
2022-02-25 09:20:49 +00:00
|
|
|
x.cliPrm.MarkRaw()
|
2021-11-01 08:35:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// SetAddress sets object address.
|
|
|
|
//
|
|
|
|
// Required parameter.
|
2022-01-26 12:11:13 +00:00
|
|
|
func (x *PayloadRangePrm) SetAddress(addr *addressSDK.Address) {
|
2022-02-25 09:20:49 +00:00
|
|
|
if id := addr.ContainerID(); id != nil {
|
|
|
|
x.cliPrm.FromContainer(*id)
|
|
|
|
}
|
|
|
|
|
|
|
|
if id := addr.ObjectID(); id != nil {
|
|
|
|
x.cliPrm.ByID(*id)
|
|
|
|
}
|
2021-11-01 08:35:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// SetRange range of the object payload to be read.
|
|
|
|
//
|
|
|
|
// Required parameter.
|
|
|
|
func (x *PayloadRangePrm) SetRange(rng *object.Range) {
|
2022-02-25 09:20:49 +00:00
|
|
|
x.cliPrm.SetOffset(rng.GetOffset())
|
|
|
|
x.ln = rng.GetLength()
|
2021-11-01 08:35:33 +00:00
|
|
|
}
|
|
|
|
|
2022-04-21 11:28:05 +00:00
|
|
|
// PayloadRangeRes groups the resulting values of GetObject operation.
|
2021-11-01 08:35:33 +00:00
|
|
|
type PayloadRangeRes struct {
|
2022-02-25 09:20:49 +00:00
|
|
|
data []byte
|
2021-11-01 08:35:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// PayloadRange returns data of the requested payload range.
|
|
|
|
func (x PayloadRangeRes) PayloadRange() []byte {
|
2022-02-25 09:20:49 +00:00
|
|
|
return x.data
|
2021-11-01 08:35:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// PayloadRange reads object payload range by address.
|
|
|
|
//
|
|
|
|
// Client, context and key must be set.
|
|
|
|
//
|
2022-04-21 11:28:05 +00:00
|
|
|
// Returns any error which prevented the operation from completing correctly in error return.
|
2021-11-01 08:35:33 +00:00
|
|
|
// Returns:
|
2022-03-17 08:03:58 +00:00
|
|
|
// error of type *object.SplitInfoError if object raw flag is set and requested object is virtual;
|
2022-04-21 11:28:05 +00:00
|
|
|
// error of type *apistatus.ObjectAlreadyRemoved if the requested object is marked to be removed.
|
2022-02-25 09:20:49 +00:00
|
|
|
func PayloadRange(prm PayloadRangePrm) (*PayloadRangeRes, error) {
|
|
|
|
if prm.local {
|
|
|
|
prm.cliPrm.MarkLocal()
|
|
|
|
}
|
|
|
|
|
|
|
|
if prm.tokenSession != nil {
|
|
|
|
prm.cliPrm.WithinSession(*prm.tokenSession)
|
|
|
|
}
|
|
|
|
|
|
|
|
if prm.tokenBearer != nil {
|
|
|
|
prm.cliPrm.WithBearerToken(*prm.tokenBearer)
|
|
|
|
}
|
|
|
|
|
|
|
|
prm.cliPrm.SetLength(prm.ln)
|
2022-03-03 12:30:20 +00:00
|
|
|
prm.cliPrm.WithXHeaders(prm.xHeadersPrm()...)
|
2022-02-25 09:20:49 +00:00
|
|
|
|
|
|
|
rdr, err := prm.cli.ObjectRangeInit(prm.ctx, prm.cliPrm)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("init payload reading: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
data := make([]byte, prm.ln)
|
|
|
|
|
|
|
|
_, err = io.ReadFull(rdr, data)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("read payload: %w", err)
|
2021-11-06 11:13:04 +00:00
|
|
|
}
|
2021-11-01 08:35:33 +00:00
|
|
|
|
2022-02-25 09:20:49 +00:00
|
|
|
return &PayloadRangeRes{
|
|
|
|
data: data,
|
|
|
|
}, nil
|
2021-11-01 08:35:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// PutObjectPrm groups parameters of PutObject operation.
|
|
|
|
type PutObjectPrm struct {
|
|
|
|
commonPrm
|
|
|
|
|
2022-02-25 09:20:49 +00:00
|
|
|
obj *object.Object
|
2021-11-01 08:35:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// SetObject sets object to be stored.
|
|
|
|
//
|
|
|
|
// Required parameter.
|
|
|
|
func (x *PutObjectPrm) SetObject(obj *object.Object) {
|
2022-02-25 09:20:49 +00:00
|
|
|
x.obj = obj
|
2021-11-01 08:35:33 +00:00
|
|
|
}
|
|
|
|
|
2022-04-21 11:28:05 +00:00
|
|
|
// PutObjectRes groups the resulting values of PutObject operation.
|
2021-11-01 08:35:33 +00:00
|
|
|
type PutObjectRes struct {
|
2022-02-25 09:20:49 +00:00
|
|
|
id *oidSDK.ID
|
2021-11-01 08:35:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ID returns identifier of the stored object.
|
2022-01-26 12:11:13 +00:00
|
|
|
func (x PutObjectRes) ID() *oidSDK.ID {
|
2022-02-25 09:20:49 +00:00
|
|
|
return x.id
|
2021-11-01 08:35:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// PutObject saves the object in local storage of the remote node.
|
|
|
|
//
|
|
|
|
// Client, context and key must be set.
|
2021-11-03 17:13:16 +00:00
|
|
|
//
|
2022-04-21 11:28:05 +00:00
|
|
|
// Returns any error which prevented the operation from completing correctly in error return.
|
2022-02-25 09:20:49 +00:00
|
|
|
func PutObject(prm PutObjectPrm) (*PutObjectRes, error) {
|
|
|
|
var prmCli client.PrmObjectPutInit
|
|
|
|
|
|
|
|
w, err := prm.cli.ObjectPutInit(prm.ctx, prmCli)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("init object writing on client: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
w.MarkLocal()
|
|
|
|
|
|
|
|
if prm.key != nil {
|
|
|
|
w.UseKey(*prm.key)
|
|
|
|
}
|
|
|
|
|
|
|
|
if prm.tokenSession != nil {
|
|
|
|
w.WithinSession(*prm.tokenSession)
|
|
|
|
}
|
|
|
|
|
|
|
|
if prm.tokenBearer != nil {
|
|
|
|
w.WithBearerToken(*prm.tokenBearer)
|
|
|
|
}
|
|
|
|
|
2022-03-03 12:30:20 +00:00
|
|
|
w.WithXHeaders(prm.xHeadersPrm()...)
|
|
|
|
|
2022-02-25 09:20:49 +00:00
|
|
|
if w.WriteHeader(*prm.obj) {
|
|
|
|
w.WritePayloadChunk(prm.obj.Payload())
|
|
|
|
}
|
|
|
|
|
|
|
|
res, err := w.Close()
|
2021-11-06 11:13:04 +00:00
|
|
|
if err == nil {
|
2022-02-25 09:20:49 +00:00
|
|
|
err = apistatus.ErrFromStatus(res.Status())
|
2021-11-06 11:13:04 +00:00
|
|
|
}
|
2021-11-01 08:35:33 +00:00
|
|
|
|
2022-02-25 09:20:49 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("write object via client: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
var id oidSDK.ID
|
|
|
|
if !res.ReadStoredObjectID(&id) {
|
|
|
|
return nil, errors.New("missing identifier in the response")
|
|
|
|
}
|
|
|
|
|
|
|
|
return &PutObjectRes{
|
|
|
|
id: &id,
|
|
|
|
}, nil
|
2021-11-01 08:35:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// SearchObjectsPrm groups parameters of SearchObjects operation.
|
|
|
|
type SearchObjectsPrm struct {
|
|
|
|
readPrmCommon
|
|
|
|
|
2022-02-25 09:20:49 +00:00
|
|
|
cliPrm client.PrmObjectSearch
|
2021-11-01 08:35:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// SetContainerID sets identifier of the container to search the objects.
|
|
|
|
//
|
|
|
|
// Required parameter.
|
|
|
|
func (x *SearchObjectsPrm) SetContainerID(id *cid.ID) {
|
2022-02-25 09:20:49 +00:00
|
|
|
if id != nil {
|
|
|
|
x.cliPrm.InContainer(*id)
|
|
|
|
}
|
2021-11-01 08:35:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// SetFilters sets search filters.
|
|
|
|
func (x *SearchObjectsPrm) SetFilters(fs object.SearchFilters) {
|
2022-02-25 09:20:49 +00:00
|
|
|
x.cliPrm.SetFilters(fs)
|
2021-11-01 08:35:33 +00:00
|
|
|
}
|
|
|
|
|
2022-04-21 11:28:05 +00:00
|
|
|
// SearchObjectsRes groups the resulting values of SearchObjects operation.
|
2021-11-01 08:35:33 +00:00
|
|
|
type SearchObjectsRes struct {
|
2022-03-15 12:11:35 +00:00
|
|
|
ids []oidSDK.ID
|
2021-11-01 08:35:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// IDList returns identifiers of the matched objects.
|
2022-03-15 12:11:35 +00:00
|
|
|
func (x SearchObjectsRes) IDList() []oidSDK.ID {
|
2022-02-25 09:20:49 +00:00
|
|
|
return x.ids
|
2021-11-01 08:35:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// SearchObjects selects objects from container which match the filters.
|
2021-11-03 17:13:16 +00:00
|
|
|
//
|
2022-04-21 11:28:05 +00:00
|
|
|
// Returns any error which prevented the operation from completing correctly in error return.
|
2022-02-25 09:20:49 +00:00
|
|
|
func SearchObjects(prm SearchObjectsPrm) (*SearchObjectsRes, error) {
|
|
|
|
if prm.local {
|
|
|
|
prm.cliPrm.MarkLocal()
|
|
|
|
}
|
|
|
|
|
|
|
|
if prm.tokenSession != nil {
|
|
|
|
prm.cliPrm.WithinSession(*prm.tokenSession)
|
|
|
|
}
|
|
|
|
|
|
|
|
if prm.tokenBearer != nil {
|
|
|
|
prm.cliPrm.WithBearerToken(*prm.tokenBearer)
|
|
|
|
}
|
|
|
|
|
2022-03-03 12:30:20 +00:00
|
|
|
prm.cliPrm.WithXHeaders(prm.xHeadersPrm()...)
|
|
|
|
|
2022-02-25 09:20:49 +00:00
|
|
|
rdr, err := prm.cli.ObjectSearchInit(prm.ctx, prm.cliPrm)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("init object searching in client: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if prm.key != nil {
|
|
|
|
rdr.UseKey(*prm.key)
|
|
|
|
}
|
|
|
|
|
|
|
|
buf := make([]oidSDK.ID, 10)
|
2022-03-15 12:11:35 +00:00
|
|
|
var ids []oidSDK.ID
|
2022-02-25 09:20:49 +00:00
|
|
|
var n int
|
|
|
|
var ok bool
|
|
|
|
|
|
|
|
for {
|
|
|
|
n, ok = rdr.Read(buf)
|
|
|
|
if n > 0 {
|
|
|
|
for i := range buf[:n] {
|
|
|
|
v := buf[i]
|
2022-03-15 12:11:35 +00:00
|
|
|
ids = append(ids, v)
|
2022-02-25 09:20:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if !ok {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
res, err := rdr.Close()
|
2021-11-06 11:13:04 +00:00
|
|
|
if err == nil {
|
|
|
|
// pull out an error from status
|
2022-02-25 09:20:49 +00:00
|
|
|
err = apistatus.ErrFromStatus(res.Status())
|
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("read object list: %w", err)
|
2021-11-06 11:13:04 +00:00
|
|
|
}
|
2021-11-01 08:35:33 +00:00
|
|
|
|
2022-02-25 09:20:49 +00:00
|
|
|
return &SearchObjectsRes{
|
|
|
|
ids: ids,
|
|
|
|
}, nil
|
2021-11-01 08:35:33 +00:00
|
|
|
}
|