2021-11-01 08:35:33 +00:00
|
|
|
package internal
|
|
|
|
|
|
|
|
import (
|
2022-11-02 10:42:29 +00:00
|
|
|
"bytes"
|
2021-11-01 08:35:33 +00:00
|
|
|
"context"
|
|
|
|
"crypto/ecdsa"
|
2022-02-25 09:20:49 +00:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
2021-11-01 08:35:33 +00:00
|
|
|
|
2023-03-07 13:38:26 +00:00
|
|
|
coreclient "git.frostfs.info/TrueCloudLab/frostfs-node/pkg/core/client"
|
2023-05-31 09:24:04 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-observability/tracing"
|
2023-03-07 13:38:26 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-sdk-go/bearer"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-sdk-go/client"
|
|
|
|
apistatus "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/client/status"
|
|
|
|
cid "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/container/id"
|
2023-07-06 12:36:41 +00:00
|
|
|
objectSDK "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/object"
|
2023-03-07 13:38:26 +00:00
|
|
|
oid "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/object/id"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-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
|
|
|
|
2022-02-25 09:20:49 +00:00
|
|
|
key *ecdsa.PrivateKey
|
|
|
|
|
2022-05-18 15:20:08 +00:00
|
|
|
tokenSession *session.Object
|
2022-02-25 09:20:49 +00:00
|
|
|
|
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
|
|
|
|
2022-05-18 15:20:08 +00:00
|
|
|
xHeaders []string
|
2021-11-01 08:35:33 +00:00
|
|
|
}
|
|
|
|
|
2023-02-05 15:59:38 +00:00
|
|
|
// SetClient sets base client for ForstFS API communication.
|
2021-11-01 08:35:33 +00:00
|
|
|
//
|
|
|
|
// 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
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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.
|
2022-05-18 15:20:08 +00:00
|
|
|
func (x *commonPrm) SetSessionToken(tok *session.Object) {
|
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-05-18 15:20:08 +00:00
|
|
|
func (x *commonPrm) SetXHeaders(hs []string) {
|
2022-03-03 12:30:20 +00:00
|
|
|
x.xHeaders = hs
|
|
|
|
}
|
|
|
|
|
2021-11-01 08:35:33 +00:00
|
|
|
type readPrmCommon struct {
|
|
|
|
commonPrm
|
|
|
|
}
|
|
|
|
|
2023-07-06 12:36:41 +00:00
|
|
|
// SetNetmapEpoch sets the epoch number to be used to locate the objectSDK.
|
2021-11-01 08:35:33 +00:00
|
|
|
//
|
|
|
|
// By default current epoch on the server will be used.
|
2022-02-25 09:20:49 +00:00
|
|
|
func (x *readPrmCommon) SetNetmapEpoch(_ uint64) {
|
2023-06-26 10:58:22 +00:00
|
|
|
// FIXME(@fyrchik): https://git.frostfs.info/TrueCloudLab/frostfs-node/issues/465
|
2021-11-01 08:35:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetObjectPrm groups parameters of GetObject operation.
|
|
|
|
type GetObjectPrm struct {
|
|
|
|
readPrmCommon
|
|
|
|
|
2023-08-29 10:25:54 +00:00
|
|
|
ClientParams client.PrmObjectGet
|
2022-10-13 16:03:40 +00:00
|
|
|
|
|
|
|
obj oid.ID
|
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() {
|
2023-08-29 10:25:54 +00:00
|
|
|
x.ClientParams.Raw = true
|
2021-11-01 08:35:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// SetAddress sets object address.
|
|
|
|
//
|
|
|
|
// Required parameter.
|
2022-05-31 17:00:41 +00:00
|
|
|
func (x *GetObjectPrm) SetAddress(addr oid.Address) {
|
2022-10-13 16:03:40 +00:00
|
|
|
x.obj = addr.Object()
|
2023-08-29 10:25:54 +00:00
|
|
|
cnr := addr.Container()
|
|
|
|
|
|
|
|
x.ClientParams.ContainerID = &cnr
|
|
|
|
x.ClientParams.ObjectID = &x.obj
|
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 {
|
2023-07-06 12:36:41 +00:00
|
|
|
obj *objectSDK.Object
|
2021-11-01 08:35:33 +00:00
|
|
|
}
|
|
|
|
|
2023-07-06 12:36:41 +00:00
|
|
|
// Object returns requested objectSDK.
|
|
|
|
func (x GetObjectRes) Object() *objectSDK.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:
|
2023-07-06 12:36:41 +00:00
|
|
|
// - error of type *objectSDK.SplitInfoError if object raw flag is set and requested object is virtual;
|
2022-08-15 16:20:20 +00:00
|
|
|
// - error of type *apistatus.ObjectAlreadyRemoved if the requested object is marked to be removed.
|
2022-10-13 16:03:40 +00:00
|
|
|
//
|
2023-07-06 12:36:41 +00:00
|
|
|
// GetObject ignores the provided session if it is not related to the requested objectSDK.
|
2023-04-06 12:36:37 +00:00
|
|
|
func GetObject(ctx context.Context, prm GetObjectPrm) (*GetObjectRes, error) {
|
2022-10-13 16:03:40 +00:00
|
|
|
// here we ignore session if it is opened for other object since such
|
|
|
|
// request will almost definitely fail. The case can occur, for example,
|
|
|
|
// when session is bound to the parent object and child object is requested.
|
|
|
|
if prm.tokenSession != nil && prm.tokenSession.AssertObject(prm.obj) {
|
2023-08-29 10:25:54 +00:00
|
|
|
prm.ClientParams.Session = prm.tokenSession
|
2022-02-25 09:20:49 +00:00
|
|
|
}
|
|
|
|
|
2023-08-29 10:25:54 +00:00
|
|
|
prm.ClientParams.XHeaders = prm.xHeaders
|
|
|
|
prm.ClientParams.BearerToken = prm.tokenBearer
|
|
|
|
prm.ClientParams.Local = prm.local
|
|
|
|
prm.ClientParams.Key = prm.key
|
2022-03-03 12:30:20 +00:00
|
|
|
|
2023-08-29 10:25:54 +00:00
|
|
|
rdr, err := prm.cli.ObjectGetInit(ctx, prm.ClientParams)
|
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)
|
|
|
|
}
|
|
|
|
|
2023-07-06 12:36:41 +00:00
|
|
|
var obj objectSDK.Object
|
2022-02-25 09:20:49 +00:00
|
|
|
|
|
|
|
if !rdr.ReadHeader(&obj) {
|
|
|
|
res, err := rdr.Close()
|
|
|
|
if err == nil {
|
|
|
|
// pull out an error from status
|
|
|
|
err = apistatus.ErrFromStatus(res.Status())
|
2022-12-19 14:47:28 +00:00
|
|
|
} else {
|
|
|
|
ReportError(prm.cli, err)
|
2022-02-25 09:20:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
|
2023-08-29 10:25:54 +00:00
|
|
|
ClientParams client.PrmObjectHead
|
2022-10-13 16:03:40 +00:00
|
|
|
|
|
|
|
obj oid.ID
|
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() {
|
2023-08-29 10:25:54 +00:00
|
|
|
x.ClientParams.Raw = true
|
2021-11-01 08:35:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// SetAddress sets object address.
|
|
|
|
//
|
|
|
|
// Required parameter.
|
2022-05-31 17:00:41 +00:00
|
|
|
func (x *HeadObjectPrm) SetAddress(addr oid.Address) {
|
2022-10-13 16:03:40 +00:00
|
|
|
x.obj = addr.Object()
|
2023-08-29 10:25:54 +00:00
|
|
|
cnr := addr.Container()
|
|
|
|
|
|
|
|
x.ClientParams.ContainerID = &cnr
|
|
|
|
x.ClientParams.ObjectID = &x.obj
|
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 {
|
2023-07-06 12:36:41 +00:00
|
|
|
hdr *objectSDK.Object
|
2021-11-01 08:35:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Header returns requested object header.
|
2023-07-06 12:36:41 +00:00
|
|
|
func (x HeadObjectRes) Header() *objectSDK.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.
|
|
|
|
//
|
2023-04-06 12:36:37 +00:00
|
|
|
// Client and key must be set.
|
2021-11-01 08:35:33 +00:00
|
|
|
//
|
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-08-15 16:20:20 +00:00
|
|
|
//
|
2023-07-06 12:36:41 +00:00
|
|
|
// error of type *objectSDK.SplitInfoError if object raw flag is set and requested object is virtual;
|
2022-08-15 16:20:20 +00:00
|
|
|
// error of type *apistatus.ObjectAlreadyRemoved if the requested object is marked to be removed.
|
2022-10-13 16:03:40 +00:00
|
|
|
//
|
2023-07-06 12:36:41 +00:00
|
|
|
// HeadObject ignores the provided session if it is not related to the requested objectSDK.
|
2023-04-06 12:36:37 +00:00
|
|
|
func HeadObject(ctx context.Context, prm HeadObjectPrm) (*HeadObjectRes, error) {
|
2022-10-13 16:03:40 +00:00
|
|
|
// see details in same statement of GetObject
|
|
|
|
if prm.tokenSession != nil && prm.tokenSession.AssertObject(prm.obj) {
|
2023-08-29 10:25:54 +00:00
|
|
|
prm.ClientParams.Session = prm.tokenSession
|
2022-02-25 09:20:49 +00:00
|
|
|
}
|
|
|
|
|
2023-08-29 10:25:54 +00:00
|
|
|
prm.ClientParams.BearerToken = prm.tokenBearer
|
|
|
|
prm.ClientParams.Local = prm.local
|
|
|
|
prm.ClientParams.XHeaders = prm.xHeaders
|
2022-03-03 12:30:20 +00:00
|
|
|
|
2023-08-29 10:25:54 +00:00
|
|
|
cliRes, err := prm.cli.ObjectHead(ctx, prm.ClientParams)
|
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 {
|
2023-02-05 15:59:38 +00:00
|
|
|
return nil, fmt.Errorf("read object header from FrostFS: %w", err)
|
2021-11-06 11:13:04 +00:00
|
|
|
}
|
2021-11-01 08:35:33 +00:00
|
|
|
|
2023-07-06 12:36:41 +00:00
|
|
|
var hdr objectSDK.Object
|
2022-02-25 09:20:49 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
|
2023-08-29 10:25:54 +00:00
|
|
|
ClientParams client.PrmObjectRange
|
2022-10-13 16:03:40 +00:00
|
|
|
|
|
|
|
obj oid.ID
|
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() {
|
2023-08-29 10:25:54 +00:00
|
|
|
x.ClientParams.Raw = true
|
2021-11-01 08:35:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// SetAddress sets object address.
|
|
|
|
//
|
|
|
|
// Required parameter.
|
2022-05-31 17:00:41 +00:00
|
|
|
func (x *PayloadRangePrm) SetAddress(addr oid.Address) {
|
2022-10-13 16:03:40 +00:00
|
|
|
x.obj = addr.Object()
|
2023-08-29 10:25:54 +00:00
|
|
|
cnr := addr.Container()
|
|
|
|
|
|
|
|
x.ClientParams.ContainerID = &cnr
|
|
|
|
x.ClientParams.ObjectID = &x.obj
|
2021-11-01 08:35:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// SetRange range of the object payload to be read.
|
|
|
|
//
|
|
|
|
// Required parameter.
|
2023-07-06 12:36:41 +00:00
|
|
|
func (x *PayloadRangePrm) SetRange(rng *objectSDK.Range) {
|
2023-08-29 10:25:54 +00:00
|
|
|
x.ClientParams.Offset = rng.GetOffset()
|
2022-02-25 09:20:49 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2022-11-02 10:42:29 +00:00
|
|
|
// maxInitialBufferSize is the maximum initial buffer size for PayloadRange result.
|
|
|
|
// We don't want to allocate a lot of space in advance because a query can
|
|
|
|
// fail with apistatus.ObjectOutOfRange status.
|
|
|
|
const maxInitialBufferSize = 1024 * 1024 // 1 MiB
|
|
|
|
|
2021-11-01 08:35:33 +00:00
|
|
|
// PayloadRange reads object payload range by address.
|
|
|
|
//
|
2023-04-06 12:36:37 +00:00
|
|
|
// Client and key must be set.
|
2021-11-01 08:35:33 +00:00
|
|
|
//
|
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-08-15 16:20:20 +00:00
|
|
|
//
|
2023-07-06 12:36:41 +00:00
|
|
|
// error of type *objectSDK.SplitInfoError if object raw flag is set and requested object is virtual;
|
2022-11-08 12:12:52 +00:00
|
|
|
// error of type *apistatus.ObjectAlreadyRemoved if the requested object is marked to be removed;
|
|
|
|
// error of type *apistatus.ObjectOutOfRange if the requested range is too big.
|
2022-10-13 16:03:40 +00:00
|
|
|
//
|
2023-07-06 12:36:41 +00:00
|
|
|
// PayloadRange ignores the provided session if it is not related to the requested objectSDK.
|
2023-04-06 12:36:37 +00:00
|
|
|
func PayloadRange(ctx context.Context, prm PayloadRangePrm) (*PayloadRangeRes, error) {
|
2022-10-13 16:03:40 +00:00
|
|
|
// see details in same statement of GetObject
|
|
|
|
if prm.tokenSession != nil && prm.tokenSession.AssertObject(prm.obj) {
|
2023-08-29 10:25:54 +00:00
|
|
|
prm.ClientParams.Session = prm.tokenSession
|
2022-02-25 09:20:49 +00:00
|
|
|
}
|
|
|
|
|
2023-08-29 10:25:54 +00:00
|
|
|
prm.ClientParams.XHeaders = prm.xHeaders
|
|
|
|
prm.ClientParams.BearerToken = prm.tokenBearer
|
|
|
|
prm.ClientParams.Local = prm.local
|
|
|
|
prm.ClientParams.Length = prm.ln
|
2022-02-25 09:20:49 +00:00
|
|
|
|
2023-08-29 10:25:54 +00:00
|
|
|
rdr, err := prm.cli.ObjectRangeInit(ctx, prm.ClientParams)
|
2022-02-25 09:20:49 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("init payload reading: %w", err)
|
|
|
|
}
|
|
|
|
|
2022-11-02 10:42:29 +00:00
|
|
|
if int64(prm.ln) < 0 {
|
|
|
|
// `CopyN` expects `int64`, this check ensures that the result is positive.
|
|
|
|
// On practice this means that we can return incorrect results for objects
|
|
|
|
// with size > 8_388 Petabytes, this will be fixed later with support for streaming.
|
2022-11-08 12:12:52 +00:00
|
|
|
return nil, new(apistatus.ObjectOutOfRange)
|
2022-11-02 10:42:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ln := prm.ln
|
|
|
|
if ln > maxInitialBufferSize {
|
|
|
|
ln = maxInitialBufferSize
|
|
|
|
}
|
2022-02-25 09:20:49 +00:00
|
|
|
|
2022-11-02 10:42:29 +00:00
|
|
|
w := bytes.NewBuffer(make([]byte, ln))
|
|
|
|
_, err = io.CopyN(w, rdr, int64(prm.ln))
|
2022-02-25 09:20:49 +00:00
|
|
|
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{
|
2022-11-02 10:42:29 +00:00
|
|
|
data: w.Bytes(),
|
2022-02-25 09:20:49 +00:00
|
|
|
}, nil
|
2021-11-01 08:35:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// PutObjectPrm groups parameters of PutObject operation.
|
|
|
|
type PutObjectPrm struct {
|
|
|
|
commonPrm
|
|
|
|
|
2023-07-06 12:36:41 +00:00
|
|
|
obj *objectSDK.Object
|
2021-11-01 08:35:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// SetObject sets object to be stored.
|
|
|
|
//
|
|
|
|
// Required parameter.
|
2023-07-06 12:36:41 +00:00
|
|
|
func (x *PutObjectPrm) SetObject(obj *objectSDK.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-05-31 17:00:41 +00:00
|
|
|
id oid.ID
|
2021-11-01 08:35:33 +00:00
|
|
|
}
|
|
|
|
|
2023-07-06 12:36:41 +00:00
|
|
|
// ID returns identifier of the stored objectSDK.
|
2022-05-31 17:00:41 +00:00
|
|
|
func (x PutObjectRes) ID() oid.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.
|
|
|
|
//
|
2023-04-06 12:36:37 +00:00
|
|
|
// Client 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.
|
2023-04-06 12:36:37 +00:00
|
|
|
func PutObject(ctx context.Context, prm PutObjectPrm) (*PutObjectRes, error) {
|
2023-04-12 14:01:29 +00:00
|
|
|
ctx, span := tracing.StartSpanFromContext(ctx, "client.PutObject")
|
|
|
|
defer span.End()
|
|
|
|
|
2022-02-25 09:20:49 +00:00
|
|
|
var prmCli client.PrmObjectPutInit
|
|
|
|
|
2022-08-29 08:57:02 +00:00
|
|
|
prmCli.MarkLocal()
|
2022-02-25 09:20:49 +00:00
|
|
|
|
|
|
|
if prm.key != nil {
|
2022-08-29 08:57:02 +00:00
|
|
|
prmCli.UseKey(*prm.key)
|
2022-02-25 09:20:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if prm.tokenSession != nil {
|
2022-08-29 08:57:02 +00:00
|
|
|
prmCli.WithinSession(*prm.tokenSession)
|
2022-02-25 09:20:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if prm.tokenBearer != nil {
|
2022-08-29 08:57:02 +00:00
|
|
|
prmCli.WithBearerToken(*prm.tokenBearer)
|
2022-02-25 09:20:49 +00:00
|
|
|
}
|
|
|
|
|
2022-08-29 08:57:02 +00:00
|
|
|
prmCli.WithXHeaders(prm.xHeaders...)
|
|
|
|
|
2023-04-06 12:36:37 +00:00
|
|
|
w, err := prm.cli.ObjectPutInit(ctx, prmCli)
|
2022-08-29 08:57:02 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("init object writing on client: %w", err)
|
|
|
|
}
|
2022-03-03 12:30:20 +00:00
|
|
|
|
2023-06-29 06:44:11 +00:00
|
|
|
if w.WriteHeader(ctx, *prm.obj) {
|
|
|
|
w.WritePayloadChunk(ctx, prm.obj.Payload())
|
2022-02-25 09:20:49 +00:00
|
|
|
}
|
|
|
|
|
2023-06-29 06:44:11 +00:00
|
|
|
cliRes, err := w.Close(ctx)
|
2021-11-06 11:13:04 +00:00
|
|
|
if err == nil {
|
2022-05-31 17:00:41 +00:00
|
|
|
err = apistatus.ErrFromStatus(cliRes.Status())
|
2022-12-19 14:47:28 +00:00
|
|
|
} else {
|
|
|
|
ReportError(prm.cli, 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
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("write object via client: %w", err)
|
|
|
|
}
|
|
|
|
|
2022-08-22 11:04:00 +00:00
|
|
|
return &PutObjectRes{
|
|
|
|
id: cliRes.StoredObjectID(),
|
|
|
|
}, nil
|
2021-11-01 08:35:33 +00:00
|
|
|
}
|
|
|
|
|
2023-07-12 14:47:42 +00:00
|
|
|
// PutObjectSingle saves the object in local storage of the remote node with PutSingle RPC.
|
|
|
|
//
|
|
|
|
// Client and key must be set.
|
|
|
|
//
|
|
|
|
// Returns any error which prevented the operation from completing correctly in error return.
|
|
|
|
func PutObjectSingle(ctx context.Context, prm PutObjectPrm) (*PutObjectRes, error) {
|
|
|
|
ctx, span := tracing.StartSpanFromContext(ctx, "client.PutObjectSingle")
|
|
|
|
defer span.End()
|
|
|
|
|
|
|
|
objID, isSet := prm.obj.ID()
|
|
|
|
if !isSet {
|
|
|
|
return nil, errors.New("missing object id")
|
|
|
|
}
|
|
|
|
|
2023-10-19 15:19:55 +00:00
|
|
|
prmCli := client.PrmObjectPutSingle{
|
|
|
|
XHeaders: prm.xHeaders,
|
|
|
|
BearerToken: prm.tokenBearer,
|
|
|
|
Session: prm.tokenSession,
|
|
|
|
Local: true,
|
|
|
|
Key: prm.key,
|
|
|
|
Object: prm.obj,
|
2023-07-12 14:47:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
res, err := prm.cli.ObjectPutSingle(ctx, prmCli)
|
|
|
|
if err != nil {
|
|
|
|
ReportError(prm.cli, err)
|
|
|
|
return nil, fmt.Errorf("put single object on client: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = apistatus.ErrFromStatus(res.Status()); err != nil {
|
|
|
|
return nil, fmt.Errorf("put single object via client: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return &PutObjectRes{
|
|
|
|
id: objID,
|
|
|
|
}, 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.
|
2022-05-31 17:00:41 +00:00
|
|
|
func (x *SearchObjectsPrm) SetContainerID(id cid.ID) {
|
|
|
|
x.cliPrm.InContainer(id)
|
2021-11-01 08:35:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// SetFilters sets search filters.
|
2023-07-06 12:36:41 +00:00
|
|
|
func (x *SearchObjectsPrm) SetFilters(fs objectSDK.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-05-31 17:00:41 +00:00
|
|
|
ids []oid.ID
|
2021-11-01 08:35:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// IDList returns identifiers of the matched objects.
|
2022-05-31 17:00:41 +00:00
|
|
|
func (x SearchObjectsRes) IDList() []oid.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.
|
2023-04-06 12:36:37 +00:00
|
|
|
func SearchObjects(ctx context.Context, prm SearchObjectsPrm) (*SearchObjectsRes, error) {
|
2022-02-25 09:20:49 +00:00
|
|
|
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-05-18 15:20:08 +00:00
|
|
|
prm.cliPrm.WithXHeaders(prm.xHeaders...)
|
2022-03-03 12:30:20 +00:00
|
|
|
|
2022-08-29 08:57:02 +00:00
|
|
|
if prm.key != nil {
|
|
|
|
prm.cliPrm.UseKey(*prm.key)
|
|
|
|
}
|
|
|
|
|
2023-04-06 12:36:37 +00:00
|
|
|
rdr, err := prm.cli.ObjectSearchInit(ctx, prm.cliPrm)
|
2022-02-25 09:20:49 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("init object searching in client: %w", err)
|
|
|
|
}
|
|
|
|
|
2022-05-31 17:00:41 +00:00
|
|
|
buf := make([]oid.ID, 10)
|
|
|
|
var ids []oid.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
|
|
|
}
|