2022-02-07 20:27:56 +00:00
|
|
|
package client
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"crypto/ecdsa"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
|
2023-03-07 11:20:03 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-api-go/v2/acl"
|
|
|
|
v2object "git.frostfs.info/TrueCloudLab/frostfs-api-go/v2/object"
|
|
|
|
v2refs "git.frostfs.info/TrueCloudLab/frostfs-api-go/v2/refs"
|
|
|
|
rpcapi "git.frostfs.info/TrueCloudLab/frostfs-api-go/v2/rpc"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-api-go/v2/rpc/client"
|
|
|
|
v2session "git.frostfs.info/TrueCloudLab/frostfs-api-go/v2/session"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-api-go/v2/signature"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-sdk-go/bearer"
|
|
|
|
apistatus "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/client/status"
|
|
|
|
cid "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/container/id"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-sdk-go/object"
|
|
|
|
oid "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/object/id"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-sdk-go/session"
|
2022-02-07 20:27:56 +00:00
|
|
|
)
|
|
|
|
|
2023-08-28 08:07:18 +00:00
|
|
|
// PrmObjectGet groups parameters of ObjectGetInit operation.
|
|
|
|
type PrmObjectGet struct {
|
|
|
|
XHeaders []string
|
2022-03-03 11:04:53 +00:00
|
|
|
|
2023-08-28 08:07:18 +00:00
|
|
|
BearerToken *bearer.Token
|
2022-02-07 20:27:56 +00:00
|
|
|
|
2023-08-28 08:07:18 +00:00
|
|
|
Session *session.Object
|
2022-02-07 20:27:56 +00:00
|
|
|
|
2023-08-28 08:07:18 +00:00
|
|
|
Raw bool
|
2022-03-03 11:04:53 +00:00
|
|
|
|
2023-08-28 08:07:18 +00:00
|
|
|
Local bool
|
2022-02-07 20:27:56 +00:00
|
|
|
|
2023-08-28 08:07:18 +00:00
|
|
|
ContainerID *cid.ID
|
2022-02-07 20:27:56 +00:00
|
|
|
|
2023-08-28 08:07:18 +00:00
|
|
|
ObjectID *oid.ID
|
2022-02-07 20:27:56 +00:00
|
|
|
|
2023-08-28 08:07:18 +00:00
|
|
|
Key *ecdsa.PrivateKey
|
2022-02-07 20:27:56 +00:00
|
|
|
}
|
|
|
|
|
2023-08-28 08:07:18 +00:00
|
|
|
func (prm *PrmObjectGet) buildRequest(c *Client) (*v2object.GetRequest, error) {
|
|
|
|
if prm.ContainerID == nil {
|
|
|
|
return nil, errorMissingContainer
|
|
|
|
}
|
2022-02-07 20:27:56 +00:00
|
|
|
|
2023-08-28 08:07:18 +00:00
|
|
|
if prm.ObjectID == nil {
|
|
|
|
return nil, errorMissingObject
|
|
|
|
}
|
2022-02-07 20:27:56 +00:00
|
|
|
|
2023-08-28 08:07:18 +00:00
|
|
|
if len(prm.XHeaders)%2 != 0 {
|
|
|
|
return nil, errorInvalidXHeaders
|
|
|
|
}
|
|
|
|
|
|
|
|
meta := new(v2session.RequestMetaHeader)
|
|
|
|
writeXHeadersToMeta(prm.XHeaders, meta)
|
|
|
|
|
|
|
|
if prm.BearerToken != nil {
|
|
|
|
v2BearerToken := new(acl.BearerToken)
|
|
|
|
prm.BearerToken.WriteToV2(v2BearerToken)
|
|
|
|
meta.SetBearerToken(v2BearerToken)
|
|
|
|
}
|
|
|
|
|
|
|
|
if prm.Session != nil {
|
|
|
|
v2SessionToken := new(v2session.Token)
|
|
|
|
prm.Session.WriteToV2(v2SessionToken)
|
|
|
|
meta.SetSessionToken(v2SessionToken)
|
|
|
|
}
|
|
|
|
|
|
|
|
if prm.Local {
|
|
|
|
meta.SetTTL(1)
|
|
|
|
}
|
|
|
|
|
|
|
|
addr := new(v2refs.Address)
|
|
|
|
|
|
|
|
cnrV2 := new(v2refs.ContainerID)
|
|
|
|
prm.ContainerID.WriteToV2(cnrV2)
|
|
|
|
addr.SetContainerID(cnrV2)
|
2022-08-25 11:45:50 +00:00
|
|
|
|
2023-08-28 08:07:18 +00:00
|
|
|
objV2 := new(v2refs.ObjectID)
|
|
|
|
prm.ObjectID.WriteToV2(objV2)
|
|
|
|
addr.SetObjectID(objV2)
|
|
|
|
|
|
|
|
body := new(v2object.GetRequestBody)
|
|
|
|
body.SetRaw(prm.Raw)
|
|
|
|
body.SetAddress(addr)
|
|
|
|
|
|
|
|
req := new(v2object.GetRequest)
|
|
|
|
req.SetBody(body)
|
|
|
|
c.prepareRequest(req, meta)
|
|
|
|
|
|
|
|
return req, nil
|
2022-02-11 16:13:53 +00:00
|
|
|
}
|
|
|
|
|
2022-02-07 20:27:56 +00:00
|
|
|
// ResObjectGet groups the final result values of ObjectGetInit operation.
|
|
|
|
type ResObjectGet struct {
|
|
|
|
statusRes
|
|
|
|
}
|
|
|
|
|
2022-12-29 10:46:18 +00:00
|
|
|
// ObjectReader is designed to read one object from FrostFS system.
|
2022-02-07 20:27:56 +00:00
|
|
|
//
|
|
|
|
// Must be initialized using Client.ObjectGetInit, any other
|
|
|
|
// usage is unsafe.
|
|
|
|
type ObjectReader struct {
|
|
|
|
cancelCtxStream context.CancelFunc
|
|
|
|
|
2022-08-25 11:45:50 +00:00
|
|
|
client *Client
|
|
|
|
stream interface {
|
|
|
|
Read(resp *v2object.GetResponse) error
|
|
|
|
}
|
2022-02-07 20:27:56 +00:00
|
|
|
|
2022-08-25 11:45:50 +00:00
|
|
|
res ResObjectGet
|
|
|
|
err error
|
2022-02-07 20:27:56 +00:00
|
|
|
|
|
|
|
tailPayload []byte
|
2022-02-16 17:14:24 +00:00
|
|
|
|
|
|
|
remainingPayloadLen int
|
2022-02-07 20:27:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// UseKey specifies private key to sign the requests.
|
|
|
|
// If key is not provided, then Client default key is used.
|
2023-08-28 08:07:18 +00:00
|
|
|
//
|
|
|
|
// Deprecated: Use PrmObjectGet.Key instead.
|
|
|
|
func (prm *PrmObjectGet) UseKey(key ecdsa.PrivateKey) {
|
|
|
|
prm.Key = &key
|
2022-02-11 16:13:53 +00:00
|
|
|
}
|
|
|
|
|
2022-02-07 20:27:56 +00:00
|
|
|
// ReadHeader reads header of the object. Result means success.
|
|
|
|
// Failure reason can be received via Close.
|
|
|
|
func (x *ObjectReader) ReadHeader(dst *object.Object) bool {
|
2022-08-25 11:45:50 +00:00
|
|
|
var resp v2object.GetResponse
|
|
|
|
x.err = x.stream.Read(&resp)
|
|
|
|
if x.err != nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
x.res.st, x.err = x.client.processResponse(&resp)
|
|
|
|
if x.err != nil || !apistatus.IsSuccessful(x.res.st) {
|
2022-02-07 20:27:56 +00:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
var partInit *v2object.GetObjectPartInit
|
|
|
|
|
2022-08-25 11:45:50 +00:00
|
|
|
switch v := resp.GetBody().GetObjectPart().(type) {
|
2022-02-07 20:27:56 +00:00
|
|
|
default:
|
2022-08-25 11:45:50 +00:00
|
|
|
x.err = fmt.Errorf("unexpected message instead of heading part: %T", v)
|
2022-02-07 20:27:56 +00:00
|
|
|
return false
|
|
|
|
case *v2object.SplitInfo:
|
2022-08-25 11:45:50 +00:00
|
|
|
x.err = object.NewSplitInfoError(object.NewSplitInfoFromV2(v))
|
2022-02-07 20:27:56 +00:00
|
|
|
return false
|
2024-04-22 06:51:20 +00:00
|
|
|
case *v2object.ECInfo:
|
|
|
|
x.err = object.NewECInfoError(object.NewECInfoFromV2(v))
|
|
|
|
return false
|
2022-02-07 20:27:56 +00:00
|
|
|
case *v2object.GetObjectPartInit:
|
|
|
|
partInit = v
|
|
|
|
}
|
|
|
|
|
|
|
|
var objv2 v2object.Object
|
|
|
|
|
|
|
|
objv2.SetObjectID(partInit.GetObjectID())
|
|
|
|
objv2.SetHeader(partInit.GetHeader())
|
|
|
|
objv2.SetSignature(partInit.GetSignature())
|
|
|
|
|
2022-02-16 17:14:24 +00:00
|
|
|
x.remainingPayloadLen = int(objv2.GetHeader().GetPayloadLength())
|
|
|
|
|
2022-02-07 20:27:56 +00:00
|
|
|
*dst = *object.NewFromV2(&objv2) // need smth better
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
func (x *ObjectReader) readChunk(buf []byte) (int, bool) {
|
|
|
|
var read int
|
|
|
|
|
|
|
|
// read remaining tail
|
|
|
|
read = copy(buf, x.tailPayload)
|
|
|
|
|
|
|
|
x.tailPayload = x.tailPayload[read:]
|
|
|
|
|
|
|
|
if len(buf) == read {
|
|
|
|
return read, true
|
|
|
|
}
|
|
|
|
|
2022-02-24 11:33:43 +00:00
|
|
|
var chunk []byte
|
|
|
|
var lastRead int
|
|
|
|
|
|
|
|
for {
|
2022-08-25 11:45:50 +00:00
|
|
|
var resp v2object.GetResponse
|
|
|
|
x.err = x.stream.Read(&resp)
|
|
|
|
if x.err != nil {
|
2022-02-24 11:33:43 +00:00
|
|
|
return read, false
|
|
|
|
}
|
2022-02-07 20:27:56 +00:00
|
|
|
|
2022-08-25 11:45:50 +00:00
|
|
|
x.res.st, x.err = x.client.processResponse(&resp)
|
|
|
|
if x.err != nil || !apistatus.IsSuccessful(x.res.st) {
|
|
|
|
return read, false
|
|
|
|
}
|
2022-02-07 20:27:56 +00:00
|
|
|
|
2022-08-25 11:45:50 +00:00
|
|
|
part := resp.GetBody().GetObjectPart()
|
|
|
|
partChunk, ok := part.(*v2object.GetObjectPartChunk)
|
2022-02-24 11:33:43 +00:00
|
|
|
if !ok {
|
2022-08-25 11:45:50 +00:00
|
|
|
x.err = fmt.Errorf("unexpected message instead of chunk part: %T", part)
|
2022-02-24 11:33:43 +00:00
|
|
|
return read, false
|
|
|
|
}
|
2022-02-07 20:27:56 +00:00
|
|
|
|
2022-02-24 11:33:43 +00:00
|
|
|
// read new chunk
|
|
|
|
chunk = partChunk.GetChunk()
|
|
|
|
if len(chunk) == 0 {
|
|
|
|
// just skip empty chunks since they are not prohibited by protocol
|
|
|
|
continue
|
|
|
|
}
|
2022-02-07 20:27:56 +00:00
|
|
|
|
2022-02-24 11:33:43 +00:00
|
|
|
lastRead = copy(buf[read:], chunk)
|
2022-02-07 20:27:56 +00:00
|
|
|
|
2022-02-24 11:33:43 +00:00
|
|
|
read += lastRead
|
2022-02-07 20:27:56 +00:00
|
|
|
|
2022-02-24 11:33:43 +00:00
|
|
|
if read == len(buf) {
|
|
|
|
// save the tail
|
|
|
|
x.tailPayload = append(x.tailPayload, chunk[lastRead:]...)
|
2022-02-07 20:27:56 +00:00
|
|
|
|
2022-02-24 11:33:43 +00:00
|
|
|
return read, true
|
|
|
|
}
|
|
|
|
}
|
2022-02-07 20:27:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ReadChunk reads another chunk of the object payload. Works similar to
|
|
|
|
// io.Reader.Read but returns success flag instead of error.
|
|
|
|
//
|
|
|
|
// Failure reason can be received via Close.
|
|
|
|
func (x *ObjectReader) ReadChunk(buf []byte) (int, bool) {
|
|
|
|
return x.readChunk(buf)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (x *ObjectReader) close(ignoreEOF bool) (*ResObjectGet, error) {
|
|
|
|
defer x.cancelCtxStream()
|
|
|
|
|
2022-08-25 11:45:50 +00:00
|
|
|
if x.err != nil {
|
|
|
|
if !errors.Is(x.err, io.EOF) {
|
|
|
|
return nil, x.err
|
2022-02-07 20:27:56 +00:00
|
|
|
} else if !ignoreEOF {
|
2022-02-24 11:44:49 +00:00
|
|
|
if x.remainingPayloadLen > 0 {
|
|
|
|
return nil, io.ErrUnexpectedEOF
|
|
|
|
}
|
|
|
|
|
2022-02-07 20:27:56 +00:00
|
|
|
return nil, io.EOF
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-25 11:45:50 +00:00
|
|
|
return &x.res, nil
|
2022-02-07 20:27:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Close ends reading the object and returns the result of the operation
|
|
|
|
// along with the final results. Must be called after using the ObjectReader.
|
|
|
|
//
|
|
|
|
// Exactly one return value is non-nil. By default, server status is returned in res structure.
|
|
|
|
// Any client's internal or transport errors are returned as Go built-in error.
|
2022-12-29 10:46:18 +00:00
|
|
|
// If Client is tuned to resolve FrostFS API statuses, then FrostFS failures
|
2022-02-07 20:27:56 +00:00
|
|
|
// codes are returned as error.
|
|
|
|
//
|
|
|
|
// Return errors:
|
2022-08-24 14:17:40 +00:00
|
|
|
//
|
|
|
|
// *object.SplitInfoError (returned on virtual objects with PrmObjectGet.MakeRaw).
|
2024-04-27 20:06:15 +00:00
|
|
|
// *object.ECInfoError (returned on erasure-coded objects with PrmObjectGet.MakeRaw).
|
2022-02-07 20:27:56 +00:00
|
|
|
//
|
|
|
|
// Return statuses:
|
2022-02-28 10:56:46 +00:00
|
|
|
// - global (see Client docs);
|
|
|
|
// - *apistatus.ContainerNotFound;
|
|
|
|
// - *apistatus.ObjectNotFound;
|
|
|
|
// - *apistatus.ObjectAccessDenied;
|
|
|
|
// - *apistatus.ObjectAlreadyRemoved;
|
|
|
|
// - *apistatus.SessionTokenExpired.
|
2022-02-07 20:27:56 +00:00
|
|
|
func (x *ObjectReader) Close() (*ResObjectGet, error) {
|
|
|
|
return x.close(true)
|
|
|
|
}
|
|
|
|
|
2022-02-17 15:34:38 +00:00
|
|
|
// Read implements io.Reader of the object payload.
|
2022-02-07 20:27:56 +00:00
|
|
|
func (x *ObjectReader) Read(p []byte) (int, error) {
|
|
|
|
n, ok := x.readChunk(p)
|
2022-02-17 17:16:30 +00:00
|
|
|
|
|
|
|
x.remainingPayloadLen -= n
|
|
|
|
|
2022-02-07 20:27:56 +00:00
|
|
|
if !ok {
|
|
|
|
res, err := x.close(false)
|
|
|
|
if err != nil {
|
|
|
|
return n, err
|
|
|
|
}
|
2022-02-16 17:14:24 +00:00
|
|
|
|
|
|
|
return n, apistatus.ErrFromStatus(res.Status())
|
2022-02-07 20:27:56 +00:00
|
|
|
}
|
|
|
|
|
2022-02-17 17:16:30 +00:00
|
|
|
if x.remainingPayloadLen < 0 {
|
2022-02-16 17:14:24 +00:00
|
|
|
return n, errors.New("payload size overflow")
|
|
|
|
}
|
|
|
|
|
2022-02-07 20:27:56 +00:00
|
|
|
return n, nil
|
|
|
|
}
|
|
|
|
|
2022-12-29 10:46:18 +00:00
|
|
|
// ObjectGetInit initiates reading an object through a remote server using FrostFS API protocol.
|
2022-02-07 20:27:56 +00:00
|
|
|
//
|
2022-02-11 16:13:53 +00:00
|
|
|
// The call only opens the transmission channel, explicit fetching is done using the ObjectReader.
|
2022-02-07 20:27:56 +00:00
|
|
|
// Exactly one return value is non-nil. Resulting reader must be finally closed.
|
|
|
|
//
|
2023-02-27 07:53:27 +00:00
|
|
|
// Returns an error if parameters are set incorrectly (see PrmObjectGet docs).
|
2022-02-07 20:27:56 +00:00
|
|
|
// Context is required and must not be nil. It is used for network communication.
|
|
|
|
func (c *Client) ObjectGetInit(ctx context.Context, prm PrmObjectGet) (*ObjectReader, error) {
|
2023-08-28 08:07:18 +00:00
|
|
|
req, err := prm.buildRequest(c)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2022-02-07 20:27:56 +00:00
|
|
|
}
|
|
|
|
|
2023-08-28 08:07:18 +00:00
|
|
|
key := prm.Key
|
2022-09-14 06:02:26 +00:00
|
|
|
if key == nil {
|
2023-11-13 12:18:07 +00:00
|
|
|
key = &c.prm.Key
|
2022-09-14 06:02:26 +00:00
|
|
|
}
|
|
|
|
|
2023-08-28 08:07:18 +00:00
|
|
|
err = signature.SignServiceMessage(key, req)
|
2022-08-25 11:45:50 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("sign request: %w", err)
|
|
|
|
}
|
2022-02-07 20:27:56 +00:00
|
|
|
|
2022-08-25 11:45:50 +00:00
|
|
|
ctx, cancel := context.WithCancel(ctx)
|
2022-02-07 20:27:56 +00:00
|
|
|
|
2023-08-28 08:07:18 +00:00
|
|
|
stream, err := rpcapi.GetObject(&c.c, req, client.WithContext(ctx))
|
2022-08-25 11:45:50 +00:00
|
|
|
if err != nil {
|
|
|
|
cancel()
|
|
|
|
return nil, fmt.Errorf("open stream: %w", err)
|
2022-02-07 20:27:56 +00:00
|
|
|
}
|
|
|
|
|
2022-08-25 11:45:50 +00:00
|
|
|
var r ObjectReader
|
|
|
|
r.cancelCtxStream = cancel
|
|
|
|
r.stream = stream
|
|
|
|
r.client = c
|
|
|
|
|
2022-02-07 20:27:56 +00:00
|
|
|
return &r, nil
|
|
|
|
}
|
2022-02-11 16:13:53 +00:00
|
|
|
|
|
|
|
// PrmObjectHead groups parameters of ObjectHead operation.
|
|
|
|
type PrmObjectHead struct {
|
2023-08-28 08:07:18 +00:00
|
|
|
XHeaders []string
|
|
|
|
|
|
|
|
BearerToken *bearer.Token
|
|
|
|
|
|
|
|
Session *session.Object
|
|
|
|
|
|
|
|
Raw bool
|
2022-02-21 12:02:23 +00:00
|
|
|
|
2023-08-28 08:07:18 +00:00
|
|
|
Local bool
|
|
|
|
|
|
|
|
ContainerID *cid.ID
|
|
|
|
|
|
|
|
ObjectID *oid.ID
|
|
|
|
|
|
|
|
Key *ecdsa.PrivateKey
|
2022-02-21 12:02:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// UseKey specifies private key to sign the requests.
|
|
|
|
// If key is not provided, then Client default key is used.
|
2023-08-28 08:07:18 +00:00
|
|
|
//
|
|
|
|
// Deprecated: Use PrmObjectHead.Key instead.
|
|
|
|
func (prm *PrmObjectHead) UseKey(key ecdsa.PrivateKey) {
|
|
|
|
prm.Key = &key
|
2022-02-11 16:13:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ResObjectHead groups resulting values of ObjectHead operation.
|
|
|
|
type ResObjectHead struct {
|
|
|
|
statusRes
|
|
|
|
|
|
|
|
// requested object (response doesn't carry the ID)
|
|
|
|
idObj oid.ID
|
|
|
|
|
|
|
|
hdr *v2object.HeaderWithSignature
|
|
|
|
}
|
|
|
|
|
|
|
|
// ReadHeader reads header of the requested object.
|
|
|
|
// Returns false if header is missing in the response (not read).
|
|
|
|
func (x *ResObjectHead) ReadHeader(dst *object.Object) bool {
|
|
|
|
if x.hdr == nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
var objv2 v2object.Object
|
|
|
|
|
|
|
|
objv2.SetHeader(x.hdr.GetHeader())
|
|
|
|
objv2.SetSignature(x.hdr.GetSignature())
|
|
|
|
|
2022-03-03 11:48:55 +00:00
|
|
|
obj := object.NewFromV2(&objv2)
|
2022-04-11 16:25:14 +00:00
|
|
|
obj.SetID(x.idObj)
|
2022-02-11 16:13:53 +00:00
|
|
|
|
2022-03-03 11:48:55 +00:00
|
|
|
*dst = *obj
|
2022-02-11 16:13:53 +00:00
|
|
|
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2023-08-28 08:07:18 +00:00
|
|
|
func (prm *PrmObjectHead) buildRequest(c *Client) (*v2object.HeadRequest, error) {
|
|
|
|
if prm.ContainerID == nil {
|
|
|
|
return nil, errorMissingContainer
|
|
|
|
}
|
|
|
|
|
|
|
|
if prm.ObjectID == nil {
|
|
|
|
return nil, errorMissingObject
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(prm.XHeaders)%2 != 0 {
|
|
|
|
return nil, errorInvalidXHeaders
|
|
|
|
}
|
|
|
|
|
|
|
|
meta := new(v2session.RequestMetaHeader)
|
|
|
|
writeXHeadersToMeta(prm.XHeaders, meta)
|
|
|
|
|
|
|
|
if prm.BearerToken != nil {
|
|
|
|
v2BearerToken := new(acl.BearerToken)
|
|
|
|
prm.BearerToken.WriteToV2(v2BearerToken)
|
|
|
|
meta.SetBearerToken(v2BearerToken)
|
|
|
|
}
|
|
|
|
|
|
|
|
if prm.Session != nil {
|
|
|
|
v2SessionToken := new(v2session.Token)
|
|
|
|
prm.Session.WriteToV2(v2SessionToken)
|
|
|
|
meta.SetSessionToken(v2SessionToken)
|
|
|
|
}
|
|
|
|
|
|
|
|
if prm.Local {
|
|
|
|
meta.SetTTL(1)
|
|
|
|
}
|
|
|
|
|
|
|
|
addr := new(v2refs.Address)
|
|
|
|
|
|
|
|
cnrV2 := new(v2refs.ContainerID)
|
|
|
|
prm.ContainerID.WriteToV2(cnrV2)
|
|
|
|
addr.SetContainerID(cnrV2)
|
|
|
|
|
|
|
|
objV2 := new(v2refs.ObjectID)
|
|
|
|
prm.ObjectID.WriteToV2(objV2)
|
|
|
|
addr.SetObjectID(objV2)
|
|
|
|
body := new(v2object.HeadRequestBody)
|
|
|
|
body.SetRaw(prm.Raw)
|
|
|
|
body.SetAddress(addr)
|
|
|
|
|
|
|
|
req := new(v2object.HeadRequest)
|
|
|
|
req.SetBody(body)
|
|
|
|
c.prepareRequest(req, meta)
|
|
|
|
|
|
|
|
return req, nil
|
|
|
|
}
|
|
|
|
|
2022-12-29 10:46:18 +00:00
|
|
|
// ObjectHead reads object header through a remote server using FrostFS API protocol.
|
2022-02-11 16:13:53 +00:00
|
|
|
//
|
|
|
|
// Exactly one return value is non-nil. By default, server status is returned in res structure.
|
|
|
|
// Any client's internal or transport errors are returned as `error`,
|
2023-09-15 15:52:08 +00:00
|
|
|
// If PrmInit.DisableFrostFSFailuresResolution has been called, unsuccessful
|
2023-09-12 15:57:04 +00:00
|
|
|
// FrostFS status codes are included in the returned result structure,
|
|
|
|
// otherwise, are also returned as `error`.
|
2022-02-11 16:13:53 +00:00
|
|
|
//
|
2023-02-27 07:53:27 +00:00
|
|
|
// Returns an error if parameters are set incorrectly (see PrmObjectHead docs).
|
2022-02-11 16:13:53 +00:00
|
|
|
// Context is required and must not be nil. It is used for network communication.
|
|
|
|
//
|
|
|
|
// Return errors:
|
2022-08-24 14:17:40 +00:00
|
|
|
//
|
|
|
|
// *object.SplitInfoError (returned on virtual objects with PrmObjectHead.MakeRaw).
|
2024-04-27 20:06:15 +00:00
|
|
|
// *object.ECInfoError (returned on erasure-coded objects with PrmObjectHead.MakeRaw).
|
2022-02-11 16:13:53 +00:00
|
|
|
//
|
|
|
|
// Return statuses:
|
2022-02-28 10:56:46 +00:00
|
|
|
// - global (see Client docs);
|
|
|
|
// - *apistatus.ContainerNotFound;
|
|
|
|
// - *apistatus.ObjectNotFound;
|
|
|
|
// - *apistatus.ObjectAccessDenied;
|
|
|
|
// - *apistatus.ObjectAlreadyRemoved;
|
|
|
|
// - *apistatus.SessionTokenExpired.
|
2022-02-11 16:13:53 +00:00
|
|
|
func (c *Client) ObjectHead(ctx context.Context, prm PrmObjectHead) (*ResObjectHead, error) {
|
2023-08-28 08:07:18 +00:00
|
|
|
req, err := prm.buildRequest(c)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2022-02-11 16:13:53 +00:00
|
|
|
}
|
|
|
|
|
2023-11-13 12:18:07 +00:00
|
|
|
key := c.prm.Key
|
2023-08-28 08:07:18 +00:00
|
|
|
if prm.Key != nil {
|
|
|
|
key = *prm.Key
|
2022-08-25 10:42:43 +00:00
|
|
|
}
|
2022-02-11 16:13:53 +00:00
|
|
|
|
2022-08-25 10:42:43 +00:00
|
|
|
// sign the request
|
2023-08-28 08:07:18 +00:00
|
|
|
|
|
|
|
err = signature.SignServiceMessage(&key, req)
|
2022-08-25 10:42:43 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("sign request: %w", err)
|
|
|
|
}
|
2022-02-11 16:13:53 +00:00
|
|
|
|
2023-08-28 08:07:18 +00:00
|
|
|
resp, err := rpcapi.HeadObject(&c.c, req, client.WithContext(ctx))
|
2022-08-25 10:42:43 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("write request: %w", err)
|
2022-02-21 12:02:23 +00:00
|
|
|
}
|
|
|
|
|
2022-08-25 10:42:43 +00:00
|
|
|
var res ResObjectHead
|
|
|
|
res.st, err = c.processResponse(resp)
|
2024-09-24 13:31:56 +00:00
|
|
|
if err != nil || !apistatus.IsSuccessful(res.st) {
|
|
|
|
return &res, err
|
2022-02-11 16:13:53 +00:00
|
|
|
}
|
|
|
|
|
2023-08-28 08:07:18 +00:00
|
|
|
res.idObj = *prm.ObjectID
|
2022-08-25 10:42:43 +00:00
|
|
|
|
|
|
|
switch v := resp.GetBody().GetHeaderPart().(type) {
|
|
|
|
default:
|
|
|
|
return nil, fmt.Errorf("unexpected header type %T", v)
|
|
|
|
case *v2object.SplitInfo:
|
|
|
|
return nil, object.NewSplitInfoError(object.NewSplitInfoFromV2(v))
|
2024-04-22 06:51:20 +00:00
|
|
|
case *v2object.ECInfo:
|
|
|
|
return nil, object.NewECInfoError(object.NewECInfoFromV2(v))
|
2022-08-25 10:42:43 +00:00
|
|
|
case *v2object.HeaderWithSignature:
|
|
|
|
res.hdr = v
|
2022-02-11 16:13:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return &res, nil
|
|
|
|
}
|
2022-02-12 09:26:38 +00:00
|
|
|
|
|
|
|
// PrmObjectRange groups parameters of ObjectRange operation.
|
|
|
|
type PrmObjectRange struct {
|
2023-08-28 08:07:18 +00:00
|
|
|
XHeaders []string
|
2022-02-12 09:26:38 +00:00
|
|
|
|
2023-08-28 08:07:18 +00:00
|
|
|
BearerToken *bearer.Token
|
2022-08-25 11:32:37 +00:00
|
|
|
|
2023-08-28 08:07:18 +00:00
|
|
|
Session *session.Object
|
|
|
|
|
|
|
|
Raw bool
|
|
|
|
|
|
|
|
Local bool
|
|
|
|
|
|
|
|
ContainerID *cid.ID
|
|
|
|
|
|
|
|
ObjectID *oid.ID
|
2022-02-12 09:26:38 +00:00
|
|
|
|
2023-08-28 08:07:18 +00:00
|
|
|
Key *ecdsa.PrivateKey
|
|
|
|
|
|
|
|
Offset uint64
|
|
|
|
|
|
|
|
Length uint64
|
2022-02-12 09:26:38 +00:00
|
|
|
}
|
|
|
|
|
2023-08-28 08:07:18 +00:00
|
|
|
func (prm *PrmObjectRange) buildRequest(c *Client) (*v2object.GetRangeRequest, error) {
|
|
|
|
if prm.Length == 0 {
|
|
|
|
return nil, errorZeroRangeLength
|
|
|
|
}
|
|
|
|
|
|
|
|
if prm.ContainerID == nil {
|
|
|
|
return nil, errorMissingContainer
|
|
|
|
}
|
|
|
|
|
|
|
|
if prm.ObjectID == nil {
|
|
|
|
return nil, errorMissingObject
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(prm.XHeaders)%2 != 0 {
|
|
|
|
return nil, errorInvalidXHeaders
|
|
|
|
}
|
|
|
|
|
|
|
|
meta := new(v2session.RequestMetaHeader)
|
|
|
|
writeXHeadersToMeta(prm.XHeaders, meta)
|
|
|
|
|
|
|
|
if prm.BearerToken != nil {
|
|
|
|
v2BearerToken := new(acl.BearerToken)
|
|
|
|
prm.BearerToken.WriteToV2(v2BearerToken)
|
|
|
|
meta.SetBearerToken(v2BearerToken)
|
|
|
|
}
|
|
|
|
|
|
|
|
if prm.Session != nil {
|
|
|
|
v2SessionToken := new(v2session.Token)
|
|
|
|
prm.Session.WriteToV2(v2SessionToken)
|
|
|
|
meta.SetSessionToken(v2SessionToken)
|
|
|
|
}
|
|
|
|
|
|
|
|
if prm.Local {
|
|
|
|
meta.SetTTL(1)
|
|
|
|
}
|
|
|
|
|
|
|
|
addr := new(v2refs.Address)
|
|
|
|
|
|
|
|
cnrV2 := new(v2refs.ContainerID)
|
|
|
|
prm.ContainerID.WriteToV2(cnrV2)
|
|
|
|
addr.SetContainerID(cnrV2)
|
|
|
|
|
|
|
|
objV2 := new(v2refs.ObjectID)
|
|
|
|
prm.ObjectID.WriteToV2(objV2)
|
|
|
|
addr.SetObjectID(objV2)
|
|
|
|
|
|
|
|
rng := new(v2object.Range)
|
|
|
|
rng.SetLength(prm.Length)
|
|
|
|
rng.SetOffset(prm.Offset)
|
|
|
|
|
|
|
|
body := new(v2object.GetRangeRequestBody)
|
|
|
|
body.SetRaw(prm.Raw)
|
|
|
|
body.SetAddress(addr)
|
|
|
|
body.SetRange(rng)
|
|
|
|
|
|
|
|
req := new(v2object.GetRangeRequest)
|
|
|
|
req.SetBody(body)
|
|
|
|
c.prepareRequest(req, meta)
|
|
|
|
|
|
|
|
return req, nil
|
2022-02-12 09:26:38 +00:00
|
|
|
}
|
|
|
|
|
2022-08-25 11:32:37 +00:00
|
|
|
// UseKey specifies private key to sign the requests.
|
|
|
|
// If key is not provided, then Client default key is used.
|
2023-08-28 08:07:18 +00:00
|
|
|
//
|
|
|
|
// Deprecated: Use PrmObjectRange.Key instead.
|
|
|
|
func (prm *PrmObjectRange) UseKey(key ecdsa.PrivateKey) {
|
|
|
|
prm.Key = &key
|
2022-08-25 11:32:37 +00:00
|
|
|
}
|
|
|
|
|
2022-02-12 09:26:38 +00:00
|
|
|
// ResObjectRange groups the final result values of ObjectRange operation.
|
|
|
|
type ResObjectRange struct {
|
|
|
|
statusRes
|
|
|
|
}
|
|
|
|
|
|
|
|
// ObjectRangeReader is designed to read payload range of one object
|
2022-12-29 10:46:18 +00:00
|
|
|
// from FrostFS system.
|
2022-02-12 09:26:38 +00:00
|
|
|
//
|
|
|
|
// Must be initialized using Client.ObjectRangeInit, any other
|
|
|
|
// usage is unsafe.
|
|
|
|
type ObjectRangeReader struct {
|
|
|
|
cancelCtxStream context.CancelFunc
|
|
|
|
|
2022-08-25 11:32:37 +00:00
|
|
|
client *Client
|
2022-02-12 09:26:38 +00:00
|
|
|
|
2022-08-25 11:32:37 +00:00
|
|
|
res ResObjectRange
|
|
|
|
err error
|
2022-02-12 09:26:38 +00:00
|
|
|
|
2022-08-25 11:32:37 +00:00
|
|
|
stream interface {
|
|
|
|
Read(resp *v2object.GetRangeResponse) error
|
|
|
|
}
|
2022-02-12 09:26:38 +00:00
|
|
|
|
|
|
|
tailPayload []byte
|
2022-02-16 17:14:24 +00:00
|
|
|
|
|
|
|
remainingPayloadLen int
|
2022-02-12 09:26:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (x *ObjectRangeReader) readChunk(buf []byte) (int, bool) {
|
|
|
|
var read int
|
|
|
|
|
|
|
|
// read remaining tail
|
|
|
|
read = copy(buf, x.tailPayload)
|
|
|
|
|
|
|
|
x.tailPayload = x.tailPayload[read:]
|
|
|
|
|
|
|
|
if len(buf) == read {
|
|
|
|
return read, true
|
|
|
|
}
|
|
|
|
|
|
|
|
var partChunk *v2object.GetRangePartChunk
|
2022-02-24 11:33:43 +00:00
|
|
|
var chunk []byte
|
|
|
|
var lastRead int
|
|
|
|
|
|
|
|
for {
|
2022-08-25 11:32:37 +00:00
|
|
|
var resp v2object.GetRangeResponse
|
|
|
|
x.err = x.stream.Read(&resp)
|
|
|
|
if x.err != nil {
|
|
|
|
return read, false
|
|
|
|
}
|
|
|
|
|
|
|
|
x.res.st, x.err = x.client.processResponse(&resp)
|
|
|
|
if x.err != nil || !apistatus.IsSuccessful(x.res.st) {
|
2022-02-24 11:33:43 +00:00
|
|
|
return read, false
|
|
|
|
}
|
2022-02-12 09:26:38 +00:00
|
|
|
|
2022-02-24 11:33:43 +00:00
|
|
|
// get chunk message
|
2022-08-25 11:32:37 +00:00
|
|
|
switch v := resp.GetBody().GetRangePart().(type) {
|
2022-02-24 11:33:43 +00:00
|
|
|
default:
|
2022-08-25 11:32:37 +00:00
|
|
|
x.err = fmt.Errorf("unexpected message received: %T", v)
|
2022-02-24 11:33:43 +00:00
|
|
|
return read, false
|
|
|
|
case *v2object.SplitInfo:
|
2022-08-25 11:32:37 +00:00
|
|
|
x.err = object.NewSplitInfoError(object.NewSplitInfoFromV2(v))
|
2022-02-24 11:33:43 +00:00
|
|
|
return read, false
|
2024-04-27 20:06:15 +00:00
|
|
|
case *v2object.ECInfo:
|
|
|
|
x.err = object.NewECInfoError(object.NewECInfoFromV2(v))
|
|
|
|
return read, false
|
2022-02-24 11:33:43 +00:00
|
|
|
case *v2object.GetRangePartChunk:
|
|
|
|
partChunk = v
|
|
|
|
}
|
2022-02-12 09:26:38 +00:00
|
|
|
|
2022-02-24 11:33:43 +00:00
|
|
|
chunk = partChunk.GetChunk()
|
|
|
|
if len(chunk) == 0 {
|
|
|
|
// just skip empty chunks since they are not prohibited by protocol
|
|
|
|
continue
|
|
|
|
}
|
2022-02-12 09:26:38 +00:00
|
|
|
|
2022-02-24 11:33:43 +00:00
|
|
|
lastRead = copy(buf[read:], chunk)
|
2022-02-12 09:26:38 +00:00
|
|
|
|
2022-02-24 11:33:43 +00:00
|
|
|
read += lastRead
|
2022-02-12 09:26:38 +00:00
|
|
|
|
2022-02-24 11:33:43 +00:00
|
|
|
if read == len(buf) {
|
|
|
|
// save the tail
|
|
|
|
x.tailPayload = append(x.tailPayload, chunk[lastRead:]...)
|
2022-02-12 09:26:38 +00:00
|
|
|
|
2022-02-24 11:33:43 +00:00
|
|
|
return read, true
|
|
|
|
}
|
|
|
|
}
|
2022-02-12 09:26:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ReadChunk reads another chunk of the object payload range.
|
|
|
|
// Works similar to io.Reader.Read but returns success flag instead of error.
|
|
|
|
//
|
|
|
|
// Failure reason can be received via Close.
|
|
|
|
func (x *ObjectRangeReader) ReadChunk(buf []byte) (int, bool) {
|
|
|
|
return x.readChunk(buf)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (x *ObjectRangeReader) close(ignoreEOF bool) (*ResObjectRange, error) {
|
|
|
|
defer x.cancelCtxStream()
|
|
|
|
|
2022-08-25 11:32:37 +00:00
|
|
|
if x.err != nil {
|
|
|
|
if !errors.Is(x.err, io.EOF) {
|
|
|
|
return nil, x.err
|
2022-02-12 09:26:38 +00:00
|
|
|
} else if !ignoreEOF {
|
2022-02-24 11:44:49 +00:00
|
|
|
if x.remainingPayloadLen > 0 {
|
|
|
|
return nil, io.ErrUnexpectedEOF
|
|
|
|
}
|
|
|
|
|
2022-02-12 09:26:38 +00:00
|
|
|
return nil, io.EOF
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-25 11:32:37 +00:00
|
|
|
return &x.res, nil
|
2022-02-12 09:26:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Close ends reading the payload range and returns the result of the operation
|
|
|
|
// along with the final results. Must be called after using the ObjectRangeReader.
|
|
|
|
//
|
|
|
|
// Exactly one return value is non-nil. By default, server status is returned in res structure.
|
|
|
|
// Any client's internal or transport errors are returned as Go built-in error.
|
2022-12-29 10:46:18 +00:00
|
|
|
// If Client is tuned to resolve FrostFS API statuses, then FrostFS failures
|
2022-02-12 09:26:38 +00:00
|
|
|
// codes are returned as error.
|
|
|
|
//
|
|
|
|
// Return errors:
|
2022-08-24 14:17:40 +00:00
|
|
|
//
|
|
|
|
// *object.SplitInfoError (returned on virtual objects with PrmObjectRange.MakeRaw).
|
2024-04-27 20:06:15 +00:00
|
|
|
// *object.ECInfoError (returned on erasure-coded objects with PrmObjectRange.MakeRaw).
|
2022-02-12 09:26:38 +00:00
|
|
|
//
|
|
|
|
// Return statuses:
|
2022-02-28 10:56:46 +00:00
|
|
|
// - global (see Client docs);
|
|
|
|
// - *apistatus.ContainerNotFound;
|
|
|
|
// - *apistatus.ObjectNotFound;
|
|
|
|
// - *apistatus.ObjectAccessDenied;
|
|
|
|
// - *apistatus.ObjectAlreadyRemoved;
|
2022-06-29 17:24:56 +00:00
|
|
|
// - *apistatus.ObjectOutOfRange;
|
2022-02-28 10:56:46 +00:00
|
|
|
// - *apistatus.SessionTokenExpired.
|
2022-02-12 09:26:38 +00:00
|
|
|
func (x *ObjectRangeReader) Close() (*ResObjectRange, error) {
|
|
|
|
return x.close(true)
|
|
|
|
}
|
|
|
|
|
2022-02-17 15:34:38 +00:00
|
|
|
// Read implements io.Reader of the object payload.
|
2022-02-12 09:26:38 +00:00
|
|
|
func (x *ObjectRangeReader) Read(p []byte) (int, error) {
|
|
|
|
n, ok := x.readChunk(p)
|
2022-02-17 17:16:30 +00:00
|
|
|
|
|
|
|
x.remainingPayloadLen -= n
|
|
|
|
|
2022-02-12 09:26:38 +00:00
|
|
|
if !ok {
|
|
|
|
res, err := x.close(false)
|
|
|
|
if err != nil {
|
|
|
|
return n, err
|
|
|
|
}
|
2022-02-16 17:14:24 +00:00
|
|
|
|
|
|
|
return n, apistatus.ErrFromStatus(res.Status())
|
|
|
|
}
|
|
|
|
|
2022-02-17 17:16:30 +00:00
|
|
|
if x.remainingPayloadLen < 0 {
|
2022-02-16 17:14:24 +00:00
|
|
|
return n, errors.New("payload range size overflow")
|
2022-02-12 09:26:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return n, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// ObjectRangeInit initiates reading an object's payload range through a remote
|
2022-12-29 10:46:18 +00:00
|
|
|
// server using FrostFS API protocol.
|
2022-02-12 09:26:38 +00:00
|
|
|
//
|
|
|
|
// The call only opens the transmission channel, explicit fetching is done using the ObjectRangeReader.
|
|
|
|
// Exactly one return value is non-nil. Resulting reader must be finally closed.
|
|
|
|
//
|
2023-02-27 07:53:27 +00:00
|
|
|
// Returns an error if parameters are set incorrectly (see PrmObjectRange docs).
|
2022-02-12 09:26:38 +00:00
|
|
|
// Context is required and must not be nil. It is used for network communication.
|
|
|
|
func (c *Client) ObjectRangeInit(ctx context.Context, prm PrmObjectRange) (*ObjectRangeReader, error) {
|
2023-08-28 08:07:18 +00:00
|
|
|
req, err := prm.buildRequest(c)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2022-02-12 09:26:38 +00:00
|
|
|
}
|
|
|
|
|
2023-08-28 08:07:18 +00:00
|
|
|
key := prm.Key
|
2022-08-29 10:13:21 +00:00
|
|
|
if key == nil {
|
2023-11-13 12:18:07 +00:00
|
|
|
key = &c.prm.Key
|
2022-08-29 10:13:21 +00:00
|
|
|
}
|
|
|
|
|
2023-08-28 08:07:18 +00:00
|
|
|
err = signature.SignServiceMessage(key, req)
|
2022-08-29 10:13:21 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("sign request: %w", err)
|
|
|
|
}
|
|
|
|
|
2022-08-25 11:32:37 +00:00
|
|
|
ctx, cancel := context.WithCancel(ctx)
|
2022-02-12 09:26:38 +00:00
|
|
|
|
2023-08-28 08:07:18 +00:00
|
|
|
stream, err := rpcapi.GetObjectRange(&c.c, req, client.WithContext(ctx))
|
2022-08-25 11:32:37 +00:00
|
|
|
if err != nil {
|
|
|
|
cancel()
|
|
|
|
return nil, fmt.Errorf("open stream: %w", err)
|
2022-02-12 09:26:38 +00:00
|
|
|
}
|
|
|
|
|
2022-08-25 11:32:37 +00:00
|
|
|
var r ObjectRangeReader
|
2023-08-28 08:07:18 +00:00
|
|
|
r.remainingPayloadLen = int(prm.Length)
|
2022-08-25 11:32:37 +00:00
|
|
|
r.cancelCtxStream = cancel
|
|
|
|
r.stream = stream
|
|
|
|
r.client = c
|
|
|
|
|
2022-02-12 09:26:38 +00:00
|
|
|
return &r, nil
|
|
|
|
}
|