方法1
package main
import (
"context"
"log"
minio "github.com/minio/minio-go/v7"
"github.com/minio/minio-go/v7/pkg/credentials"
)
func main() {
ctx := context.Background()
endpoint := "192.168.8.14:9000"
accessKeyID := "user00"
secretAccessKey := "123qweasd"
useSSL := false
minioClient, err := minio.New(endpoint, &minio.Options{
Creds: credentials.NewStaticV4(accessKeyID, secretAccessKey, ""),
Secure: useSSL,
})
if err != nil {
log.Fatalln(err)
}
bucketName := "demo00"
objectName := "test-go.go"
filePath := "C:\\Users\\admin\\Desktop\\go-test\\test-go.go"
contentType := "demo-server/demo00/jb0"
n, err := minioClient.FPutObject(ctx, bucketName, objectName, filePath, minio.PutObjectOptions{ContentType: contentType})
if err != nil {
log.Fatalln("上传文件失败", err)
}
log.Printf("上传文件 %s 成功\n", objectName, n)
}
方法2
package main
import (
"context"
"log"
minio "github.com/minio/minio-go/v7"
"github.com/minio/minio-go/v7/pkg/credentials"
)
func main() {
ctx := context.Background()
endpoint := "192.168.8.14:9000"
accessKeyID := "user00"
secretAccessKey := "123qweasd"
useSSL := false
minioClient, err := minio.New(endpoint, &minio.Options{
Creds: credentials.NewStaticV4(accessKeyID, secretAccessKey, ""),
Secure: useSSL,
})
if err != nil {
log.Fatalln(err)
}
bucketName := "demo00"
location := "demo-server/demo00/jb0/"
err = minioClient.MakeBucket(ctx, bucketName, minio.MakeBucketOptions{Region: location})
if err != nil {
exists, errBucketExists := minioClient.BucketExists(ctx, bucketName)
if errBucketExists == nil && exists {
log.Printf("存储桶 %s已经存在", bucketName)
} else {
log.Fatalln("查询存储桶状态异常", err)
}
} else {
log.Printf("创建存储桶 %s成功", bucketName)
}
objectName := "test-go.go"
filePath := "C:\\Users\\admin\\Desktop\\go-test\\test-go.go"
contentType := "demo-server/demo00/jb0"
n, err := minioClient.FPutObject(ctx, bucketName, objectName, filePath, minio.PutObjectOptions{ContentType: contentType})
if err != nil {
log.Fatalln("上传文件失败", err)
}
log.Printf("上传文件 %s 成功\n", objectName, n)
}
官网实例
https://docs.min.io/docs/golang-client-quickstart-guide.html