1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
| package main
import ( "context" "log"
"github.com/minio/minio-go/v7" "github.com/minio/minio-go/v7/pkg/credentials" )
func main() {
endpoint := "192.168.0.221:9000" accessKey := "tvR7984WQeZZig2tWkgE" secretKey := "Tf0qT98Yh6oGCqqAvHEUVn0liskCe2CHHrhrL1LL" useSSL := false
minioClient, err := minio.New(endpoint, &minio.Options{ Creds: credentials.NewStaticV4(accessKey, secretKey, ""), Secure: useSSL, }) if err != nil { log.Fatalln("创建客户端失败: " + err.Error()) }
bucketName := "test-bucket" location := "company_221_docker"
err = minioClient.MakeBucket(context.Background(), bucketName, minio.MakeBucketOptions{Region: location}) if err != nil { exist, existErr := minioClient.BucketExists(context.Background(), bucketName) if existErr == nil && exist { log.Printf("Bucket %s already exists.\n", bucketName) } else { log.Fatalln("创建桶失败: " + err.Error()) } }
log.Printf("Successfully created %s\n", bucketName)
filePath := "/Users/yw/Project/go/demo/minio/credentials.json" uploadFileName := "test-001.json" contentType := "application/json"
info, err := minioClient.FPutObject(context.Background(), bucketName, uploadFileName, filePath, minio.PutObjectOptions{ContentType: contentType}) if err != nil { log.Fatalln("上传文件失败" + err.Error())
}
log.Printf("Successfully uploaded %s of size %d\n", uploadFileName, info.Size)
}
|