From 324720b5f08e4f697d1aca20f8633b248789ffdc Mon Sep 17 00:00:00 2001 From: Nipe Setiawan Harefa Date: Wed, 6 Oct 2021 13:36:47 +0700 Subject: [PATCH] feat(s3): add custome endpoint --- action.yml | 4 ++++ go.mod | 1 + go.sum | 7 +++++++ s3.go | 47 ++++++++++++++++++++++++++++++++++++++++++----- 4 files changed, 54 insertions(+), 5 deletions(-) diff --git a/action.yml b/action.yml index b567558..c094c51 100644 --- a/action.yml +++ b/action.yml @@ -29,6 +29,9 @@ inputs: description: "Specifies the desired Storage Class for the object." required: false default: STANDARD + endpoint: + description: "Custome S3 Endpoint" + required: false runs: using: "composite" steps: @@ -44,3 +47,4 @@ runs: KEY: ${{ inputs.key }} ARTIFACTS: ${{ inputs.artifacts }} OS: ${{ runner.os }} + AWS_ENDPOINT: ${{ inputs.endpoint }} diff --git a/go.mod b/go.mod index 9dfd437..5a70b01 100644 --- a/go.mod +++ b/go.mod @@ -5,6 +5,7 @@ go 1.16 require ( github.com/aws/aws-sdk-go-v2 v1.9.1 github.com/aws/aws-sdk-go-v2/config v1.8.2 + github.com/aws/aws-sdk-go-v2/feature/s3/manager v1.5.3 github.com/aws/aws-sdk-go-v2/service/s3 v1.16.0 github.com/pkg/errors v0.9.1 ) diff --git a/go.sum b/go.sum index bdddf9b..71b5d89 100644 --- a/go.sum +++ b/go.sum @@ -6,6 +6,8 @@ github.com/aws/aws-sdk-go-v2/credentials v1.4.2 h1:8kVE4Og6wlhVrMGiORQ3p9gRj2exj github.com/aws/aws-sdk-go-v2/credentials v1.4.2/go.mod h1:9Sp6u121/f0NnvHyhG7dgoYeUTEFC2vsvJqJ6wXpkaI= github.com/aws/aws-sdk-go-v2/feature/ec2/imds v1.5.1 h1:Nm+BxqBtT0r+AnD6byGMCGT4Km0QwHBy8mAYptNPXY4= github.com/aws/aws-sdk-go-v2/feature/ec2/imds v1.5.1/go.mod h1:W1ldHfsgeGlKpJ4xZMKZUI6Wmp6EAstU7PxnhbXWWrI= +github.com/aws/aws-sdk-go-v2/feature/s3/manager v1.5.3 h1:0O72494cCsazjpsGfo+LXezru6PMSp0HUB1m5UfpaRU= +github.com/aws/aws-sdk-go-v2/feature/s3/manager v1.5.3/go.mod h1:claNkz2j/N/AZceFcAbR0NyuWnrn+jCYpI+6Ozjsc0k= github.com/aws/aws-sdk-go-v2/internal/ini v1.2.3 h1:NnXJXUz7oihrSlPKEM0yZ19b+7GQ47MX/LluLlEyE/Y= github.com/aws/aws-sdk-go-v2/internal/ini v1.2.3/go.mod h1:EES9ToeC3h063zCFDdqWGnARExNdULPaBvARm1FLwxA= github.com/aws/aws-sdk-go-v2/service/internal/accept-encoding v1.3.0 h1:gceOysEWNNwLd6cki65IMBZ4WAM0MwgBQq2n7kejoT8= @@ -22,17 +24,22 @@ github.com/aws/aws-sdk-go-v2/service/sts v1.7.1 h1:7ce9ugapSgBapwLhg7AJTqKW5U92V github.com/aws/aws-sdk-go-v2/service/sts v1.7.1/go.mod h1:r1i8QwKPzwByXqZb3POQfBs7jozrdnHz8PVbsvyx73w= github.com/aws/smithy-go v1.8.0 h1:AEwwwXQZtUwP5Mz506FeXXrKBe0jA8gVM+1gEcSRooc= github.com/aws/smithy-go v1.8.0/go.mod h1:SObp3lf9smib00L/v3U2eAKG8FyQ7iLrJnQiAmR5n+E= +github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/google/go-cmp v0.5.4/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= github.com/google/go-cmp v0.5.6 h1:BKbKCqvP6I+rmFHt06ZmyQtvB8xAkWdhFyr0ZUNZcxQ= github.com/google/go-cmp v0.5.6/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= +github.com/jmespath/go-jmespath v0.4.0 h1:BEgLn5cpjn8UN1mAw4NjwDrS35OdebyEtFe+9YPoQUg= github.com/jmespath/go-jmespath v0.4.0/go.mod h1:T8mJZnbsbmF+m6zOOFylbeCJqk5+pHWvzYPziyZiYoo= +github.com/jmespath/go-jmespath/internal/testify v1.5.1 h1:shLQSRRSCCPj3f2gpwzGwWFoC7ycTf1rcQZHOlsJ6N8= github.com/jmespath/go-jmespath/internal/testify v1.5.1/go.mod h1:L3OGu8Wl2/fWfCI6z80xFu9LTZmf1ZRjMHUOPmWr69U= github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4= github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= +github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4= golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/yaml.v2 v2.2.8 h1:obN1ZagJSUGI0Ek/LBmuj4SNLPfIny3KsKFopxRdj10= gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= diff --git a/s3.go b/s3.go index 9946985..5dc6703 100644 --- a/s3.go +++ b/s3.go @@ -7,14 +7,35 @@ import ( "github.com/aws/aws-sdk-go-v2/aws" "github.com/aws/aws-sdk-go-v2/config" + "github.com/aws/aws-sdk-go-v2/feature/s3/manager" "github.com/aws/aws-sdk-go-v2/service/s3" "github.com/aws/aws-sdk-go-v2/service/s3/types" "github.com/pkg/errors" ) +func loadCustomEndpoint() aws.EndpointResolverFunc { + endpoint := os.Getenv("AWS_ENDPOINT") + awsRegion := os.Getenv("AWS_REGION") + + return aws.EndpointResolverFunc(func(service, region string) (aws.Endpoint, error) { + if endpoint != "" { + return aws.Endpoint{ + PartitionID: "aws", + URL: endpoint, + SigningRegion: awsRegion, + }, nil + } + + return aws.Endpoint{}, nil + }) +} + // PutObject - Upload object to s3 bucket func PutObject(key, bucket, s3Class string) error { - cfg, err := config.LoadDefaultConfig(context.TODO()) + cfg, err := config.LoadDefaultConfig(context.TODO(), config.WithEndpointResolver(loadCustomEndpoint())) + if err != nil { + return err + } session := s3.NewFromConfig(cfg) file, err := os.Open(key) @@ -40,15 +61,25 @@ func PutObject(key, bucket, s3Class string) error { // GetObject - Get object from s3 bucket func GetObject(key, bucket string) error { - cfg, err := config.LoadDefaultConfig(context.TODO()) + cfg, err := config.LoadDefaultConfig(context.TODO(), config.WithEndpointResolver(loadCustomEndpoint())) + if err != nil { + return err + } session := s3.NewFromConfig(cfg) + downloader := manager.NewDownloader(session) + + file, err := os.Create(key) + if err != nil { + return err + } + i := &s3.GetObjectInput{ Bucket: aws.String(bucket), Key: aws.String(key), } - size, err := session.GetObject(context.TODO(), i) + size, err := downloader.Download(context.TODO(), file, i) log.Printf("Cache downloaded successfully, containing %d bytes", size) @@ -57,7 +88,10 @@ func GetObject(key, bucket string) error { // DeleteObject - Delete object from s3 bucket func DeleteObject(key, bucket string) error { - cfg, err := config.LoadDefaultConfig(context.TODO()) + cfg, err := config.LoadDefaultConfig(context.TODO(), config.WithEndpointResolver(loadCustomEndpoint())) + if err != nil { + return err + } session := s3.NewFromConfig(cfg) i := &s3.DeleteObjectInput{ @@ -75,7 +109,10 @@ func DeleteObject(key, bucket string) error { // ObjectExists - Verify if object exists in s3 func ObjectExists(key, bucket string) (bool, error) { - cfg, err := config.LoadDefaultConfig(context.TODO()) + cfg, err := config.LoadDefaultConfig(context.TODO(), config.WithEndpointResolver(loadCustomEndpoint())) + if err != nil { + return false, err + } session := s3.NewFromConfig(cfg) i := &s3.HeadObjectInput{