2023-03-23 13:52:44 +08:00
|
|
|
package storage
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"net/url"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/minio/minio-go/v7"
|
|
|
|
"github.com/minio/minio-go/v7/pkg/credentials"
|
|
|
|
)
|
|
|
|
|
2023-03-23 17:43:27 +08:00
|
|
|
type StorageMinio struct {
|
2023-03-23 13:52:44 +08:00
|
|
|
client *minio.Client
|
|
|
|
|
|
|
|
endpoint string
|
|
|
|
accessKey string
|
|
|
|
accessSecret string
|
|
|
|
secure bool
|
|
|
|
|
|
|
|
bucket string
|
|
|
|
}
|
|
|
|
|
2023-03-23 17:43:27 +08:00
|
|
|
func newMinioStorage(endpoint, accessKey, accessSecret string, secure bool, bucket string) (*StorageMinio, error) {
|
2023-03-23 13:52:44 +08:00
|
|
|
client, err := minio.New(endpoint, &minio.Options{
|
|
|
|
Creds: credentials.NewStaticV4(accessKey, accessSecret, ""),
|
|
|
|
Secure: secure,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2023-03-23 17:43:27 +08:00
|
|
|
return &StorageMinio{
|
2023-03-23 13:52:44 +08:00
|
|
|
client: client,
|
|
|
|
endpoint: endpoint,
|
|
|
|
accessKey: accessKey,
|
|
|
|
accessSecret: accessSecret,
|
|
|
|
secure: secure,
|
|
|
|
bucket: bucket,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// PresignedPutObject implements ObjectStorage
|
2023-03-23 17:43:27 +08:00
|
|
|
func (m *StorageMinio) PresignedPutObject(ctx context.Context, objectName string, expires time.Duration) (u *url.URL, err error) {
|
2023-03-23 13:52:44 +08:00
|
|
|
return m.client.PresignedPutObject(ctx, m.bucket, objectName, expires)
|
|
|
|
}
|
|
|
|
|
|
|
|
// PresignedGetObject implements ObjectStorage
|
2023-03-23 17:43:27 +08:00
|
|
|
func (m *StorageMinio) PresignedGetObject(ctx context.Context, objectName string, expires time.Duration) (u *url.URL, err error) {
|
2023-03-23 13:52:44 +08:00
|
|
|
return m.client.PresignedGetObject(ctx, m.bucket, objectName, expires, nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
// MoveObject implements ObjectStorage
|
2023-03-23 17:43:27 +08:00
|
|
|
func (m *StorageMinio) MoveObject(ctx context.Context, srcObject, dstObject string) (err error) {
|
2023-03-23 13:52:44 +08:00
|
|
|
dst := minio.CopyDestOptions{Bucket: m.bucket, Object: dstObject}
|
|
|
|
src := minio.CopySrcOptions{Bucket: m.bucket, Object: srcObject}
|
|
|
|
if _, err := m.client.CopyObject(ctx, dst, src); err != nil {
|
2023-03-23 17:43:27 +08:00
|
|
|
return fmt.Errorf("move object failed while copy: %w", err)
|
2023-03-23 13:52:44 +08:00
|
|
|
}
|
|
|
|
if err := m.client.RemoveObject(ctx, m.bucket, srcObject, minio.RemoveObjectOptions{}); err != nil {
|
2023-03-23 17:43:27 +08:00
|
|
|
return fmt.Errorf("move object failed while remove: %w", err)
|
2023-03-23 13:52:44 +08:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// ObjectExists implements ObjectStorage
|
2023-03-23 17:43:27 +08:00
|
|
|
func (m *StorageMinio) ObjectStats(ctx context.Context, objectName string) (stat *minio.ObjectInfo, exists bool, err error) {
|
|
|
|
obj, err := m.client.StatObject(ctx, m.bucket, objectName, minio.StatObjectOptions{})
|
2023-03-23 13:52:44 +08:00
|
|
|
if err != nil {
|
2023-03-23 17:43:27 +08:00
|
|
|
switch minio.ToErrorResponse(err).Code {
|
2023-03-23 13:52:44 +08:00
|
|
|
case "NoSuchKey", "NoSuchBucket":
|
2023-03-23 17:43:27 +08:00
|
|
|
return nil, false, nil
|
2023-03-23 13:52:44 +08:00
|
|
|
default:
|
2023-03-23 17:43:27 +08:00
|
|
|
return nil, false, err
|
2023-03-23 13:52:44 +08:00
|
|
|
}
|
|
|
|
}
|
2023-03-23 17:43:27 +08:00
|
|
|
return &obj, true, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// MoveObject implements ObjectStorage
|
|
|
|
func (m *StorageMinio) DeleteObject(ctx context.Context, objectName string) (err error) {
|
|
|
|
if err := m.client.RemoveObject(ctx, m.bucket, objectName, minio.RemoveObjectOptions{}); err != nil {
|
|
|
|
return fmt.Errorf("move object failed while remove: %w", err)
|
|
|
|
}
|
|
|
|
return nil
|
2023-03-23 13:52:44 +08:00
|
|
|
}
|