I have recently upgraded (19 Feb release of SDK) the AWS SDK v2. The older version was very much old (year 2021). I found some breaking changes with this. I am not able to upload file on AWS S3 bucket.
Configurations
the default credentials file is present in .aws folder in my $HOME directory.
Consider following code for the file upload:
func UploadFileInS3Bucket(fileContent io.Reader, filePath, bucketName, contentType string) (*manager.UploadOutput, error) {
cfg, _ := awsconfig.LoadDefaultConfig(context.TODO(),
awsconfig.WithRegion("us-west-1"),
)
client := s3.NewFromConfig(cfg)
// Create an uploader with the client and options
uploader := manager.NewUploader(client)
cacheExpireTime := time.Now().AddDate(1, 0, 0)
cacheMaxAgeSecs := "max-age=31556926" // seconds in 1 year
// Upload the file to S3.
uploadResp, err := uploader.Upload(context.TODO(), &s3.PutObjectInput{
Bucket: aws.String(bucketName),
Key: aws.String(filePath),
Body: fileContent,
ContentType: aws.String(contentType),
Expires: &cacheExpireTime,
CacheControl: aws.String(cacheMaxAgeSecs),
})
return uploadResp, err
}
Error with this:
operation error S3: PutObject, get identity: get credentials: failed to refresh cached credentials, no EC2 IMDS role found, operation error ec2imds: GetMetadata, http response error StatusCode: 404, request to EC2 IMDS failed
It was working fine before upgrade. Any suggestions to fix this ?