2021-11-01 08:35:33 +00:00
|
|
|
package internal
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"crypto/ecdsa"
|
|
|
|
"strconv"
|
|
|
|
|
|
|
|
session2 "github.com/nspcc-dev/neofs-api-go/v2/session"
|
2022-01-13 15:01:50 +00:00
|
|
|
coreclient "github.com/nspcc-dev/neofs-node/pkg/core/client"
|
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"
|
|
|
|
"github.com/nspcc-dev/neofs-sdk-go/token"
|
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
|
|
|
|
|
|
|
|
opts []client.CallOption
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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) {
|
|
|
|
x.opts = append(x.opts, client.WithKey(key))
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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) {
|
|
|
|
x.opts = append(x.opts, client.WithSession(tok))
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetBearerToken sets bearer token to be attached to the request.
|
|
|
|
//
|
|
|
|
// By default token is not attached to the request.
|
|
|
|
func (x *commonPrm) SetBearerToken(tok *token.BearerToken) {
|
|
|
|
x.opts = append(x.opts, client.WithBearer(tok))
|
|
|
|
}
|
|
|
|
|
2022-01-29 13:33:11 +00:00
|
|
|
// SetTTL sets time-to-live call option.
|
|
|
|
func (x *commonPrm) SetTTL(ttl uint32) {
|
|
|
|
x.opts = append(x.opts, client.WithTTL(ttl))
|
|
|
|
}
|
|
|
|
|
2021-11-01 08:35:33 +00:00
|
|
|
// SetXHeaders sets request X-Headers.
|
|
|
|
//
|
|
|
|
// By default X-Headers will not be attached to the request.
|
2021-11-10 07:08:33 +00:00
|
|
|
func (x *commonPrm) SetXHeaders(xhdrs []*session.XHeader) {
|
2021-11-01 08:35:33 +00:00
|
|
|
for _, xhdr := range xhdrs {
|
|
|
|
x.opts = append(x.opts, client.WithXHeader(xhdr))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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.
|
|
|
|
func (x *readPrmCommon) SetNetmapEpoch(epoch uint64) {
|
2021-11-10 07:08:33 +00:00
|
|
|
xNetmapEpoch := session.NewXHeader()
|
2021-11-01 08:35:33 +00:00
|
|
|
xNetmapEpoch.SetKey(session2.XHeaderNetmapEpoch)
|
|
|
|
xNetmapEpoch.SetValue(strconv.FormatUint(epoch, 10))
|
|
|
|
|
|
|
|
x.opts = append(x.opts, client.WithXHeader(xNetmapEpoch))
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetObjectPrm groups parameters of GetObject operation.
|
|
|
|
type GetObjectPrm struct {
|
|
|
|
readPrmCommon
|
|
|
|
|
|
|
|
cliPrm client.GetObjectParams
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetRawFlag sets raw flag of the request.
|
|
|
|
//
|
|
|
|
// By default request will not be raw.
|
|
|
|
func (x *GetObjectPrm) SetRawFlag() {
|
|
|
|
x.cliPrm.WithRawFlag(true)
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetAddress sets object address.
|
|
|
|
//
|
|
|
|
// Required parameter.
|
2022-01-26 12:11:13 +00:00
|
|
|
func (x *GetObjectPrm) SetAddress(addr *addressSDK.Address) {
|
2021-11-01 08:35:33 +00:00
|
|
|
x.cliPrm.WithAddress(addr)
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetObjectRes groups resulting values of GetObject operation.
|
|
|
|
type GetObjectRes struct {
|
2021-11-06 11:13:04 +00:00
|
|
|
cliRes *client.ObjectGetRes
|
2021-11-01 08:35:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Object returns requested object.
|
|
|
|
func (x GetObjectRes) Object() *object.Object {
|
2021-11-06 11:13:04 +00:00
|
|
|
return x.cliRes.Object()
|
2021-11-01 08:35:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetObject reads the object by address.
|
|
|
|
//
|
|
|
|
// Client, context and key must be set.
|
|
|
|
//
|
2021-11-03 17:13:16 +00:00
|
|
|
// Returns any error prevented the operation from completing correctly in error return.
|
2021-11-01 08:35:33 +00:00
|
|
|
// Returns:
|
|
|
|
// error of type *object.SplitInfoError if object if raw flag is set and requested object is virtual;
|
|
|
|
// object.ErrAlreadyRemoved error if requested object is marked to be removed.
|
|
|
|
func GetObject(prm GetObjectPrm) (res GetObjectRes, err error) {
|
|
|
|
res.cliRes, err = prm.cli.GetObject(prm.ctx, &prm.cliPrm, prm.opts...)
|
2021-11-06 11:13:04 +00:00
|
|
|
if err == nil {
|
|
|
|
// pull out an error from status
|
|
|
|
err = apistatus.ErrFromStatus(res.cliRes.Status())
|
|
|
|
}
|
2021-11-01 08:35:33 +00:00
|
|
|
|
2022-02-07 13:34:02 +00:00
|
|
|
// FIXME: #1158 object.ErrAlreadyRemoved never returns
|
2021-11-03 17:13:16 +00:00
|
|
|
|
2021-11-01 08:35:33 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// HeadObjectPrm groups parameters of HeadObject operation.
|
|
|
|
type HeadObjectPrm struct {
|
|
|
|
readPrmCommon
|
|
|
|
|
|
|
|
cliPrm client.ObjectHeaderParams
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetRawFlag sets raw flag of the request.
|
|
|
|
//
|
|
|
|
// By default request will not be raw.
|
|
|
|
func (x *HeadObjectPrm) SetRawFlag() {
|
|
|
|
x.cliPrm.WithRawFlag(true)
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetAddress sets object address.
|
|
|
|
//
|
|
|
|
// Required parameter.
|
2022-01-26 12:11:13 +00:00
|
|
|
func (x *HeadObjectPrm) SetAddress(addr *addressSDK.Address) {
|
2021-11-01 08:35:33 +00:00
|
|
|
x.cliPrm.WithAddress(addr)
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetObjectRes groups resulting values of GetObject operation.
|
|
|
|
type HeadObjectRes struct {
|
2021-11-06 11:13:04 +00:00
|
|
|
cliRes *client.ObjectHeadRes
|
2021-11-01 08:35:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Header returns requested object header.
|
|
|
|
func (x HeadObjectRes) Header() *object.Object {
|
2021-11-06 11:13:04 +00:00
|
|
|
return x.cliRes.Object()
|
2021-11-01 08:35:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// HeadObject reads object header by address.
|
|
|
|
//
|
|
|
|
// Client, context and key must be set.
|
|
|
|
//
|
2021-11-03 17:13:16 +00:00
|
|
|
// Returns any error prevented the operation from completing correctly in error return.
|
2021-11-01 08:35:33 +00:00
|
|
|
// Returns:
|
|
|
|
// error of type *object.SplitInfoError if object if raw flag is set and requested object is virtual;
|
|
|
|
// object.ErrAlreadyRemoved error if requested object is marked to be removed.
|
|
|
|
func HeadObject(prm HeadObjectPrm) (res HeadObjectRes, err error) {
|
2021-11-06 11:13:04 +00:00
|
|
|
res.cliRes, err = prm.cli.HeadObject(prm.ctx, &prm.cliPrm, prm.opts...)
|
|
|
|
if err == nil {
|
|
|
|
// pull out an error from status
|
|
|
|
err = apistatus.ErrFromStatus(res.cliRes.Status())
|
|
|
|
}
|
2021-11-01 08:35:33 +00:00
|
|
|
|
2022-02-07 13:34:02 +00:00
|
|
|
// FIXME: #1158 object.ErrAlreadyRemoved never returns
|
2021-11-03 17:13:16 +00:00
|
|
|
|
2021-11-01 08:35:33 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// PayloadRangePrm groups parameters of PayloadRange operation.
|
|
|
|
type PayloadRangePrm struct {
|
|
|
|
readPrmCommon
|
|
|
|
|
|
|
|
cliPrm client.RangeDataParams
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetRawFlag sets raw flag of the request.
|
|
|
|
//
|
|
|
|
// By default request will not be raw.
|
|
|
|
func (x *PayloadRangePrm) SetRawFlag() {
|
|
|
|
x.cliPrm.WithRaw(true)
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetAddress sets object address.
|
|
|
|
//
|
|
|
|
// Required parameter.
|
2022-01-26 12:11:13 +00:00
|
|
|
func (x *PayloadRangePrm) SetAddress(addr *addressSDK.Address) {
|
2021-11-01 08:35:33 +00:00
|
|
|
x.cliPrm.WithAddress(addr)
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetRange range of the object payload to be read.
|
|
|
|
//
|
|
|
|
// Required parameter.
|
|
|
|
func (x *PayloadRangePrm) SetRange(rng *object.Range) {
|
|
|
|
x.cliPrm.WithRange(rng)
|
|
|
|
}
|
|
|
|
|
|
|
|
// PayloadRangeRes groups resulting values of GetObject operation.
|
|
|
|
type PayloadRangeRes struct {
|
2021-11-06 11:13:04 +00:00
|
|
|
cliRes *client.ObjectRangeRes
|
2021-11-01 08:35:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// PayloadRange returns data of the requested payload range.
|
|
|
|
func (x PayloadRangeRes) PayloadRange() []byte {
|
2021-11-06 11:13:04 +00:00
|
|
|
return x.cliRes.Data()
|
2021-11-01 08:35:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// PayloadRange reads object payload range by address.
|
|
|
|
//
|
|
|
|
// Client, context and key must be set.
|
|
|
|
//
|
2021-11-03 17:13:16 +00:00
|
|
|
// Returns any error prevented the operation from completing correctly in error return.
|
2021-11-01 08:35:33 +00:00
|
|
|
// Returns:
|
|
|
|
// error of type *object.SplitInfoError if object if raw flag is set and requested object is virtual;
|
|
|
|
// object.ErrAlreadyRemoved error if requested object is marked to be removed.
|
|
|
|
func PayloadRange(prm PayloadRangePrm) (res PayloadRangeRes, err error) {
|
|
|
|
res.cliRes, err = prm.cli.ObjectPayloadRangeData(prm.ctx, &prm.cliPrm, prm.opts...)
|
2021-11-06 11:13:04 +00:00
|
|
|
if err == nil {
|
|
|
|
// pull out an error from status
|
|
|
|
err = apistatus.ErrFromStatus(res.cliRes.Status())
|
|
|
|
}
|
2021-11-01 08:35:33 +00:00
|
|
|
|
2022-02-07 13:34:02 +00:00
|
|
|
// FIXME: #1158 object.ErrAlreadyRemoved never returns
|
2021-11-03 17:13:16 +00:00
|
|
|
|
2021-11-01 08:35:33 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// PutObjectPrm groups parameters of PutObject operation.
|
|
|
|
type PutObjectPrm struct {
|
|
|
|
commonPrm
|
|
|
|
|
|
|
|
cliPrm client.PutObjectParams
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetObject sets object to be stored.
|
|
|
|
//
|
|
|
|
// Required parameter.
|
|
|
|
func (x *PutObjectPrm) SetObject(obj *object.Object) {
|
|
|
|
x.cliPrm.WithObject(obj)
|
|
|
|
}
|
|
|
|
|
|
|
|
// PutObjectRes groups resulting values of PutObject operation.
|
|
|
|
type PutObjectRes struct {
|
2021-11-06 11:13:04 +00:00
|
|
|
cliRes *client.ObjectPutRes
|
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 {
|
2021-11-06 11:13:04 +00:00
|
|
|
return x.cliRes.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
|
|
|
//
|
|
|
|
// Returns any error prevented the operation from completing correctly in error return.
|
2021-11-01 08:35:33 +00:00
|
|
|
func PutObject(prm PutObjectPrm) (res PutObjectRes, err error) {
|
|
|
|
res.cliRes, err = prm.cli.PutObject(prm.ctx, &prm.cliPrm,
|
|
|
|
append(prm.opts, client.WithTTL(1))...,
|
|
|
|
)
|
2021-11-06 11:13:04 +00:00
|
|
|
if err == nil {
|
|
|
|
// pull out an error from status
|
|
|
|
err = apistatus.ErrFromStatus(res.cliRes.Status())
|
|
|
|
}
|
2021-11-01 08:35:33 +00:00
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// SearchObjectsPrm groups parameters of SearchObjects operation.
|
|
|
|
type SearchObjectsPrm struct {
|
|
|
|
readPrmCommon
|
|
|
|
|
|
|
|
cliPrm client.SearchObjectParams
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetContainerID sets identifier of the container to search the objects.
|
|
|
|
//
|
|
|
|
// Required parameter.
|
|
|
|
func (x *SearchObjectsPrm) SetContainerID(id *cid.ID) {
|
|
|
|
x.cliPrm.WithContainerID(id)
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetFilters sets search filters.
|
|
|
|
func (x *SearchObjectsPrm) SetFilters(fs object.SearchFilters) {
|
|
|
|
x.cliPrm.WithSearchFilters(fs)
|
|
|
|
}
|
|
|
|
|
|
|
|
// SearchObjectsRes groups resulting values of SearchObjects operation.
|
|
|
|
type SearchObjectsRes struct {
|
2021-11-06 11:13:04 +00:00
|
|
|
cliRes *client.ObjectSearchRes
|
2021-11-01 08:35:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// IDList returns identifiers of the matched objects.
|
2022-01-26 12:11:13 +00:00
|
|
|
func (x SearchObjectsRes) IDList() []*oidSDK.ID {
|
2021-11-06 11:13:04 +00:00
|
|
|
return x.cliRes.IDList()
|
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
|
|
|
//
|
|
|
|
// Returns any error prevented the operation from completing correctly in error return.
|
2021-11-01 08:35:33 +00:00
|
|
|
func SearchObjects(prm SearchObjectsPrm) (res SearchObjectsRes, err error) {
|
2021-11-06 11:13:04 +00:00
|
|
|
res.cliRes, err = prm.cli.SearchObjects(prm.ctx, &prm.cliPrm, prm.opts...)
|
|
|
|
if err == nil {
|
|
|
|
// pull out an error from status
|
|
|
|
err = apistatus.ErrFromStatus(res.cliRes.Status())
|
|
|
|
}
|
2021-11-01 08:35:33 +00:00
|
|
|
|
|
|
|
return
|
|
|
|
}
|