是的,aws-go-sdk-v2 可以与本地 MinIO 服务器集成。
以下是一个使用 aws-go-sdk-v2 和 MinIO 的示例代码:
首先,需要安装 aws-sdk-go 和 MinIO Go 客户端:
go get github.com/aws/aws-sdk-go-v2 go get github.com/minio/minio-go/v7
接下来,需要创建用于连接 MinIO 的 aws.Config 和 MinIO 客户端:
import ( "context" "github.com/aws/aws-sdk-go-v2/aws" "github.com/aws/aws-sdk-go-v2/config" "github.com/aws/aws-sdk-go-v2/service/s3" "github.com/minio/minio-go/v7" )
func main() { // Create an S3 client cfg, err := config.LoadDefaultConfig(context.TODO()) if err != nil { panic("failed to load config, " + err.Error()) } s3Client := s3.NewFromConfig(cfg)
// Create a MinIO client minioClient, err := minio.New("localhost:9000", "access_key", "secret_key", false) if err != nil { panic("failed to create minio client, " + err.Error()) }
// Upload a file to MinIO _, err = minioClient.FPutObject(context.Background(), "mybucket", "mykey", "myfile", minio.PutObjectOptions{}) if err != nil { panic("failed to upload file, " + err.Error()) }
// Download the file from MinIO using the S3 client resp, err := s3Client.GetObject(context.TODO(), &s3.GetObjectInput{ Bucket: aws.String("mybucket"), Key: aws.String("mykey"), }) if err != nil { panic("failed to download file, " + err.Error()) } defer resp.Body.Close()
// Do something with the downloaded file }
这个例子中,我们首先创建了 S3 客户端和 MinIO 客户端。然后,我们使用 MinIO 客户端将文件上传到 MinIO 存储桶。最后,我们使用 S3 客户端从 MinIO 下载文件。