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