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

如何使用 aws-sdk-go-v2 连接我的本地 minIO 存储?我可以在以前版本的 go SDK 中找到有关如何执行此操作的明确文档,但在 V2 中找不到。我通读了版本 2 的源代码,似乎 aws-sdk-go-v2 删除了禁用 SSL 并指定本地 S3 端点的选项(服务 URL 必须采用亚马逊风格)。

stack overflow Is aws-go-sdk-v2 integrated with local MinIO server?
原文答案
author avatar

接受的答案

您可以通过以下方式轻松完成此操作:

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)

答案:

作者头像

从今天开始,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)
作者头像

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)