From 50713cdbd8f962ea39c4b64aa9f14bfa644c5009 Mon Sep 17 00:00:00 2001 From: neo-f Date: Thu, 23 Mar 2023 22:32:19 +0800 Subject: [PATCH] feat(storage): compat for cos --- internal/dal/storage.go | 4 ++-- pkg/storage/storage.go | 11 ++++++----- pkg/storage/storage_minio.go | 18 +++++++++--------- 3 files changed, 17 insertions(+), 16 deletions(-) diff --git a/internal/dal/storage.go b/internal/dal/storage.go index b4f1d0b..be8388e 100644 --- a/internal/dal/storage.go +++ b/internal/dal/storage.go @@ -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 } diff --git a/pkg/storage/storage.go b/pkg/storage/storage.go index 0dd62e7..fae6a00 100644 --- a/pkg/storage/storage.go +++ b/pkg/storage/storage.go @@ -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") } diff --git a/pkg/storage/storage_minio.go b/pkg/storage/storage_minio.go index 432a6a6..4a86fb6 100644 --- a/pkg/storage/storage_minio.go +++ b/pkg/storage/storage_minio.go @@ -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 {