Skip to content

Commit

Permalink
Merge pull request #1543 from ksw2000/refactor-s3-config
Browse files Browse the repository at this point in the history
♻️refactor(s3): WithEndpointResolver is deprecated
  • Loading branch information
ReneWerner87 authored Jan 10, 2025
2 parents d2af2eb + e4d46ce commit 40a6167
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 30 deletions.
18 changes: 3 additions & 15 deletions dynamodb/dynamodb.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,9 @@ func New(config Config) *Storage {
}

// Create db
sess := awsdynamodb.NewFromConfig(awscfg)
sess := awsdynamodb.NewFromConfig(awscfg, func(o *awsdynamodb.Options) {
o.BaseEndpoint = aws.String(cfg.Endpoint)
})

timeoutCtx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
Expand Down Expand Up @@ -232,23 +234,10 @@ func (s *Storage) requestContext() (context.Context, context.CancelFunc) {
}

func returnAWSConfig(cfg Config) (aws.Config, error) {
endpoint := aws.EndpointResolverWithOptionsFunc(func(service, region string, options ...interface{}) (aws.Endpoint, error) {
if cfg.Endpoint != "" {
return aws.Endpoint{
PartitionID: "aws",
URL: cfg.Endpoint,
SigningRegion: cfg.Region,
HostnameImmutable: true,
}, nil
}
return aws.Endpoint{}, &aws.EndpointNotFoundError{}
})

if cfg.Credentials != (Credentials{}) {
credentials := credentials.NewStaticCredentialsProvider(cfg.Credentials.AccessKey, cfg.Credentials.SecretAccessKey, "")
return awsconfig.LoadDefaultConfig(context.TODO(),
awsconfig.WithRegion(cfg.Region),
awsconfig.WithEndpointResolverWithOptions(endpoint),
awsconfig.WithCredentialsProvider(credentials),
awsconfig.WithRetryer(func() aws.Retryer {
return retry.AddWithMaxAttempts(retry.NewStandard(), cfg.MaxAttempts)
Expand All @@ -258,7 +247,6 @@ func returnAWSConfig(cfg Config) (aws.Config, error) {

return awsconfig.LoadDefaultConfig(context.TODO(),
awsconfig.WithRegion(cfg.Region),
awsconfig.WithEndpointResolverWithOptions(endpoint),
awsconfig.WithRetryer(func() aws.Retryer {
return retry.AddWithMaxAttempts(retry.NewStandard(), cfg.MaxAttempts)
}),
Expand Down
19 changes: 4 additions & 15 deletions s3/s3.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,10 @@ func New(config ...Config) *Storage {
panic(fmt.Sprintf("unable to load SDK config, %v", err))
}

sess := s3.NewFromConfig(awscfg)
sess := s3.NewFromConfig(awscfg, func(o *s3.Options) {
o.BaseEndpoint = aws.String(cfg.Endpoint)
})

storage := &Storage{
svc: sess,
downloader: manager.NewDownloader(sess),
Expand Down Expand Up @@ -173,23 +176,10 @@ func (s *Storage) requestContext() (context.Context, context.CancelFunc) {
}

func returnAWSConfig(cfg Config) (aws.Config, error) {
endpoint := aws.EndpointResolverWithOptionsFunc(func(service, region string, options ...interface{}) (aws.Endpoint, error) {
if cfg.Endpoint != "" {
return aws.Endpoint{
PartitionID: "aws",
URL: cfg.Endpoint,
SigningRegion: cfg.Region,
HostnameImmutable: true,
}, nil
}
return aws.Endpoint{}, &aws.EndpointNotFoundError{}
})

if cfg.Credentials != (Credentials{}) {
creds := credentials.NewStaticCredentialsProvider(cfg.Credentials.AccessKey, cfg.Credentials.SecretAccessKey, "")
return awsconfig.LoadDefaultConfig(context.TODO(),
awsconfig.WithRegion(cfg.Region),
awsconfig.WithEndpointResolverWithOptions(endpoint),
awsconfig.WithCredentialsProvider(creds),
awsconfig.WithRetryer(func() aws.Retryer {
return retry.AddWithMaxAttempts(retry.NewStandard(), cfg.MaxAttempts)
Expand All @@ -199,7 +189,6 @@ func returnAWSConfig(cfg Config) (aws.Config, error) {

return awsconfig.LoadDefaultConfig(context.TODO(),
awsconfig.WithRegion(cfg.Region),
awsconfig.WithEndpointResolverWithOptions(endpoint),
awsconfig.WithRetryer(func() aws.Retryer {
return retry.AddWithMaxAttempts(retry.NewStandard(), cfg.MaxAttempts)
}),
Expand Down

0 comments on commit 40a6167

Please sign in to comment.