Skip to content

Commit 705a775

Browse files
neofs: add GetSDKClient, ObjectSearch and GetWithClient
Could be used for operation with neofs. Signed-off-by: Ekaterina Pavlova <ekt@morphbits.io>
1 parent 89a3b01 commit 705a775

File tree

2 files changed

+94
-24
lines changed

2 files changed

+94
-24
lines changed

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ require (
3030
golang.org/x/term v0.23.0
3131
golang.org/x/text v0.17.0
3232
golang.org/x/tools v0.24.0
33+
google.golang.org/grpc v1.62.0
3334
gopkg.in/yaml.v3 v3.0.1
3435
)
3536

@@ -72,7 +73,6 @@ require (
7273
golang.org/x/sync v0.8.0 // indirect
7374
golang.org/x/sys v0.23.0 // indirect
7475
google.golang.org/genproto/googleapis/rpc v0.0.0-20240221002015-b0ce06bbee7c // indirect
75-
google.golang.org/grpc v1.62.0 // indirect
7676
google.golang.org/protobuf v1.34.2 // indirect
7777
rsc.io/tmplfunc v0.0.3 // indirect
7878
)

pkg/services/oracle/neofs/neofs.go

Lines changed: 93 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"net/url"
1010
"strconv"
1111
"strings"
12+
"time"
1213

1314
"github.com/nspcc-dev/neo-go/pkg/crypto/keys"
1415
"github.com/nspcc-dev/neo-go/pkg/util"
@@ -17,6 +18,8 @@ import (
1718
"github.com/nspcc-dev/neofs-sdk-go/object"
1819
oid "github.com/nspcc-dev/neofs-sdk-go/object/id"
1920
"github.com/nspcc-dev/neofs-sdk-go/user"
21+
"google.golang.org/grpc/codes"
22+
"google.golang.org/grpc/status"
2023
)
2124

