I am new in Go and AWS and have been trying for almost a week to connect to a local deployment of S3 to no avail. I am currently using the AWS-SDK v1 for Go and are trying to create a simple connection to the endpoint so that I can list the contents of a bucket. I have simple client setup with set credentials but every time, I get either an authentication error, or DNS resolution error. Can anyone help please?
I’ve tried several different ways of specifying the credentials (credentials file, environment variables, setting env vars in the golang program) but still get the same error. yet when I use them in Python it works with no issue. I have read similar cases but none of the solutions seem to work for me
Below is my code
package main
import (
"fmt"
"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"
)
type S3Client struct {
url string
accessKey string
secretKey string
bucketName string
//httpClient *http.Client
}
func NewClient(endpointUrl, accessKey, secretKey, bucketName string) (S3Client, error) {
justS3 := S3Client{
url: endpointUrl,
accessKey: accessKey,
secretKey: secretKey,
bucketName: bucketName,
}
return justS3, nil
}
func (c *S3Client) ListBucketContents() {
creds := credentials.NewStaticCredentials(c.accessKey, c.secretKey, "")
sess, err := session.NewSession(&aws.Config{
Credentials: creds,
Endpoint: aws.String(c.url),
Region: aws.String("us-east-01"),
DisableSSL: aws.Bool(true),
})
if err != nil {
fmt.Printf("Unable to login, due to error %v", err)
}
//create an s3 service client
svc := s3.New(sess)
//get list of all items
resp, err := svc.ListObjectsV2(&s3.ListObjectsV2Input{Bucket:
aws.String(c.bucketName)})
if err != nil {
fmt.Printf("unable to list items in bucket %q, %v,", c.bucketName, err)
}
for _, item := range resp.Contents {
fmt.Printf("Below is a list of Items under the bucket %s .n", c.bucketName)
fmt.Print("*********************************")
fmt.Println("Name: ", item.Key)
fmt.Print("Last Modified", item.LastModified)
fmt.Print("Size", item.Size)
fmt.Println("")
}
}
package main
import (
"fmt"
)
func main() {
client, err := NewClient("<endpoint>", "Accesskey", "SecretKey", "bucketname")
if err != nil {
fmt.Println("The logging has failed")
}
client.ListBucketContents()
}
2
Answers
Found the solution by setting the S3ForcePathStyle field to true on the Config struct, and removing the V2 reference in my functions.
resp, err := svc.ListObjects(&s3.ListObjectsInput{Bucket: aws.String(c.bucketName)}).
It's pretty weird but the are a lot of ssl authentication issues with V1. will close the case now
AWS has announced end-of-support for AWS SDK for Go V1 and recommends to migrate to AWS SDK for Go V2.
With V2 you would do: