118 lines
3.2 KiB
Go
118 lines
3.2 KiB
Go
package main
|
|
|
|
import (
|
|
"github.com/minio/minio-go"
|
|
"strings"
|
|
)
|
|
|
|
type CmiiMinioOperator struct {
|
|
LocalEndpoint string
|
|
PublicEndpoint string
|
|
|
|
AccessKeyID string
|
|
SecretAccessKey string
|
|
|
|
LocalClient *minio.Client
|
|
PublicClient *minio.Client
|
|
DemoClient *minio.Client
|
|
|
|
OctopusBucketName string
|
|
}
|
|
|
|
var DefaultCmiiMinioOperator = newInstance()
|
|
|
|
func newInstance() *CmiiMinioOperator {
|
|
op := &CmiiMinioOperator{}
|
|
|
|
op.buildCmiiMinioOperator()
|
|
|
|
return op
|
|
}
|
|
|
|
const (
|
|
DefaultLocalEndpoint = "10.250.0.100:9000"
|
|
DefaultPublicEndpoint = "42.192.52.227:9000"
|
|
DemoEndpoint = "oss.demo.uavcmlc.com:18000"
|
|
DefaultAccessKeyID = "cmii"
|
|
DefaultSecretAccessKey = "B#923fC7mk"
|
|
DefaultOctopusBucketName = "octopus"
|
|
)
|
|
|
|
func (op *CmiiMinioOperator) buildCmiiMinioOperator() {
|
|
|
|
var err error
|
|
if op.LocalClient == nil {
|
|
// 初始化Minio客户端对象。
|
|
op.LocalClient, err = minio.New(DefaultLocalEndpoint, DefaultAccessKeyID, DefaultSecretAccessKey, false)
|
|
if err != nil {
|
|
log.ErrorF("[buildCmiiMinioOperator] - build for LocalClient error !=> %s", err.Error())
|
|
}
|
|
}
|
|
|
|
if op.PublicClient == nil {
|
|
// 初始化Minio客户端对象。
|
|
op.PublicClient, err = minio.New(DefaultPublicEndpoint, DefaultAccessKeyID, DefaultSecretAccessKey, false)
|
|
if err != nil {
|
|
log.ErrorF("[buildCmiiMinioOperator] - build for PublicClient error ! => %s", err.Error())
|
|
}
|
|
}
|
|
|
|
if op.DemoClient == nil {
|
|
// 初始化Minio客户端对象。
|
|
op.DemoClient, err = minio.New(DemoEndpoint, DefaultAccessKeyID, DefaultSecretAccessKey, true)
|
|
if err != nil {
|
|
log.ErrorF("[buildCmiiMinioOperator] - build for DemoClient error ! => %s", err.Error())
|
|
}
|
|
}
|
|
|
|
log.DebugF("[buildCmiiMinioOperator] - build client success !")
|
|
}
|
|
|
|
func (op *CmiiMinioOperator) UploadToLocalOctopus(filePath, fileName string) bool {
|
|
return op.uploadToOctopus(op.LocalClient, filePath, fileName)
|
|
}
|
|
|
|
func (op *CmiiMinioOperator) UploadToPublicOctopus(filePath, fileName string) bool {
|
|
|
|
return op.uploadToOctopus(op.PublicClient, filePath, fileName)
|
|
}
|
|
|
|
func (op *CmiiMinioOperator) UploadToDemo(bucketName, filePath, fileName string) bool {
|
|
|
|
return op.uploadToOss(op.DemoClient, bucketName, filePath, fileName)
|
|
}
|
|
|
|
func (op *CmiiMinioOperator) uploadToOctopus(client *minio.Client, filePath, fileName string) bool {
|
|
|
|
return op.uploadToOss(client, DefaultOctopusBucketName, filePath, fileName)
|
|
}
|
|
|
|
func (op *CmiiMinioOperator) uploadToOss(client *minio.Client, bucketName, filePath, fileName string) bool {
|
|
|
|
if !strings.HasSuffix(filePath, "/") {
|
|
filePath += "/"
|
|
}
|
|
|
|
realFileName := fileName
|
|
// 解析bucket上传的真实名称
|
|
if strings.Contains(bucketName, "/") {
|
|
splitN := strings.SplitN(bucketName, "/", 2)
|
|
bucketName = splitN[0]
|
|
if !strings.HasSuffix(splitN[1], "/") {
|
|
splitN[1] = splitN[1] + "/"
|
|
}
|
|
realFileName = splitN[1] + fileName
|
|
}
|
|
|
|
// 使用PutObject上传文件
|
|
// realFileName ==> tmp/123/123.txt
|
|
n, err := client.FPutObject(bucketName, realFileName, filePath+fileName, minio.PutObjectOptions{})
|
|
if err != nil {
|
|
log.ErrorF("[uploadToOss] - upload %s error %s", filePath+fileName, err.Error())
|
|
return false
|
|
}
|
|
|
|
log.InfoF("[uploadToOss] - uploaded %s of size %d", filePath+fileName, n)
|
|
return true
|
|
}
|