minio-sample/main.go

72 lines
1.9 KiB
Go
Raw Normal View History

2025-03-24 10:01:52 +00:00
package main
import (
"context"
"log"
"github.com/minio/minio-go/v7"
"github.com/minio/minio-go/v7/pkg/credentials"
)
func main() {
endpoint := "i.myschools.me:6733"
accessKeyID := "Kbhjuw26QwKtV49lAGuR"
secretAccessKey := "iSFz9KI8Oqx3kNFLNDFHkbyHtRTw6d4BMwgYIy1Y"
useSSL := false
// 初始化 MinIO 客户端
minioClient, err := minio.New(endpoint, &minio.Options{
Creds: credentials.NewStaticV4(accessKeyID, secretAccessKey, ""),
Secure: useSSL,
})
if err != nil {
log.Fatalln(err)
}
// 创建存储桶
bucketName := "sandbox"
location := "cn-north-1"
err = minioClient.MakeBucket(context.Background(), bucketName, minio.MakeBucketOptions{Region: location})
if err != nil {
// 检查存储桶是否已经存在
exists, errBucketExists := minioClient.BucketExists(context.Background(), bucketName)
if errBucketExists == nil && exists {
log.Printf("Bucket %s already exists\n", bucketName)
} else {
log.Fatalln(err)
}
} else {
log.Printf("Successfully created bucket %s\n", bucketName)
}
// 上传文件
objectName := "ys.zip"
filePath := "/Users/suguo/Desktop/YS-M8固件升级.zip"
contentType := "text/plain"
n, err := minioClient.FPutObject(context.Background(), bucketName, objectName, filePath, minio.PutObjectOptions{ContentType: contentType})
if err != nil {
log.Fatalln(err)
}
log.Printf("Successfully uploaded %s of size %d\n", objectName, n.Size)
// 下载文件
outputPath := "downloaded_file.txt"
err = minioClient.FGetObject(context.Background(), bucketName, objectName, outputPath, minio.GetObjectOptions{})
if err != nil {
log.Fatalln(err)
}
log.Printf("Successfully downloaded %s to %s\n", objectName, outputPath)
// 删除文件
// err = minioClient.RemoveObject(context.Background(), bucketName, objectName, minio.RemoveObjectOptions{})
// if err != nil {
// log.Fatalln(err)
// }
// log.Printf("Successfully removed %s from %s\n", objectName, bucketName)
}