From b0f7f7592967bfa892fd0e8514aed4ec37bb6a45 Mon Sep 17 00:00:00 2001 From: Leonard Lyubich Date: Thu, 17 Feb 2022 20:17:23 +0300 Subject: [PATCH] [#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 --- client/object_get.go | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/client/object_get.go b/client/object_get.go index 55fe239f..373cddaf 100644 --- a/client/object_get.go +++ b/client/object_get.go @@ -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