从github中下载Go版本的SDK

go get github.com/minio/minio-go/v7

以下是Go版本的文件操作代码。

package main

import (

    "fmt"

    "github.com/minio/minio-go/v7"

    "github.com/minio/minio-go/v7/pkg/credentials"

    "log"

    "regexp"

)

type BucketManager struct {

    minioClient *minio.Client

}

func NewBucketManager(minioAddress, minioAdmin, minioPassword string) *BucketManager {

    minioClient, err := minio.New(minioAddress, &minio.Options{

        Creds:  credentials.NewStaticV4(minioAdmin, minioPassword, ""),

        Secure: false,

    })

    if err != nil {

        log.Fatalln(err)

    }

    return &BucketManager{minioClient: minioClient}

}

func (bm *BucketManager) CreateBucket(bucketName string) {

    found, err := bm.minioClient.BucketExists(bucketName)

    if err == nil && found {

        fmt.Println("该存储桶已经存在")

    } else {

        err = bm.minioClient.MakeBucket(bucketName, "")

        if err != nil {

            log.Fatalln(err)

        } else {

            fmt.Println("存储桶创建成功")

        }

    }

}

func (bm *BucketManager) ListBuckets() {

    buckets, err := bm.minioClient.ListBuckets()

    if err != nil {

        log.Fatalln(err)

    }

    for _, bucket := range buckets {

        fmt.Println(bucket.Name)

    }

}

func (bm *BucketManager) RemoveBucket(bucketName string) {

    err := bm.minioClient.RemoveBucket(bucketName)

    if err != nil {

        log.Fatalln(err)

    } else {

        fmt.Println("删除存储桶 " + bucketName + " 成功")

    }

}

func (bm *BucketManager) UploadFile(bucketName, objectName, filePath string) {

    found, err := bm.minioClient.BucketExists(bucketName)

    if err == nil && !found {

        err = bm.minioClient.MakeBucket(bucketName, "")

    }

    if err == nil {

        _, err = bm.minioClient.FPutObject(bucketName, objectName, filePath, minio.PutObjectOptions{})

        if err != nil {

            log.Fatalln(err)

        }

    }

}

func (bm *BucketManager) DownloadFile(bucketName, objectName, filePath string) {

    err := bm.minioClient.FGetObject(bucketName, objectName, filePath, minio.GetObjectOptions{})

    if err != nil {

        log.Fatalln(err)

    }

}

func (bm *BucketManager) RemoveObject(bucketName, objectName string) {

    err := bm.minioClient.RemoveObject(bucketName, minio.RemoveObjectOptions{ObjectName: objectName})

    if err != nil {

        log.Fatalln(err)

    }

}

func (bm *BucketManager) PathValidation(objectName string) bool {

    match, _ := regexp.MatchString(`^a\/(b|c)\/[^\/]+\/[^\/]+\/[^\/]+\/[^\/]+\/[^\/]+$`, objectName)

    return match

}

func main() {

    bm := NewBucketManager("minioAddress", "minioAdmin", "minioPassword")

    bm.CreateBucket("exampleBucket")

}