From f77c4e49dc7a0037e28904c7c31eac21db4df628 Mon Sep 17 00:00:00 2001 From: Pavel Korotkov Date: Tue, 16 Feb 2021 18:38:30 +0300 Subject: [PATCH] [#18] Add fetching owner variously on file uploading Signed-off-by: Pavel Korotkov --- upload.go | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/upload.go b/upload.go index 3124e26..1360ec5 100644 --- a/upload.go +++ b/upload.go @@ -1,6 +1,7 @@ package main import ( + "context" "encoding/json" "io" "strconv" @@ -9,6 +10,7 @@ import ( sdk "github.com/nspcc-dev/cdn-sdk" "github.com/nspcc-dev/neofs-api-go/pkg/container" "github.com/nspcc-dev/neofs-api-go/pkg/object" + "github.com/nspcc-dev/neofs-api-go/pkg/owner" "github.com/valyala/fasthttp" "go.uber.org/zap" ) @@ -33,6 +35,14 @@ func (pr *putResponse) Encode(w io.Writer) error { return enc.Encode(pr) } +func (a *app) fetchOwner(ctx context.Context) *owner.ID { + if tkn, err := sdk.BearerToken(ctx); err == nil && tkn != nil { + return tkn.Issuer() + } + + return a.cli.Owner() +} + func (a *app) upload(c *fasthttp.RequestCtx) { var ( err error @@ -109,7 +119,7 @@ func (a *app) upload(c *fasthttp.RequestCtx) { // prepares new object and fill it raw := object.NewRaw() raw.SetContainerID(cid) - raw.SetOwnerID(a.cli.Owner()) // should be various: from sdk / BearerToken + raw.SetOwnerID(a.fetchOwner(c)) raw.SetAttributes(attributes...) // tries to put file into NeoFS or throw error