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