From c1890f0c331e1d805179c65acb298ec0d0b06271 Mon Sep 17 00:00:00 2001 From: brunopadz Date: Mon, 4 Oct 2021 15:51:37 -0300 Subject: [PATCH 1/3] chore: bump aws sdk to v2 Signed-off-by: brunopadz --- go.mod | 7 +++++- go.sum | 50 ++++++++++++++++++++++++++----------------- s3.go | 65 ++++++++++++++++++++++++++++++-------------------------- types.go | 4 +++- 4 files changed, 74 insertions(+), 52 deletions(-) diff --git a/go.mod b/go.mod index dd2d6e8..9dfd437 100644 --- a/go.mod +++ b/go.mod @@ -2,4 +2,9 @@ module action-s3-cache go 1.16 -require github.com/aws/aws-sdk-go v1.35.35 +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/service/s3 v1.16.0 + github.com/pkg/errors v0.9.1 +) diff --git a/go.sum b/go.sum index f111e21..bdddf9b 100644 --- a/go.sum +++ b/go.sum @@ -1,28 +1,38 @@ -github.com/aws/aws-sdk-go v1.35.35 h1:o/EbgEcIPWga7GWhJhb3tiaxqk4/goTdo5YEMdnVxgE= -github.com/aws/aws-sdk-go v1.35.35/go.mod h1:hcU610XS61/+aQV88ixoOzUoG7v3b31pl2zKMmprdro= -github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8= +github.com/aws/aws-sdk-go-v2 v1.9.1 h1:ZbovGV/qo40nrOJ4q8G33AGICzaPI45FHQWJ9650pF4= +github.com/aws/aws-sdk-go-v2 v1.9.1/go.mod h1:cK/D0BBs0b/oWPIcX/Z/obahJK1TT7IPVjy53i/mX/4= +github.com/aws/aws-sdk-go-v2/config v1.8.2 h1:Dqy4ySXFmulRmZhfynm/5CD4Y6aXiTVhDtXLIuUe/r0= +github.com/aws/aws-sdk-go-v2/config v1.8.2/go.mod h1:r0bkX9NyuCuf28qVcsEMtpAQibT7gA1Q0gzkjvgJdLU= +github.com/aws/aws-sdk-go-v2/credentials v1.4.2 h1:8kVE4Og6wlhVrMGiORQ3p9gRj2exjzhFRB+QzWBUa5Q= +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/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= +github.com/aws/aws-sdk-go-v2/service/internal/accept-encoding v1.3.0/go.mod h1:v8ygadNyATSm6elwJ/4gzJwcFhri9RqS8skgHKiwXPU= +github.com/aws/aws-sdk-go-v2/service/internal/presigned-url v1.3.1 h1:APEjhKZLFlNVLATnA/TJyA+w1r/xd5r5ACWBDZ9aIvc= +github.com/aws/aws-sdk-go-v2/service/internal/presigned-url v1.3.1/go.mod h1:Ve+eJOx9UWaT/lMVebnFhDhO49fSLVedHoA82+Rqme0= +github.com/aws/aws-sdk-go-v2/service/internal/s3shared v1.7.1 h1:YEz2KMyqK2zyG3uOa0l2xBc/H6NUVJir8FhwHQHF3rc= +github.com/aws/aws-sdk-go-v2/service/internal/s3shared v1.7.1/go.mod h1:yg4EN/BKoc7+DLhNOxxdvoO3+iyW2FuynvaKqLcLDUM= +github.com/aws/aws-sdk-go-v2/service/s3 v1.16.0 h1:dt1JQFj/135ozwGIWeCM3aQ8N/kB3Xu3Uu4r9zuOIyc= +github.com/aws/aws-sdk-go-v2/service/s3 v1.16.0/go.mod h1:Tk23mCmfL3wb3tNIeMk/0diUZ0W4R6uZtjYKguMLW2s= +github.com/aws/aws-sdk-go-v2/service/sso v1.4.1 h1:RfgQyv3bFT2Js6XokcrNtTjQ6wAVBRpoCgTFsypihHA= +github.com/aws/aws-sdk-go-v2/service/sso v1.4.1/go.mod h1:ycPdbJZlM0BLhuBnd80WX9PucWPG88qps/2jl9HugXs= +github.com/aws/aws-sdk-go-v2/service/sts v1.7.1 h1:7ce9ugapSgBapwLhg7AJTqKW5U92VRX3vX65k2tsB+g= +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/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= -github.com/jmespath/go-jmespath v0.4.0 h1:BEgLn5cpjn8UN1mAw4NjwDrS35OdebyEtFe+9YPoQUg= +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/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/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= -golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= -golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= -golang.org/x/net v0.0.0-20201110031124-69a78807bb2b h1:uwuIcX0g4Yl1NC5XAz37xsr2lTtcqevgzYNVt49waME= -golang.org/x/net v0.0.0-20201110031124-69a78807bb2b/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU= -golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= -golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= -golang.org/x/text v0.3.3 h1:cokOdA+Jmi5PJGXLlLllQSgYigAEfHXJAERHVMaCc2k= -golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= -golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= -gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= +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 8d5158a..b5def09 100644 --- a/s3.go +++ b/s3.go @@ -1,20 +1,21 @@ package main import ( + "context" "log" "os" - "github.com/aws/aws-sdk-go/aws" - "github.com/aws/aws-sdk-go/aws/awserr" - "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" + "github.com/aws/aws-sdk-go-v2/aws" + "github.com/aws/aws-sdk-go-v2/config" + "github.com/aws/aws-sdk-go-v2/service/s3" + "github.com/aws/aws-sdk-go-v2/service/s3/types" + "github.com/pkg/errors" ) // PutObject - Upload object to s3 bucket func PutObject(key, bucket, s3Class string) error { - session := session.Must(session.NewSession()) - uploader := s3manager.NewUploader(session) + cfg, err := config.LoadDefaultConfig(context.TODO()) + session := s3.NewFromConfig(cfg) file, err := os.Open(key) if err != nil { @@ -22,12 +23,14 @@ func PutObject(key, bucket, s3Class string) error { } defer file.Close() - _, err = uploader.Upload(&s3manager.UploadInput{ + i := &s3.PutObjectInput{ Bucket: aws.String(bucket), Key: aws.String(key), Body: file, - StorageClass: aws.String(s3Class), - }) + StorageClass: types.StorageClass(s3Class), + } + + _, err = session.PutObject(context.TODO(), i) if err == nil { log.Print("Cache saved successfully") } @@ -37,18 +40,17 @@ func PutObject(key, bucket, s3Class string) error { // GetObject - Get object from s3 bucket func GetObject(key, bucket string) error { - session := session.Must(session.NewSession()) - downloader := s3manager.NewDownloader(session) + cfg, err := config.LoadDefaultConfig(context.TODO()) + session := s3.NewFromConfig(cfg) + //session := session.Must(session.NewSession()) + //downloader := s3manager.NewDownloader(session) - file, err := os.Create(key) - if err != nil { - return err - } - - size, err := downloader.Download(file, &s3.GetObjectInput{ + i := &s3.GetObjectInput{ Bucket: aws.String(bucket), Key: aws.String(key), - }) + } + + size, err := session.GetObject(context.TODO(), i) log.Printf("Cache downloaded successfully, containing %d bytes", size) @@ -57,13 +59,15 @@ func GetObject(key, bucket string) error { // DeleteObject - Delete object from s3 bucket func DeleteObject(key, bucket string) error { - session := session.Must(session.NewSession()) - service := s3.New(session) + cfg, err := config.LoadDefaultConfig(context.TODO()) + session := s3.NewFromConfig(cfg) - _, err := service.DeleteObject(&s3.DeleteObjectInput{ + i := &s3.DeleteObjectInput{ Bucket: aws.String(bucket), Key: aws.String(key), - }) + } + + _, err = session.DeleteObject(context.TODO(), i) if err == nil { log.Print("Cache purged successfully") } @@ -73,18 +77,19 @@ func DeleteObject(key, bucket string) error { // ObjectExists - Verify if object exists in s3 func ObjectExists(key, bucket string) (bool, error) { - session := session.Must(session.NewSession()) - service := s3.New(session) + cfg, err := config.LoadDefaultConfig(context.TODO()) + session := s3.NewFromConfig(cfg) - if _, err := service.HeadObject(&s3.HeadObjectInput{ + i := &s3.HeadObjectInput{ Bucket: aws.String(bucket), Key: aws.String(key), - }); err != nil { - if aerr := err.(awserr.Error); aerr.Code() == ErrCodeNotFound { + } + + if _, err = session.HeadObject(context.TODO(), i); err != nil { + var nsk *types.NoSuchKey + if errors.As(err, &nsk) { return false, nil } - - return false, err } return true, nil diff --git a/types.go b/types.go index 060fda7..f01c908 100644 --- a/types.go +++ b/types.go @@ -1,5 +1,7 @@ package main +import "github.com/aws/aws-sdk-go-v2/service/s3/types" + const ( // PutAction - Put artifacts PutAction = "put" @@ -19,7 +21,7 @@ type ( Action struct { Action string Bucket string - S3Class string + S3Class types.StorageClass Key string Artifacts []string } From 63ebc1338b72b1ddfbc011cf96c85c77187952bf Mon Sep 17 00:00:00 2001 From: brunopadz Date: Tue, 5 Oct 2021 08:39:08 -0300 Subject: [PATCH 2/3] fix: rollback s3 class type to string Signed-off-by: brunopadz --- types.go | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/types.go b/types.go index f01c908..060fda7 100644 --- a/types.go +++ b/types.go @@ -1,7 +1,5 @@ package main -import "github.com/aws/aws-sdk-go-v2/service/s3/types" - const ( // PutAction - Put artifacts PutAction = "put" @@ -21,7 +19,7 @@ type ( Action struct { Action string Bucket string - S3Class types.StorageClass + S3Class string Key string Artifacts []string } From 1ab3c3c35d59a38f4314ea1dba33e8fc58f83302 Mon Sep 17 00:00:00 2001 From: brunopadz Date: Tue, 5 Oct 2021 08:54:02 -0300 Subject: [PATCH 3/3] refactor: remove unecessary comments Signed-off-by: brunopadz --- s3.go | 2 -- 1 file changed, 2 deletions(-) diff --git a/s3.go b/s3.go index b5def09..9946985 100644 --- a/s3.go +++ b/s3.go @@ -42,8 +42,6 @@ func PutObject(key, bucket, s3Class string) error { func GetObject(key, bucket string) error { cfg, err := config.LoadDefaultConfig(context.TODO()) session := s3.NewFromConfig(cfg) - //session := session.Must(session.NewSession()) - //downloader := s3manager.NewDownloader(session) i := &s3.GetObjectInput{ Bucket: aws.String(bucket),