aws-go-sdk-v2是否与本地MinIO服务器集成?

tag5nh1u  于 2023-09-28  发布在  Go
关注(0)|答案(4)|浏览(125)

如何将本地minIO存储连接到aws-sdk-go-v2?我可以在以前版本的go SDK中找到关于如何做到这一点的清晰文档,但在V2中没有。我通读了版本2的源代码,似乎aws-sdk-go-v2删除了禁用SSL和指定本地S3端点的选项(服务URL必须是amazon风格的)。

hxzsmxv2

hxzsmxv21#

你可以很容易地做到这一点:

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

ykejflvf2#

截至今天aws.EndpointResolverFunc已弃用,这是我的工作:

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

shyt4zoc3#

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

drnojrws4#

对于带有EndpointResolverV2的aws-sdk-go-v2

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

    ...
}

相关问题