From 8307273b66febd7c81969966194d692207a70477 Mon Sep 17 00:00:00 2001 From: warjiang <1096409085@qq.com> Date: Wed, 25 Oct 2023 00:43:06 +0800 Subject: [PATCH 1/4] feat: add NewAmazonS3BackendWithOptions enable S3ForcePathStyle as options --- amazon.go | 44 +++++++++++++++++++++++++++++++++++++++----- go.mod | 2 +- 2 files changed, 40 insertions(+), 6 deletions(-) diff --git a/amazon.go b/amazon.go index 8a2225b9..94e4fc80 100644 --- a/amazon.go +++ b/amazon.go @@ -18,17 +18,17 @@ package storage import ( "bytes" - "io/ioutil" - pathutil "path" - "strings" - "net/http" "crypto/tls" - "os" "github.com/aws/aws-sdk-go/aws" "github.com/aws/aws-sdk-go/aws/credentials" "github.com/aws/aws-sdk-go/aws/session" "github.com/aws/aws-sdk-go/service/s3" "github.com/aws/aws-sdk-go/service/s3/s3manager" + "io/ioutil" + "net/http" + "os" + pathutil "path" + "strings" ) // AmazonS3Backend is a storage backend for Amazon S3 @@ -68,6 +68,40 @@ func NewAmazonS3Backend(bucket string, prefix string, region string, endpoint st return b } +type AmazonS3Options struct { + S3ForcePathStyle *bool +} + +func NewAmazonS3BackendWithOptions(bucket string, prefix string, region string, endpoint string, sse string, options *AmazonS3Options) *AmazonS3Backend { + client := http.DefaultClient + if os.Getenv("AWS_INSECURE_SKIP_VERIFY") == "true" { + tr := &http.Transport{ + TLSClientConfig: &tls.Config{InsecureSkipVerify: true}, + } + client = &http.Client{Transport: tr} + } + s3ForcePathStyle := endpoint != "" + if options.S3ForcePathStyle != nil { + s3ForcePathStyle = *options.S3ForcePathStyle + } + service := s3.New(session.New(), &aws.Config{ + HTTPClient: client, + Region: aws.String(region), + Endpoint: aws.String(endpoint), + DisableSSL: aws.Bool(strings.HasPrefix(endpoint, "http://")), + S3ForcePathStyle: aws.Bool(s3ForcePathStyle), + }) + b := &AmazonS3Backend{ + Bucket: bucket, + Client: service, + Downloader: s3manager.NewDownloaderWithClient(service), + Prefix: cleanPrefix(prefix), + Uploader: s3manager.NewUploaderWithClient(service), + SSE: sse, + } + return b +} + // NewAmazonS3BackendWithCredentials creates a new instance of AmazonS3Backend with credentials func NewAmazonS3BackendWithCredentials(bucket string, prefix string, region string, endpoint string, sse string, credentials *credentials.Credentials) *AmazonS3Backend { client := http.DefaultClient diff --git a/go.mod b/go.mod index 3950ce6a..c2fde33c 100644 --- a/go.mod +++ b/go.mod @@ -1,4 +1,4 @@ -module github.com/chartmuseum/storage +module github.com/warjiang/storage go 1.17 From 62f6224b45b3828a44dd35a5a2e4948f88613a07 Mon Sep 17 00:00:00 2001 From: warjiang <1096409085@qq.com> Date: Wed, 25 Oct 2023 11:08:28 +0800 Subject: [PATCH 2/4] feat: revert automatic code format --- amazon.go | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/amazon.go b/amazon.go index 94e4fc80..901a6a08 100644 --- a/amazon.go +++ b/amazon.go @@ -18,17 +18,17 @@ package storage import ( "bytes" + "io/ioutil" + pathutil "path" + "strings" + "net/http" "crypto/tls" + "os" "github.com/aws/aws-sdk-go/aws" "github.com/aws/aws-sdk-go/aws/credentials" "github.com/aws/aws-sdk-go/aws/session" "github.com/aws/aws-sdk-go/service/s3" "github.com/aws/aws-sdk-go/service/s3/s3manager" - "io/ioutil" - "net/http" - "os" - pathutil "path" - "strings" ) // AmazonS3Backend is a storage backend for Amazon S3 From 218fad5c0e802fe25b01db9a5455cdb3e42f3279 Mon Sep 17 00:00:00 2001 From: warjiang <1096409085@qq.com> Date: Wed, 25 Oct 2023 11:09:57 +0800 Subject: [PATCH 3/4] feat: revert go pkg name --- go.mod | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/go.mod b/go.mod index c2fde33c..3950ce6a 100644 --- a/go.mod +++ b/go.mod @@ -1,4 +1,4 @@ -module github.com/warjiang/storage +module github.com/chartmuseum/storage go 1.17 From 4b4a57eba7b9e9cb7bb91d3dd2982c683c65edf3 Mon Sep 17 00:00:00 2001 From: warjiang <1096409085@qq.com> Date: Wed, 25 Oct 2023 11:26:07 +0800 Subject: [PATCH 4/4] feat: double check options to ensure options not nil --- amazon.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/amazon.go b/amazon.go index 901a6a08..731cc08d 100644 --- a/amazon.go +++ b/amazon.go @@ -81,7 +81,7 @@ func NewAmazonS3BackendWithOptions(bucket string, prefix string, region string, client = &http.Client{Transport: tr} } s3ForcePathStyle := endpoint != "" - if options.S3ForcePathStyle != nil { + if options != nil && options.S3ForcePathStyle != nil { s3ForcePathStyle = *options.S3ForcePathStyle } service := s3.New(session.New(), &aws.Config{