2022-09-16 18:53:45 +08:00
|
|
|
package client
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"crypto/tls"
|
|
|
|
"io"
|
|
|
|
"net/http"
|
|
|
|
"os"
|
|
|
|
"strings"
|
|
|
|
|
2022-10-17 16:32:31 +08:00
|
|
|
"github.com/1Panel-dev/1Panel/backend/constant"
|
2022-09-16 18:53:45 +08:00
|
|
|
"github.com/minio/minio-go/v7"
|
|
|
|
"github.com/minio/minio-go/v7/pkg/credentials"
|
|
|
|
)
|
|
|
|
|
|
|
|
type minIoClient struct {
|
|
|
|
Vars map[string]interface{}
|
|
|
|
client *minio.Client
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewMinIoClient(vars map[string]interface{}) (*minIoClient, error) {
|
|
|
|
var endpoint string
|
|
|
|
var accessKeyID string
|
|
|
|
var secretAccessKey string
|
|
|
|
if _, ok := vars["endpoint"]; ok {
|
|
|
|
endpoint = vars["endpoint"].(string)
|
|
|
|
} else {
|
|
|
|
return nil, constant.ErrInvalidParams
|
|
|
|
}
|
|
|
|
if _, ok := vars["accessKey"]; ok {
|
|
|
|
accessKeyID = vars["accessKey"].(string)
|
|
|
|
} else {
|
|
|
|
return nil, constant.ErrInvalidParams
|
|
|
|
}
|
|
|
|
if _, ok := vars["secretKey"]; ok {
|
|
|
|
secretAccessKey = vars["secretKey"].(string)
|
|
|
|
} else {
|
|
|
|
return nil, constant.ErrInvalidParams
|
|
|
|
}
|
|
|
|
ssl := strings.Split(endpoint, ":")[0]
|
|
|
|
if len(ssl) == 0 || (ssl != "https" && ssl != "http") {
|
|
|
|
return nil, constant.ErrInvalidParams
|
|
|
|
}
|
|
|
|
|
|
|
|
secure := false
|
|
|
|
tlsConfig := &tls.Config{}
|
|
|
|
if ssl == "https" {
|
|
|
|
secure = true
|
|
|
|
tlsConfig.InsecureSkipVerify = true
|
|
|
|
}
|
|
|
|
var transport http.RoundTripper = &http.Transport{
|
|
|
|
TLSClientConfig: tlsConfig,
|
|
|
|
}
|
2022-09-28 18:11:36 +08:00
|
|
|
client, err := minio.New(strings.ReplaceAll(endpoint, ssl+"://", ""), &minio.Options{
|
2022-09-16 18:53:45 +08:00
|
|
|
Creds: credentials.NewStaticV4(accessKeyID, secretAccessKey, ""),
|
|
|
|
Secure: secure,
|
|
|
|
Transport: transport,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &minIoClient{
|
|
|
|
client: client,
|
|
|
|
Vars: vars,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (minIo minIoClient) ListBuckets() ([]interface{}, error) {
|
|
|
|
buckets, err := minIo.client.ListBuckets(context.Background())
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
var result []interface{}
|
|
|
|
for _, bucket := range buckets {
|
|
|
|
result = append(result, bucket.Name)
|
|
|
|
}
|
|
|
|
return result, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (minIo minIoClient) Exist(path string) (bool, error) {
|
|
|
|
if _, ok := minIo.Vars["bucket"]; ok {
|
|
|
|
_, err := minIo.client.GetObject(context.Background(), minIo.Vars["bucket"].(string), path, minio.GetObjectOptions{})
|
|
|
|
if err != nil {
|
2023-05-12 22:30:26 +08:00
|
|
|
return false, err
|
2022-09-16 18:53:45 +08:00
|
|
|
}
|
2023-05-12 22:30:26 +08:00
|
|
|
return true, nil
|
2022-09-16 18:53:45 +08:00
|
|
|
} else {
|
|
|
|
return false, constant.ErrInvalidParams
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (minIo minIoClient) Delete(path string) (bool, error) {
|
|
|
|
if _, ok := minIo.Vars["bucket"]; ok {
|
|
|
|
object, err := minIo.client.GetObject(context.Background(), minIo.Vars["bucket"].(string), path, minio.GetObjectOptions{})
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
info, err := object.Stat()
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
err = minIo.client.RemoveObject(context.Background(), minIo.Vars["bucket"].(string), path, minio.RemoveObjectOptions{
|
|
|
|
GovernanceBypass: true,
|
|
|
|
VersionID: info.VersionID,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
return true, nil
|
|
|
|
} else {
|
|
|
|
return false, constant.ErrInvalidParams
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (minIo minIoClient) Upload(src, target string) (bool, error) {
|
|
|
|
var bucket string
|
|
|
|
if _, ok := minIo.Vars["bucket"]; ok {
|
|
|
|
bucket = minIo.Vars["bucket"].(string)
|
|
|
|
} else {
|
|
|
|
return false, constant.ErrInvalidParams
|
|
|
|
}
|
|
|
|
|
|
|
|
file, err := os.Open(src)
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
defer file.Close()
|
|
|
|
|
|
|
|
fileStat, err := file.Stat()
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
_, err = minIo.client.PutObject(context.Background(), bucket, target, file, fileStat.Size(), minio.PutObjectOptions{ContentType: "application/octet-stream"})
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (minIo minIoClient) Download(src, target string) (bool, error) {
|
|
|
|
if _, ok := minIo.Vars["bucket"]; ok {
|
|
|
|
object, err := minIo.client.GetObject(context.Background(), minIo.Vars["bucket"].(string), src, minio.GetObjectOptions{})
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
localFile, err := os.Create(target)
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
if _, err = io.Copy(localFile, object); err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
return true, nil
|
|
|
|
} else {
|
|
|
|
return false, constant.ErrInvalidParams
|
|
|
|
}
|
|
|
|
}
|
2022-09-28 00:08:21 +08:00
|
|
|
|
2022-09-28 18:11:36 +08:00
|
|
|
func (minIo *minIoClient) GetBucket() (string, error) {
|
|
|
|
if _, ok := minIo.Vars["bucket"]; ok {
|
|
|
|
return minIo.Vars["bucket"].(string), nil
|
|
|
|
} else {
|
|
|
|
return "", constant.ErrInvalidParams
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-08 16:44:12 +08:00
|
|
|
func (minIo minIoClient) ListObjects(prefix string) ([]string, error) {
|
2022-09-28 18:11:36 +08:00
|
|
|
bucket, err := minIo.GetBucket()
|
|
|
|
if err != nil {
|
|
|
|
return nil, constant.ErrInvalidParams
|
|
|
|
}
|
|
|
|
opts := minio.ListObjectsOptions{
|
|
|
|
Recursive: true,
|
|
|
|
Prefix: prefix,
|
|
|
|
}
|
|
|
|
|
2023-08-08 16:44:12 +08:00
|
|
|
var result []string
|
2022-09-28 18:11:36 +08:00
|
|
|
for object := range minIo.client.ListObjects(context.Background(), bucket, opts) {
|
|
|
|
if object.Err != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
result = append(result, object.Key)
|
|
|
|
}
|
|
|
|
return result, nil
|
2022-09-28 00:08:21 +08:00
|
|
|
}
|