From ea7f7a33bfee99516651a69d7cfaf6c47ccfaa81 Mon Sep 17 00:00:00 2001 From: Ajat Prabha Date: Mon, 22 Feb 2021 17:49:20 +0530 Subject: [PATCH] add endpoint support to s3 storage (#62) --- config.example.yaml | 1 + pkg/config/types.go | 3 +++ pkg/service/dependencies.go | 1 + pkg/storage/aws/s3/options.go | 7 +++++++ pkg/storage/aws/s3/storage.go | 10 +++++++--- 5 files changed, 19 insertions(+), 3 deletions(-) diff --git a/config.example.yaml b/config.example.yaml index ca4ec85..15f4cf7 100644 --- a/config.example.yaml +++ b/config.example.yaml @@ -22,6 +22,7 @@ source: region: "region" accessKey: "randomAccessKey" secretKey: "superSecret" + endpoint: "custom-endpoint.com" pathPrefix: "/prefixPath/to/folder" port: 3000 diff --git a/pkg/config/types.go b/pkg/config/types.go index 59a2ad0..6721520 100644 --- a/pkg/config/types.go +++ b/pkg/config/types.go @@ -15,6 +15,8 @@ type S3Bucket struct { AccessKey string // Secret key that should be used to access the bucket SecretKey string + // Endpoint overrides the default generated endpoint for bucket client + Endpoint string } // GoogleCloudStorage contains the configuration values for GoogleCloudStorage source @@ -73,6 +75,7 @@ func (s *Source) readValue() { Region: v.GetString("source.bucket.region"), AccessKey: v.GetString("source.bucket.accessKey"), SecretKey: v.GetString("source.bucket.secretKey"), + Endpoint: v.GetString("source.bucket.endpoint"), } } else if regex.CloudfrontMatcher.MatchString(s.Kind) { s.Value = Cloudfront{ diff --git a/pkg/service/dependencies.go b/pkg/service/dependencies.go index 3770308..3a6b289 100644 --- a/pkg/service/dependencies.go +++ b/pkg/service/dependencies.go @@ -80,6 +80,7 @@ func NewS3Storage(b config.S3Bucket, hc base.HystrixCommand) *s3.Storage { s3.WithBucketRegion(b.Region), s3.WithAccessKey(b.AccessKey), s3.WithSecretKey(b.SecretKey), + s3.WithEndpoint(b.Endpoint), s3.WithHystrixCommand(hc), ) } diff --git a/pkg/storage/aws/s3/options.go b/pkg/storage/aws/s3/options.go index 850b49c..483f037 100644 --- a/pkg/storage/aws/s3/options.go +++ b/pkg/storage/aws/s3/options.go @@ -33,6 +33,13 @@ func WithSecretKey(secretKey string) Option { } } +// WithEndpoint sets the bucket endpoint +func WithEndpoint(endpoint string) Option { + return func(s *Storage) { + s.endpoint = endpoint + } +} + // WithHystrixCommand sets the bucket hystrixCmd func WithHystrixCommand(hytrixCmd storage.HystrixCommand) Option { return func(s *Storage) { diff --git a/pkg/storage/aws/s3/storage.go b/pkg/storage/aws/s3/storage.go index decfbd4..fd48258 100644 --- a/pkg/storage/aws/s3/storage.go +++ b/pkg/storage/aws/s3/storage.go @@ -22,6 +22,7 @@ type Storage struct { bucketRegion string accessKey string secretKey string + endpoint string service s3iface.S3API hystrixCmd storage.HystrixCommand downloader s3manageriface.DownloaderAPI @@ -118,9 +119,12 @@ func NewStorage(opts ...Option) *Storage { for _, opt := range opts { opt(&s) } - cfg := aws.NewConfig().WithRegion(s.bucketRegion).WithCredentials( - credentials.NewStaticCredentials(s.accessKey, s.secretKey, ""), - ) + cfg := aws.NewConfig(). + WithRegion(s.bucketRegion). + WithEndpoint(s.endpoint). + WithCredentials( + credentials.NewStaticCredentials(s.accessKey, s.secretKey, ""), + ) ssn, _ := session.NewSession(cfg) s.service = s3.New(ssn) s.downloader = s3manager.NewDownloaderWithClient(s.service)