Skip to content

Commit

Permalink
update xstorage
Browse files Browse the repository at this point in the history
  • Loading branch information
showurl committed Feb 21, 2023
1 parent b0b68e1 commit 16956fe
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 0 deletions.
3 changes: 3 additions & 0 deletions app/mgmt/internal/logic/x_uploadFile.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,9 @@ func (l *UploadFileLogic) UploadFile(key string, data []byte) (string, error) {
l.Errorf("请配置app对象存储")
return "", errors.New("请配置app对象存储")
}
if exist, err := storage.ExistObject(l.ctx, key); err == nil && exist {
return storage.GetObjectUrl(key), nil
}
return storage.PutObject(l.ctx, key, data)
}

Expand Down
13 changes: 13 additions & 0 deletions common/utils/xstorage/cos.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package xstorage
import (
"bytes"
"context"
"fmt"
"github.com/cherish-chat/xxim-server/common/pb"
cos "github.com/tencentyun/cos-go-sdk-v5"
"net/http"
Expand All @@ -15,6 +16,18 @@ type CosStorage struct {
bucket *cos.Client
}

func (o *CosStorage) GetObjectUrl(key string) string {
return fmt.Sprintf("%s/%s", o.Config.BucketUrl, key)
}

func (o *CosStorage) ExistObject(ctx context.Context, key string) (exists bool, err error) {
response, err := o.bucket.Object.Head(ctx, key, nil)
if err != nil {
return false, err
}
return response.StatusCode == http.StatusOK, nil
}

var singletonCosStorage *CosStorage

// NewCosStorage 创建CosStorage
Expand Down
3 changes: 3 additions & 0 deletions common/utils/xstorage/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ import "context"

// Storage 抽象存储接口
type Storage interface {
// ExistObject 对象是否存在
ExistObject(ctx context.Context, key string) (exists bool, err error)
// PutObject 上传对象
PutObject(ctx context.Context, key string, data []byte) (url string, err error)
GetObjectUrl(key string) string
}
9 changes: 9 additions & 0 deletions common/utils/xstorage/kodo.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package xstorage
import (
"bytes"
"context"
"fmt"
"github.com/cherish-chat/xxim-server/common/pb"
"github.com/qiniu/go-sdk/v7/auth/qbox"
"github.com/qiniu/go-sdk/v7/storage"
Expand All @@ -15,6 +16,14 @@ type KodoStorage struct {
putPolicy *storage.PutPolicy
}

func (s *KodoStorage) GetObjectUrl(key string) string {
return fmt.Sprintf("%s/%s", s.Config.BucketUrl, key)
}

func (s *KodoStorage) ExistObject(ctx context.Context, key string) (exists bool, err error) {
return false, nil
}

var singletonKodoStorage *KodoStorage

// NewKodoStorage 创建kodo存储
Expand Down
17 changes: 17 additions & 0 deletions common/utils/xstorage/minio.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package xstorage
import (
"bytes"
"context"
"fmt"
"github.com/cherish-chat/xxim-server/common/pb"
minio "github.com/minio/minio-go/v7"
"github.com/minio/minio-go/v7/pkg/credentials"
Expand All @@ -14,6 +15,18 @@ type MinioStorage struct {
Client *minio.Client
}

func (s *MinioStorage) ExistObject(ctx context.Context, key string) (exists bool, err error) {
_, err = s.Client.StatObject(ctx, s.Config.BucketName, key, minio.StatObjectOptions{})
if err != nil {
e, ok := err.(minio.ErrorResponse)
if ok && e.Code == "NoSuchKey" {
return false, nil
}
return false, err
}
return true, nil
}

var singletonMinioStorage *MinioStorage

// NewMinioStorage 创建minio存储
Expand Down Expand Up @@ -47,3 +60,7 @@ func (s *MinioStorage) PutObject(ctx context.Context, objectName string, data []
}
return s.Config.BucketUrl + "/" + objectName, nil
}

func (s *MinioStorage) GetObjectUrl(key string) string {
return fmt.Sprintf("%s/%s", s.Config.BucketUrl, key)
}
9 changes: 9 additions & 0 deletions common/utils/xstorage/oss.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package xstorage

import (
"bytes"
"fmt"
"github.com/aliyun/aliyun-oss-go-sdk/oss"
"github.com/cherish-chat/xxim-server/common/pb"
"golang.org/x/net/context"
Expand All @@ -14,6 +15,10 @@ type OssStorage struct {
bucket *oss.Bucket
}

func (o *OssStorage) ExistObject(ctx context.Context, key string) (exist bool, err error) {
return o.bucket.IsObjectExist(key)
}

var singletonOssStorage *OssStorage

// NewOssStorage 创建OssStorage
Expand Down Expand Up @@ -47,3 +52,7 @@ func (o *OssStorage) PutObject(ctx context.Context, objectName string, data []by
}
return o.Config.BucketUrl + "/" + objectName, nil
}

func (o *OssStorage) GetObjectUrl(key string) string {
return fmt.Sprintf("%s/%s", o.Config.BucketUrl, key)
}

0 comments on commit 16956fe

Please sign in to comment.