How can I hook up my local minIO storage with aws-sdk-go-v2
? I can find clear documentation of how to do that in the previous version of go SDK but not with V2. I read through the version 2 source code and it seems aws-sdk-go-v2
removed the option to disable SSL and specify a local S3 endpoint(the service URL has to be in amazon style).
Is aws-go-sdk-v2 integrated with local MinIO server?
Asked Answered
You can do this easily enough with:
const defaultRegion = "us-east-1"
staticResolver := aws.EndpointResolverFunc(func(service, region string) (aws.Endpoint, error) {
return aws.Endpoint{
PartitionID: "aws",
URL: "http://localhost:9123", // or where ever you ran minio
SigningRegion: defaultRegion,
HostnameImmutable: true,
}, nil
})
cfg = aws.Config{
Region: defaultRegion,
Credentials: credentials.NewStaticCredentialsProvider("minioadmin", "minioadmin", ""),
EndpointResolver: staticResolver,
}
s3Client := s3.NewFromConfig(cfg)
Thank you danmux. Your solution worked for me. Thank you! –
Occupancy
I keep getting "not found, ResolveEndpointV2" no matter what i try? –
Amarillo
@Amarillo see tis... https://mcmap.net/q/1134274/-is-aws-go-sdk-v2-integrated-with-local-minio-server –
Greer
@Greer fixed the problem already but thank you! Minio uses /bucketName while cloudflare uses bucketname.website if i remember correctly. –
Amarillo
As of today aws.EndpointResolverFunc is deprecated this is what worked for me:
const defaultRegion = "us-east-1"
hostAddress := "http://localhost:9000"
resolver := aws.EndpointResolverWithOptionsFunc(func(service, region string, options ...any) (aws.Endpoint, error) {
return aws.Endpoint{
PartitionID: "aws",
URL: hostAddress,
SigningRegion: defaultRegion,
HostnameImmutable: true,
}, nil
})
cfg, err = config.LoadDefaultConfig(context.Background(),
config.WithRegion(defaultRegion),
config.WithEndpointResolverWithOptions(resolver),
config.WithCredentialsProvider(credentials.NewStaticCredentialsProvider("minioadmin", "minioadmin", "")),
)
s3Client := s3.NewFromConfig(cfg)
For aws-sdk-go-v2 with EndpointResolverV2
package main
import (
"context"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/service/s3"
"github.com/aws/smithy-go/endpoints"
"io"
"net/url"
)
type Resolver struct {
URL *url.URL
}
func (r *Resolver) ResolveEndpoint(_ context.Context, params s3.EndpointParameters) (transport.Endpoint, error) {
u := *r.URL
u.Path += "/" + *params.Bucket
return transport.Endpoint{URI: u}, nil
}
func main() {
endpointURL, _ := url.Parse("http://localhost:9000") // or where ever you ran minio
client := s3.New(s3.Options{
EndpointResolverV2: &Resolver{URL: endpointURL},
Credentials: aws.CredentialsProviderFunc(func(ctx context.Context) (aws.Credentials, error) {
return aws.Credentials{
AccessKeyID: "minioadmin",
SecretAccessKey: "minioadmin",
}, nil
}),
})
...
}
A derived version for aws-sdk-go-v2
const defaultRegion = "us-east-1"
hostAddress := "http://localhost:9000"
resolver := aws.EndpointResolverWithOptionsFunc(func(service, region string, options ...any) (aws.Endpoint, error) {
return aws.Endpoint{
PartitionID: "aws",
SigningRegion: defaultRegion,
URL: hostAddress,
HostnameImmutable: true,
}, nil
})
cfg := aws.Config{
Region: defaultRegion,
EndpointResolverWithOptions: resolver
Credentials: credentials.NewStaticCredentialsProvider("minioadmin", "minioadmin", ""),
}
return s3.NewFromConfig(cfg)
import (
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/credentials"
"github.com/aws/aws-sdk-go-v2/service/s3"
)
client := s3.NewFromConfig(aws.Config{Region: "us-east-1"}, func(o *s3.Options) {
o.BaseEndpoint = aws.String("http://127.0.0.1:9000")
o.Credentials = credentials.NewStaticCredentialsProvider("ak", "sk", "")
})
# bucket op
© 2022 - 2025 — McMap. All rights reserved.