feat(storage): compat for cos
This commit is contained in:
parent
2d3cf0857a
commit
50713cdbd8
@ -11,10 +11,10 @@ import (
|
||||
|
||||
var (
|
||||
storageOnce sync.Once
|
||||
storageInstance *storage.StorageMinio
|
||||
storageInstance *storage.Storage
|
||||
)
|
||||
|
||||
func GetStorage() *storage.StorageMinio {
|
||||
func GetStorage() *storage.Storage {
|
||||
storageOnce.Do(initMinio)
|
||||
return storageInstance
|
||||
}
|
||||
|
@ -3,6 +3,7 @@ package storage
|
||||
import (
|
||||
"errors"
|
||||
"net/url"
|
||||
"strings"
|
||||
|
||||
"github.com/spf13/cast"
|
||||
)
|
||||
@ -20,7 +21,7 @@ import (
|
||||
// GetObjectCreatedTime(ctx context.Context, objectName string) (time.Time, error)
|
||||
// }
|
||||
|
||||
func NewObjectStorage(dsn string) (*StorageMinio, error) {
|
||||
func NewObjectStorage(dsn string) (*Storage, error) {
|
||||
u, err := url.Parse(dsn)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@ -30,14 +31,14 @@ func NewObjectStorage(dsn string) (*StorageMinio, error) {
|
||||
pass, _ := u.User.Password()
|
||||
|
||||
switch u.Scheme {
|
||||
// case "cos":
|
||||
// ex: cos://examplebucket-1250000000.cos.COS_REGION.myqcloud.com
|
||||
// return newCosStorage(u.Host, user, pass)
|
||||
case "cos":
|
||||
bucket := strings.Split(u.Host, "-")[0]
|
||||
return newStorage(u.Host, user, pass, true, bucket)
|
||||
case "minio":
|
||||
// ex: minio://minio-api:9001?secure=false&bucket=images
|
||||
secure := cast.ToBool(u.Query().Get("secure"))
|
||||
bucket := u.Query().Get("bucket")
|
||||
return newMinioStorage(u.Host, user, pass, secure, bucket)
|
||||
return newStorage(u.Host, user, pass, secure, bucket)
|
||||
default:
|
||||
return nil, errors.New("the storage type not support yet")
|
||||
}
|
||||
|
@ -10,7 +10,7 @@ import (
|
||||
"github.com/minio/minio-go/v7/pkg/credentials"
|
||||
)
|
||||
|
||||
type StorageMinio struct {
|
||||
type Storage struct {
|
||||
client *minio.Client
|
||||
|
||||
endpoint string
|
||||
@ -21,7 +21,7 @@ type StorageMinio struct {
|
||||
bucket string
|
||||
}
|
||||
|
||||
func newMinioStorage(endpoint, accessKey, accessSecret string, secure bool, bucket string) (*StorageMinio, error) {
|
||||
func newStorage(endpoint, accessKey, accessSecret string, secure bool, bucket string) (*Storage, error) {
|
||||
client, err := minio.New(endpoint, &minio.Options{
|
||||
Creds: credentials.NewStaticV4(accessKey, accessSecret, ""),
|
||||
Secure: secure,
|
||||
@ -29,7 +29,7 @@ func newMinioStorage(endpoint, accessKey, accessSecret string, secure bool, buck
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &StorageMinio{
|
||||
return &Storage{
|
||||
client: client,
|
||||
endpoint: endpoint,
|
||||
accessKey: accessKey,
|
||||
@ -40,17 +40,17 @@ func newMinioStorage(endpoint, accessKey, accessSecret string, secure bool, buck
|
||||
}
|
||||
|
||||
// PresignedPutObject implements ObjectStorage
|
||||
func (m *StorageMinio) PresignedPutObject(ctx context.Context, objectName string, expires time.Duration) (u *url.URL, err error) {
|
||||
func (m *Storage) PresignedPutObject(ctx context.Context, objectName string, expires time.Duration) (u *url.URL, err error) {
|
||||
return m.client.PresignedPutObject(ctx, m.bucket, objectName, expires)
|
||||
}
|
||||
|
||||
// PresignedGetObject implements ObjectStorage
|
||||
func (m *StorageMinio) PresignedGetObject(ctx context.Context, objectName string, expires time.Duration) (u *url.URL, err error) {
|
||||
func (m *Storage) PresignedGetObject(ctx context.Context, objectName string, expires time.Duration) (u *url.URL, err error) {
|
||||
return m.client.PresignedGetObject(ctx, m.bucket, objectName, expires, nil)
|
||||
}
|
||||
|
||||
// MoveObject implements ObjectStorage
|
||||
func (m *StorageMinio) MoveObject(ctx context.Context, srcObject, dstObject string) (err error) {
|
||||
func (m *Storage) MoveObject(ctx context.Context, srcObject, dstObject string) (err error) {
|
||||
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 {
|
||||
@ -63,7 +63,7 @@ func (m *StorageMinio) MoveObject(ctx context.Context, srcObject, dstObject stri
|
||||
}
|
||||
|
||||
// ObjectExists implements ObjectStorage
|
||||
func (m *StorageMinio) ObjectStats(ctx context.Context, objectName string) (stat *minio.ObjectInfo, exists bool, err error) {
|
||||
func (m *Storage) ObjectStats(ctx context.Context, objectName string) (stat *minio.ObjectInfo, exists bool, err error) {
|
||||
obj, err := m.client.StatObject(ctx, m.bucket, objectName, minio.StatObjectOptions{})
|
||||
if err != nil {
|
||||
switch minio.ToErrorResponse(err).Code {
|
||||
@ -77,7 +77,7 @@ func (m *StorageMinio) ObjectStats(ctx context.Context, objectName string) (stat
|
||||
}
|
||||
|
||||
// MoveObject implements ObjectStorage
|
||||
func (m *StorageMinio) DeleteObject(ctx context.Context, objectName string) (err error) {
|
||||
func (m *Storage) 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)
|
||||
}
|
||||
@ -85,7 +85,7 @@ func (m *StorageMinio) DeleteObject(ctx context.Context, objectName string) (err
|
||||
}
|
||||
|
||||
// MoveObject implements ObjectStorage
|
||||
func (m *StorageMinio) DeleteObjects(ctx context.Context, objectNames []string) []minio.RemoveObjectError {
|
||||
func (m *Storage) DeleteObjects(ctx context.Context, objectNames []string) []minio.RemoveObjectError {
|
||||
// FIXME: why does DeleteObjects not working??
|
||||
var errors []minio.RemoveObjectError
|
||||
for _, objectName := range objectNames {
|
||||
|
Loading…
x
Reference in New Issue
Block a user