From f47552bf60274d05c7484dd1078eedddff5b6908 Mon Sep 17 00:00:00 2001 From: Derrick Hammer Date: Sat, 24 Feb 2024 10:34:49 -0500 Subject: [PATCH] refactor: move s3 client creation to a factory method on storage for re-usability --- protocols/s5/tus.go | 24 +----------------------- storage/storage.go | 34 ++++++++++++++++++++++++++++++++++ 2 files changed, 35 insertions(+), 23 deletions(-) diff --git a/protocols/s5/tus.go b/protocols/s5/tus.go index 1241aac..a705bd5 100644 --- a/protocols/s5/tus.go +++ b/protocols/s5/tus.go @@ -20,8 +20,6 @@ import ( "git.lumeweb.com/LumeWeb/portal/metadata" - awsConfig "github.com/aws/aws-sdk-go-v2/config" - "github.com/aws/aws-sdk-go-v2/credentials" "github.com/tus/tusd/v2/pkg/s3store" tusd "github.com/tus/tusd/v2/pkg/handler" @@ -125,31 +123,11 @@ func (t *TusHandler) Init() error { return blankResp, blankChanges, nil } - customResolver := aws.EndpointResolverWithOptionsFunc(func(service, region string, options ...interface{}) (aws.Endpoint, error) { - if service == s3.ServiceID { - return aws.Endpoint{ - URL: t.config.Config().Core.Storage.S3.Endpoint, - SigningRegion: t.config.Config().Core.Storage.S3.Region, - }, nil - } - return aws.Endpoint{}, &aws.EndpointNotFoundError{} - }) - - cfg, err := awsConfig.LoadDefaultConfig(context.TODO(), - awsConfig.WithRegion("us-east-1"), - awsConfig.WithCredentialsProvider(credentials.NewStaticCredentialsProvider( - t.config.Config().Core.Storage.S3.AccessKey, - t.config.Config().Core.Storage.S3.SecretKey, - "", - )), - awsConfig.WithEndpointResolverWithOptions(customResolver), - ) + s3Client, err := t.storage.S3Client(context.Background()) if err != nil { return err } - s3Client := s3.NewFromConfig(cfg) - store := s3store.New(t.config.Config().Core.Storage.S3.BufferBucket, s3Client) locker := NewMySQLLocker(t.db, t.logger) diff --git a/storage/storage.go b/storage/storage.go index c36c89e..f6728b8 100644 --- a/storage/storage.go +++ b/storage/storage.go @@ -7,6 +7,12 @@ import ( "io" "net/http" + "github.com/aws/aws-sdk-go-v2/aws" + + awsConfig "github.com/aws/aws-sdk-go-v2/config" + "github.com/aws/aws-sdk-go-v2/credentials" + "github.com/aws/aws-sdk-go-v2/service/s3" + "git.lumeweb.com/LumeWeb/portal/config" "go.uber.org/fx" @@ -52,6 +58,7 @@ type StorageService interface { DownloadObjectProof(ctx context.Context, protocol StorageProtocol, objectHash []byte) (io.ReadCloser, error) DeleteObject(ctx context.Context, protocol StorageProtocol, objectHash []byte) error DeleteObjectProof(ctx context.Context, protocol StorageProtocol, objectHash []byte) error + S3Client(ctx context.Context) (*s3.Client, error) } type StorageServiceDefault struct { @@ -288,6 +295,33 @@ func (s StorageServiceDefault) DeleteObjectProof(ctx context.Context, protocol S return nil } +func (s StorageServiceDefault) S3Client(ctx context.Context) (*s3.Client, error) { + customResolver := aws.EndpointResolverWithOptionsFunc(func(service, region string, options ...interface{}) (aws.Endpoint, error) { + if service == s3.ServiceID { + return aws.Endpoint{ + URL: s.config.Config().Core.Storage.S3.Endpoint, + SigningRegion: s.config.Config().Core.Storage.S3.Region, + }, nil + } + return aws.Endpoint{}, &aws.EndpointNotFoundError{} + }) + cfg, err := awsConfig.LoadDefaultConfig(ctx, + awsConfig.WithRegion("us-east-1"), + awsConfig.WithCredentialsProvider(credentials.NewStaticCredentialsProvider( + s.config.Config().Core.Storage.S3.AccessKey, + s.config.Config().Core.Storage.S3.SecretKey, + "", + )), + awsConfig.WithEndpointResolverWithOptions(customResolver), + ) + if err != nil { + return nil, err + } + + return s3.NewFromConfig(cfg), nil + +} + func (s StorageServiceDefault) getProofPath(protocol StorageProtocol, objectHash []byte) string { return fmt.Sprintf("%s%s", protocol.EncodeFileName(objectHash), PROOF_EXTENSION) }