package main
import (
"context"
"fmt"
"github.com/minio/minio-go/v7"
"github.com/minio/minio-go/v7/pkg/credentials"
"os"
"path/filepath"
)
func main() {
// 用户的 MinIO 访问密钥和密钥,请替换为实际的访问密钥和密钥
accessKeyID := "ddddddddddddd"
secretAccessKey := "kkkkkkkkkkkkkkkkkkkkk"
// 存储桶的名称,请替换为实际的存储桶名称
bucketName := "bbbbbbbbb"
// MinIO 服务的 endpoint,请替换为实际的 endpoint
endpoint := "test.com"
// 创建一个 MinIO 客户端
minioClient, err := minio.New(endpoint, &minio.Options{
Creds: credentials.NewStaticV4(accessKeyID, secretAccessKey, ""),
Secure: false, // 如果使用 HTTPS,请设置为 true
})
if err != nil {
fmt.Println("Failed to create MinIO client:", err)
os.Exit(1)
}
// 列出存储桶中的对象
objectCh := minioClient.ListObjects(context.Background(), bucketName, minio.ListObjectsOptions{
Recursive: true,
})
// 下载对象到本地
for object := range objectCh {
if object.Err != nil {
fmt.Println("Failed to list objects:", object.Err)
os.Exit(1)
}
// 获取对象的完整路径
objectPath := object.Key
// 创建本地目录结构
localDir := filepath.Dir(objectPath)
err := os.MkdirAll(localDir, 0755)
if err != nil {
fmt.Println("Failed to create local directory:", err)
os.Exit(1)
}
// 下载对象到本地文件
localFilePath := filepath.Join(localDir, filepath.Base(objectPath))
err = minioClient.FGetObject(context.Background(), bucketName, objectPath, localFilePath, minio.GetObjectOptions{})
if err != nil {
fmt.Println("Failed to download object:", err)
os.Exit(1)
}
fmt.Println("Downloaded", objectPath, "to", localFilePath)
}
}