refactor: move s3 client creation to a factory method on storage for re-usability

This commit is contained in:
Derrick Hammer 2024-02-24 10:34:49 -05:00
parent 988a313f93
commit f47552bf60
Signed by: pcfreak30
GPG Key ID: C997C339BE476FF2
2 changed files with 35 additions and 23 deletions

View File

@ -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)

View File

@ -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)
}