2023-03-23 15:58:12 +08:00
|
|
|
package minio
|
|
|
|
|
|
|
|
import (
|
2023-04-24 15:14:04 +08:00
|
|
|
"context"
|
2023-03-23 15:58:12 +08:00
|
|
|
"git.hpds.cc/Component/logging"
|
2023-04-24 15:14:04 +08:00
|
|
|
"github.com/minio/minio-go/v7"
|
|
|
|
"github.com/minio/minio-go/v7/pkg/credentials"
|
|
|
|
"io"
|
2023-03-23 15:58:12 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
type MinClient struct {
|
|
|
|
Client *minio.Client
|
|
|
|
Logger *logging.Logger
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewClient(ak, sak, ep string, useSSL bool, logger *logging.Logger) *MinClient {
|
2023-04-24 15:14:04 +08:00
|
|
|
opt := &minio.Options{
|
|
|
|
Creds: credentials.NewStaticV4(ak, sak, ""),
|
|
|
|
Secure: useSSL,
|
|
|
|
}
|
|
|
|
client, err := minio.New(ep, opt)
|
2023-03-23 15:58:12 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return &MinClient{
|
|
|
|
Client: client,
|
|
|
|
Logger: logger,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (cli *MinClient) UploadObject(fn, dst, bucket string) error {
|
2023-04-24 15:14:04 +08:00
|
|
|
_, err := cli.Client.FPutObject(context.Background(), bucket, dst, fn, minio.PutObjectOptions{})
|
2023-03-23 15:58:12 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2023-04-24 15:14:04 +08:00
|
|
|
|
|
|
|
func (cli *MinClient) GetObject(dstUrl, bucket string) ([]byte, error) {
|
|
|
|
f, err := cli.Client.GetObject(context.Background(), bucket, dstUrl, minio.GetObjectOptions{})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
imgByte, _ := io.ReadAll(f)
|
|
|
|
return imgByte, nil
|
|
|
|
}
|