2225
const (
@@ -45,41 +48,48 @@ var (
4548
// URI scheme is "neofs:<Container-ID>/<Object-ID/<Command>/<Params>".
4649
// If Command is not provided, full object is requested.
4750
func Get(ctx context.Context, priv *keys.PrivateKey, u *url.URL, addr string) (io.ReadCloser, error) {
48-
objectAddr, ps, err := parseNeoFSURL(u)
51+
c, err := GetSDKClient(ctx, addr, 0)
4952
if err != nil {
50-
return nil, err
53+
return clientCloseWrapper{c: c}, fmt.Errorf("failed to create client: %w", err)
5154
}
55+
return GetWithClient(ctx, c, priv, u, true)
56+
}
5257

53-
c, err := client.New(client.PrmInit{})
58+
// GetWithClient returns a neofs object from the provided url using the provided client.
59+
// URI scheme is "neofs:<Container-ID>/<Object-ID/<Command>/<Params>".
60+
// If Command is not provided, full object is requested. If wrapClientCloser is true,
61+
// the client will be closed when the returned ReadCloser is closed.
62+
func GetWithClient(ctx context.Context, c *client.Client, priv *keys.PrivateKey, u *url.URL, wrapClientCloser bool) (io.ReadCloser, error) {
63+
objectAddr, ps, err := parseNeoFSURL(u)
5464
if err != nil {
55-
return nil, fmt.Errorf("failed to create client: %w", err)
65+
return nil, err
5666
}
57-
5867
var (
59-
res = clientCloseWrapper{c: c}
60-
prmd client.PrmDial
68+
res io.ReadCloser
69+
s = user.NewAutoIDSignerRFC6979(priv.PrivateKey)
6170
)
62-
prmd.SetServerURI(addr)
63-
prmd.SetContext(ctx)
64-
err = c.Dial(prmd) //nolint:contextcheck // contextcheck: Function `Dial->Balance->SendUnary->Init->setNeoFSAPIServer` should pass the context parameter
65-
if err != nil {
66-
return res, err
67-
}
68-
69-
var s = user.NewAutoIDSignerRFC6979(priv.PrivateKey)
7071
switch {
71-
case len(ps) == 0 || ps[0] == "": // Get request
72-
res.ReadCloser, err = getPayload(ctx, s, c, objectAddr)
72+
case len(ps) == 0 || ps[0] == "":
73+
res, err = getPayload(ctx, s, c, objectAddr)
7374
case ps[0] == rangeCmd:
74-
res.ReadCloser, err = getRange(ctx, s, c, objectAddr, ps[1:]...)
75+
res, err = getRange(ctx, s, c, objectAddr, ps[1:]...)
7576
case ps[0] == headerCmd:
76-
res.ReadCloser, err = getHeader(ctx, s, c, objectAddr)
77+
res, err = getHeader(ctx, s, c, objectAddr)
7778
case ps[0] == hashCmd:
78-
res.ReadCloser, err = getHash(ctx, s, c, objectAddr, ps[1:]...)
79+
res, err = getHash(ctx, s, c, objectAddr, ps[1:]...)
7980
default:
80-
err = ErrInvalidCommand
81+
return nil, ErrInvalidCommand
82+
}
83+
if err != nil {
84+
return nil, err
85+
}
86+
if wrapClientCloser {
87+
return clientCloseWrapper{
88+
c: c,
89+
ReadCloser: res,
90+
}, nil
8191
}
82-
return res, err
92+
return res, nil
8393
}
8494

8595
type clientCloseWrapper struct {
@@ -92,7 +102,12 @@ func (w clientCloseWrapper) Close() error {
92102
if w.ReadCloser != nil {
93103
res = w.ReadCloser.Close()
94104
}
95-
w.c.Close()
105+
if w.c != nil {
106+
closeErr := w.c.Close()
107+
if closeErr != nil && res == nil {
108+
res = closeErr
109+
}
110+
}
96111
return res
97112
}
98113

@@ -220,3 +235,58 @@ func parseRange(s string) (*object.Range, error) {
220235
r.SetLength(length)
221236
return r, nil
222237
}
238+
239+
// ObjectSearch returns a list of object IDs from the provided container.
240+
func ObjectSearch(ctx context.Context, c *client.Client, priv *keys.PrivateKey, containerIDStr string, prm client.PrmObjectSearch) ([]oid.ID, error) {
241+
var (
242+
s = user.NewAutoIDSignerRFC6979(priv.PrivateKey)
243+
objectIDs []oid.ID
244+
containerID cid.ID
245+
)
246+
err := containerID.DecodeString(containerIDStr)
247+
if err != nil {
248+
return nil, fmt.Errorf("%w: %w", ErrInvalidContainer, err)
249+
}
250+
reader, err := c.ObjectSearchInit(ctx, containerID, s, prm)
251+
if err != nil {
252+
return nil, fmt.Errorf("failed to initiate object search: %w", err)
253+
}
254+
defer reader.Close()
255+
256+
err = reader.Iterate(func(oid oid.ID) bool {
257+
objectIDs = append(objectIDs, oid)
258+
return false
259+
})
260+
if err != nil {
261+
return nil, fmt.Errorf("error during object IDs iteration: %w", err)
262+
}
263+
return objectIDs, nil
264+
}
265+
266+
// GetSDKClient returns a NeoFS SDK client configured with the specified address and context.
267+
// If timeout is 0, the default timeout will be used.
268+
func GetSDKClient(ctx context.Context, addr string, timeout time.Duration) (*client.Client, error) {
269+
var prmDial client.PrmDial
270+
if addr == "" {
271+
return nil, errors.New("address is empty")
272+
}
273+
prmDial.SetServerURI(addr)
274+
prmDial.SetContext(ctx)
275+
if timeout != 0 {
276+
prmDial.SetTimeout(timeout)
277+
prmDial.SetStreamTimeout(timeout)
278+
}
279+
c, err := client.New(client.PrmInit{})
280+
if err != nil {
281+
return nil, fmt.Errorf("can't create SDK client: %w", err)
282+
}
283+
284+
if err := c.Dial(prmDial); err != nil {
285+
if status.Code(err) == codes.Unimplemented {
286+
return c, nil
287+
}
288+
return nil, fmt.Errorf("can't init SDK client: %w", err)
289+
}
290+
291+
return c, nil
292+
}

0 commit comments

Comments
 (0)