优化结构
This commit is contained in:
@@ -4,12 +4,13 @@ import (
|
||||
"context"
|
||||
"fmt"
|
||||
"io"
|
||||
"kra/internal/biz/system"
|
||||
"path"
|
||||
"strings"
|
||||
|
||||
"github.com/huaweicloud/huaweicloud-sdk-go-obs/obs"
|
||||
"kra/internal/biz"
|
||||
"kra/internal/conf"
|
||||
|
||||
"github.com/huaweicloud/huaweicloud-sdk-go-obs/obs"
|
||||
)
|
||||
|
||||
type huaweiStorage struct {
|
||||
@@ -17,7 +18,7 @@ type huaweiStorage struct {
|
||||
bucket, baseURL, prefix string
|
||||
}
|
||||
|
||||
func newHuaweiStorage(config *conf.AdminBackend_ObjectStore) (biz.FileStorage, error) {
|
||||
func newHuaweiStorage(config *conf.AdminBackend_ObjectStore) (system.FileStorage, error) {
|
||||
if config == nil || config.Endpoint == "" || config.Bucket == "" || config.AccessKey == "" || config.SecretKey == "" {
|
||||
return nil, fmt.Errorf("huawei-obs storage configuration is incomplete")
|
||||
}
|
||||
@@ -36,12 +37,12 @@ func (s *huaweiStorage) key(name string) string {
|
||||
func (s *huaweiStorage) unkey(key string) string {
|
||||
return strings.TrimPrefix(strings.TrimPrefix(key, s.prefix), "/")
|
||||
}
|
||||
func (s *huaweiStorage) file(key string, size int64) *biz.StoredFile {
|
||||
func (s *huaweiStorage) file(key string, size int64) *system.StoredFile {
|
||||
name := s.unkey(key)
|
||||
fileURL := s.baseURL + "/" + key
|
||||
return &biz.StoredFile{Name: path.Base(name), Path: name, URL: fileURL, Size: size}
|
||||
return &system.StoredFile{Name: path.Base(name), Path: name, URL: fileURL, Size: size}
|
||||
}
|
||||
func (s *huaweiStorage) Put(_ context.Context, name string, reader io.Reader) (*biz.StoredFile, error) {
|
||||
func (s *huaweiStorage) Put(_ context.Context, name string, reader io.Reader) (*system.StoredFile, error) {
|
||||
key := s.key(name)
|
||||
if _, err := s.client.PutObject(&obs.PutObjectInput{PutObjectBasicInput: obs.PutObjectBasicInput{ObjectOperationInput: obs.ObjectOperationInput{Bucket: s.bucket, Key: key}}, Body: reader}); err != nil {
|
||||
return nil, err
|
||||
@@ -63,7 +64,7 @@ func (s *huaweiStorage) Delete(_ context.Context, name string) error {
|
||||
_, err := s.client.DeleteObject(&obs.DeleteObjectInput{Bucket: s.bucket, Key: s.key(name)})
|
||||
return err
|
||||
}
|
||||
func (s *huaweiStorage) Compose(ctx context.Context, names []string, destination string) (*biz.StoredFile, string, error) {
|
||||
func (s *huaweiStorage) Compose(ctx context.Context, names []string, destination string) (*system.StoredFile, string, error) {
|
||||
return composeFiles(ctx, s, names, destination)
|
||||
}
|
||||
func (s *huaweiStorage) DeletePrefix(ctx context.Context, prefix string) error {
|
||||
@@ -84,7 +85,7 @@ func (s *huaweiStorage) DeletePrefix(ctx context.Context, prefix string) error {
|
||||
cursor = next
|
||||
}
|
||||
}
|
||||
func (s *huaweiStorage) List(_ context.Context, prefix, cursor string, limit int) ([]*biz.StoredFile, string, bool, error) {
|
||||
func (s *huaweiStorage) List(_ context.Context, prefix, cursor string, limit int) ([]*system.StoredFile, string, bool, error) {
|
||||
if limit <= 0 {
|
||||
limit = 100
|
||||
}
|
||||
@@ -92,7 +93,7 @@ func (s *huaweiStorage) List(_ context.Context, prefix, cursor string, limit int
|
||||
if err != nil {
|
||||
return nil, "", false, err
|
||||
}
|
||||
items := make([]*biz.StoredFile, 0, len(result.Contents))
|
||||
items := make([]*system.StoredFile, 0, len(result.Contents))
|
||||
for _, object := range result.Contents {
|
||||
item := s.file(object.Key, object.Size)
|
||||
item.LastModified = object.LastModified
|
||||
|
||||
Reference in New Issue
Block a user