Is aws-go-sdk-v2 integrated with local MinIO server?
Asked Answered
O

5

9

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).

Occupancy answered 17/5, 2021 at 19:6 Comment(0)
G
13

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)
Greer answered 23/6, 2021 at 0:35 Comment(4)
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-serverGreer
@Greer fixed the problem already but thank you! Minio uses /bucketName while cloudflare uses bucketname.website if i remember correctly.Amarillo
A
3

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)
Actinomycin answered 7/10, 2022 at 8:44 Comment(0)
T
3

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
        }),
    })

    ...
}
Toilet answered 6/9, 2023 at 2:53 Comment(0)
G
2

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)

Gramophone answered 20/10, 2022 at 21:21 Comment(0)
C
0

As aws-sdk-go-v2

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
Casarez answered 31/7, 2024 at 9:6 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.