From e4c00d7c7813c5f86a855d14bed8a85abb368215 Mon Sep 17 00:00:00 2001 From: Evgenii Baidakov Date: Mon, 10 Jul 2023 09:52:56 +0400 Subject: [PATCH] slicer: Add SetSession to slicer.ObjectWriter interface Signed-off-by: Evgenii Baidakov --- client/object_put.go | 9 +++++++++ object/slicer/slicer.go | 10 +++++++++- object/slicer/slicer_test.go | 9 +++++++++ 3 files changed, 27 insertions(+), 1 deletion(-) diff --git a/client/object_put.go b/client/object_put.go index c713176c..6e41b751 100644 --- a/client/object_put.go +++ b/client/object_put.go @@ -16,6 +16,7 @@ import ( "github.com/nspcc-dev/neofs-sdk-go/object" oid "github.com/nspcc-dev/neofs-sdk-go/object/id" "github.com/nspcc-dev/neofs-sdk-go/object/slicer" + "github.com/nspcc-dev/neofs-sdk-go/session" "github.com/nspcc-dev/neofs-sdk-go/stat" "github.com/nspcc-dev/neofs-sdk-go/user" ) @@ -303,10 +304,18 @@ func (c *Client) ObjectPutInit(ctx context.Context, hdr object.Object, signer ne type objectWriter struct { context context.Context client *Client + sess *session.Object +} + +func (x *objectWriter) SetSession(sess session.Object) { + x.sess = &sess } func (x *objectWriter) InitDataStream(header object.Object, signer neofscrypto.Signer) (io.Writer, error) { var prm PrmObjectPutInit + if x.sess != nil { + prm.WithinSession(*x.sess) + } stream, err := x.client.ObjectPutInit(x.context, header, signer, prm) if err != nil { diff --git a/object/slicer/slicer.go b/object/slicer/slicer.go index 5394ef43..2ea14ef2 100644 --- a/object/slicer/slicer.go +++ b/object/slicer/slicer.go @@ -35,6 +35,10 @@ type ObjectWriter interface { // Signer is required and must not be nil. The operation is executed on behalf of // the account corresponding to the specified Signer, which is taken into account, in particular, for access control. InitDataStream(header object.Object, signer neofscrypto.Signer) (dataStream io.Writer, err error) + + // SetSession pass session object to writer. It will be used for writing object routine. + // This method must be called before InitDataStream. + SetSession(sess session.Object) } // Slicer converts input raw data streams into NeoFS objects. Working Slicer @@ -64,10 +68,14 @@ func New(w ObjectWriter) *Slicer { // all resulting objects. In this case, the object is considered to be created // by a proxy on behalf of the session issuer. func NewSession(w ObjectWriter, token session.Object) *Slicer { - return &Slicer{ + slicer := &Slicer{ w: w, sessionToken: &token, } + + slicer.w.SetSession(token) + + return slicer } const defaultPayloadSizeLimit = 1 << 20 diff --git a/object/slicer/slicer_test.go b/object/slicer/slicer_test.go index a4e342f6..a39e5162 100644 --- a/object/slicer/slicer_test.go +++ b/object/slicer/slicer_test.go @@ -131,6 +131,9 @@ func benchmarkSliceDataIntoObjects(b *testing.B, size, sizeLimit uint64) { type discardObject struct{} +func (discardObject) SetSession(session.Object) { +} + func (discardObject) InitDataStream(object.Object, neofscrypto.Signer) (io.Writer, error) { return discardPayload{}, nil } @@ -269,6 +272,9 @@ type slicedObjectChecker struct { chainCollector *chainCollector } +func (*slicedObjectChecker) SetSession(session.Object) { +} + func (x *slicedObjectChecker) InitDataStream(hdr object.Object, _ neofscrypto.Signer) (io.Writer, error) { checkStaticMetadata(x.tb, hdr, x.input) @@ -528,6 +534,9 @@ type memoryWriter struct { splitID *object.SplitID } +func (*memoryWriter) SetSession(session.Object) { +} + func (w *memoryWriter) InitDataStream(hdr object.Object, _ neofscrypto.Signer) (io.Writer, error) { w.headers = append(w.headers, hdr) if w.splitID == nil && hdr.SplitID() != nil {