[#131] client: Add `GetFullObject` helper function

Add helper function which accepts container and object identifiers and
returns object instance read to the memory.

Signed-off-by: Leonard Lyubich <leonard@nspcc.ru>
remotes/fyrchik/fix-lint
Leonard Lyubich 2022-02-17 20:17:23 +03:00 committed by LeL
parent 32458baeb7
commit b0f7f75929
1 changed files with 38 additions and 0 deletions

View File

@ -342,6 +342,44 @@ func (c *Client) ObjectGetInit(ctx context.Context, prm PrmObjectGet) (*ObjectRe
return &r, nil
}
// GetFullObject reads object header and full payload to the memory and returns
// the result.
//
// Function behavior equivalent to Client.ObjectGetInit, see documentation for details.
func GetFullObject(ctx context.Context, c *Client, idCnr cid.ID, idObj oid.ID) (*object.Object, error) {
var prm PrmObjectGet
prm.FromContainer(idCnr)
prm.ByID(idObj)
rdr, err := c.ObjectGetInit(ctx, prm)
if err != nil {
return nil, fmt.Errorf("init object reading: %w", err)
}
var obj object.Object
if rdr.ReadHeader(&obj) {
payload, err := io.ReadAll(rdr)
if err != nil {
return nil, fmt.Errorf("read payload: %w", err)
}
object.NewRawFrom(&obj).SetPayload(payload)
}
res, err := rdr.Close()
if err == nil {
err = apistatus.ErrFromStatus(res.Status())
}
if err != nil {
return nil, fmt.Errorf("finish object reading: %w", err)
}
return &obj, nil
}
// PrmObjectHead groups parameters of ObjectHead operation.
type PrmObjectHead struct {
prmObjectRead