Migrate AWS SDK from v1 to v2 to resolve deprecation warnings (#2202)
* Migrate AWS SDK from v1 to v2 to resolve deprecation warnings Co-authored-by: yxxhero <11087727+yxxhero@users.noreply.github.com> * Fix error message style issue for staticcheck Co-authored-by: yxxhero <11087727+yxxhero@users.noreply.github.com> --------- Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: yxxhero <11087727+yxxhero@users.noreply.github.com>
This commit is contained in:
parent
c31ecf5061
commit
d94a7ada2b
7
go.mod
7
go.mod
|
|
@ -6,6 +6,8 @@ require (
|
|||
dario.cat/mergo v1.0.2
|
||||
github.com/Masterminds/semver/v3 v3.4.0
|
||||
github.com/Masterminds/sprig/v3 v3.3.0
|
||||
github.com/aws/aws-sdk-go-v2/config v1.29.15
|
||||
github.com/aws/aws-sdk-go-v2/service/s3 v1.80.1
|
||||
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc
|
||||
github.com/go-test/deep v1.1.1
|
||||
github.com/golang/mock v1.6.0
|
||||
|
|
@ -46,7 +48,7 @@ require (
|
|||
github.com/Azure/go-autorest/tracing v0.6.0 // indirect
|
||||
github.com/Masterminds/goutils v1.1.1 // indirect
|
||||
github.com/a8m/envsubst v1.4.3 // indirect
|
||||
github.com/aws/aws-sdk-go v1.55.7
|
||||
github.com/aws/aws-sdk-go v1.55.7 // indirect
|
||||
github.com/bgentry/go-netrc v0.0.0-20140422174119-9fd32a8b3d3d // indirect
|
||||
github.com/blang/semver v3.5.1+incompatible // indirect
|
||||
github.com/dimchansky/utfbom v1.1.1 // indirect
|
||||
|
|
@ -73,7 +75,6 @@ require (
|
|||
github.com/huandu/xstrings v1.5.0 // indirect
|
||||
github.com/inconshreveable/mousetrap v1.1.0 // indirect
|
||||
github.com/itchyny/gojq v0.12.16 // indirect
|
||||
github.com/jmespath/go-jmespath v0.4.0 // indirect
|
||||
github.com/klauspost/compress v1.18.0 // indirect
|
||||
github.com/lib/pq v1.10.9 // indirect
|
||||
github.com/mattn/go-colorable v0.1.14 // indirect
|
||||
|
|
@ -146,7 +147,6 @@ require (
|
|||
github.com/atotto/clipboard v0.1.4 // indirect
|
||||
github.com/aws/aws-sdk-go-v2 v1.38.3 // indirect
|
||||
github.com/aws/aws-sdk-go-v2/aws/protocol/eventstream v1.6.10 // indirect
|
||||
github.com/aws/aws-sdk-go-v2/config v1.29.15 // indirect
|
||||
github.com/aws/aws-sdk-go-v2/credentials v1.17.68 // indirect
|
||||
github.com/aws/aws-sdk-go-v2/feature/ec2/imds v1.16.30 // indirect
|
||||
github.com/aws/aws-sdk-go-v2/feature/s3/manager v1.17.72 // indirect
|
||||
|
|
@ -159,7 +159,6 @@ require (
|
|||
github.com/aws/aws-sdk-go-v2/service/internal/presigned-url v1.12.15 // indirect
|
||||
github.com/aws/aws-sdk-go-v2/service/internal/s3shared v1.18.15 // indirect
|
||||
github.com/aws/aws-sdk-go-v2/service/kms v1.38.3 // indirect
|
||||
github.com/aws/aws-sdk-go-v2/service/s3 v1.80.1 // indirect
|
||||
github.com/aws/aws-sdk-go-v2/service/secretsmanager v1.39.2 // indirect
|
||||
github.com/aws/aws-sdk-go-v2/service/ssm v1.64.2 // indirect
|
||||
github.com/aws/aws-sdk-go-v2/service/sso v1.25.3 // indirect
|
||||
|
|
|
|||
3
go.sum
3
go.sum
|
|
@ -461,8 +461,6 @@ github.com/itchyny/timefmt-go v0.1.6 h1:ia3s54iciXDdzWzwaVKXZPbiXzxxnv1SPGFfM/my
|
|||
github.com/itchyny/timefmt-go v0.1.6/go.mod h1:RRDZYC5s9ErkjQvTvvU7keJjxUYzIISJGxm9/mAERQg=
|
||||
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/jmoiron/sqlx v1.4.0 h1:1PLqN7S1UYp5t4SrVVnt4nUVNemrDAtxlulVe+Qgm3o=
|
||||
github.com/jmoiron/sqlx v1.4.0/go.mod h1:ZrZ7UsYB/weZdl2Bxg6jCRO9c3YHl8r3ahlKmRT4JLY=
|
||||
github.com/josharian/intern v1.0.0 h1:vlS4z54oSdjm0bgjRigI+G1HpF+tI+9rE5LLzOg8HmY=
|
||||
|
|
@ -863,7 +861,6 @@ gopkg.in/inf.v0 v0.9.1 h1:73M5CoZyi3ZLMOyDlQh031Cx6N9NDJ2Vvfl76EDAgDc=
|
|||
gopkg.in/inf.v0 v0.9.1/go.mod h1:cWUDdTG/fYaXco+Dcufb5Vnc6Gp2YChqWtbxRZE0mXw=
|
||||
gopkg.in/ini.v1 v1.67.0 h1:Dgnx+6+nfE+IfzjUEISNeydPJh9AXNNsWbGP9KzCsOA=
|
||||
gopkg.in/ini.v1 v1.67.0/go.mod h1:pNLf8WUiyNEtQjuu5G5vTm06TEv9tsIgeAvK8hOrP4k=
|
||||
gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
|
||||
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
|
||||
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
|
||||
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||
|
|
|
|||
|
|
@ -15,9 +15,8 @@ import (
|
|||
"strconv"
|
||||
"strings"
|
||||
|
||||
"github.com/aws/aws-sdk-go/aws"
|
||||
"github.com/aws/aws-sdk-go/aws/session"
|
||||
"github.com/aws/aws-sdk-go/service/s3"
|
||||
"github.com/aws/aws-sdk-go-v2/config"
|
||||
"github.com/aws/aws-sdk-go-v2/service/s3"
|
||||
"github.com/hashicorp/go-getter"
|
||||
"github.com/hashicorp/go-getter/helper/url"
|
||||
"go.uber.org/zap"
|
||||
|
|
@ -368,22 +367,22 @@ func (g *S3Getter) Get(wd, src, dst string) error {
|
|||
return err
|
||||
}
|
||||
|
||||
// Create a new AWS session using the default AWS configuration
|
||||
sess := session.Must(session.NewSessionWithOptions(session.Options{
|
||||
SharedConfigState: session.SharedConfigEnable,
|
||||
Config: aws.Config{
|
||||
Region: aws.String(region),
|
||||
},
|
||||
}))
|
||||
// Create a new AWS config and S3 client using AWS SDK v2
|
||||
cfg, err := config.LoadDefaultConfig(context.TODO(),
|
||||
config.WithRegion(region),
|
||||
)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// Create an S3 client using the session
|
||||
s3Client := s3.New(sess)
|
||||
// Create an S3 client using the config
|
||||
s3Client := s3.NewFromConfig(cfg)
|
||||
|
||||
getObjectInput := &s3.GetObjectInput{
|
||||
Bucket: &bucket,
|
||||
Key: &key,
|
||||
}
|
||||
resp, err := s3Client.GetObject(getObjectInput)
|
||||
resp, err := s3Client.GetObject(context.TODO(), getObjectInput)
|
||||
defer func(Body io.ReadCloser) {
|
||||
err := Body.Close()
|
||||
if err != nil {
|
||||
|
|
@ -467,48 +466,47 @@ func (g *S3Getter) S3FileExists(path string) (string, error) {
|
|||
}
|
||||
|
||||
// Region
|
||||
g.Logger.Debugf("Creating session for determining S3 region %s", path)
|
||||
sess := session.Must(session.NewSessionWithOptions(session.Options{
|
||||
SharedConfigState: session.SharedConfigEnable,
|
||||
}))
|
||||
g.Logger.Debugf("Creating config for determining S3 region %s", path)
|
||||
cfg, err := config.LoadDefaultConfig(context.TODO())
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
g.Logger.Debugf("Getting bucket %s location %s", bucket, path)
|
||||
s3Client := s3.New(sess)
|
||||
s3Client := s3.NewFromConfig(cfg)
|
||||
bucketRegion := "us-east-1"
|
||||
getBucketLocationInput := &s3.GetBucketLocationInput{
|
||||
Bucket: aws.String(bucket),
|
||||
Bucket: &bucket,
|
||||
}
|
||||
resp, err := s3Client.GetBucketLocation(getBucketLocationInput)
|
||||
resp, err := s3Client.GetBucketLocation(context.TODO(), getBucketLocationInput)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("Error: Failed to retrieve bucket location: %v\n", err)
|
||||
return "", fmt.Errorf("failed to retrieve bucket location: %v", err)
|
||||
}
|
||||
if resp == nil || resp.LocationConstraint == nil {
|
||||
if resp == nil || string(resp.LocationConstraint) == "" {
|
||||
g.Logger.Debugf("Bucket has no location Assuming us-east-1")
|
||||
} else {
|
||||
bucketRegion = *resp.LocationConstraint
|
||||
bucketRegion = string(resp.LocationConstraint)
|
||||
}
|
||||
g.Logger.Debugf("Got bucket location %s", bucketRegion)
|
||||
|
||||
// File existence
|
||||
g.Logger.Debugf("Creating new session with region to see if file exists")
|
||||
regionSession, err := session.NewSessionWithOptions(session.Options{
|
||||
SharedConfigState: session.SharedConfigEnable,
|
||||
Config: aws.Config{
|
||||
Region: aws.String(bucketRegion),
|
||||
},
|
||||
})
|
||||
g.Logger.Debugf("Creating new config with region to see if file exists")
|
||||
regionCfg, err := config.LoadDefaultConfig(context.TODO(),
|
||||
config.WithRegion(bucketRegion),
|
||||
)
|
||||
if err != nil {
|
||||
g.Logger.Error(err)
|
||||
return bucketRegion, err
|
||||
}
|
||||
g.Logger.Debugf("Creating new s3 client to check if object exists")
|
||||
s3Client = s3.New(regionSession)
|
||||
s3Client = s3.NewFromConfig(regionCfg)
|
||||
headObjectInput := &s3.HeadObjectInput{
|
||||
Bucket: &bucket,
|
||||
Key: &key,
|
||||
}
|
||||
|
||||
g.Logger.Debugf("Fethcing head %s", path)
|
||||
_, err = s3Client.HeadObject(headObjectInput)
|
||||
_, err = s3Client.HeadObject(context.TODO(), headObjectInput)
|
||||
return bucketRegion, err
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue