1\
This commit is contained in:
45
common/file_store/initialize.go
Normal file
45
common/file_store/initialize.go
Normal file
@ -0,0 +1,45 @@
|
||||
package file_store
|
||||
|
||||
import "fmt"
|
||||
|
||||
type OXS struct {
|
||||
// Endpoint 访问域名
|
||||
Endpoint string
|
||||
// AccessKeyID AK
|
||||
AccessKeyID string
|
||||
// AccessKeySecret AKS
|
||||
AccessKeySecret string
|
||||
// BucketName 桶名称
|
||||
BucketName string
|
||||
}
|
||||
|
||||
// Setup 配置文件存储driver
|
||||
func (e *OXS) Setup(driver DriverType, options ...ClientOption) FileStoreType {
|
||||
fileStoreType := driver
|
||||
var fileStore FileStoreType
|
||||
switch fileStoreType {
|
||||
case AliYunOSS:
|
||||
fileStore = new(ALiYunOSS)
|
||||
err := fileStore.Setup(e.Endpoint, e.AccessKeyID, e.AccessKeySecret, e.BucketName)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
}
|
||||
return fileStore
|
||||
case HuaweiOBS:
|
||||
fileStore = new(HuaWeiOBS)
|
||||
err := fileStore.Setup(e.Endpoint, e.AccessKeyID, e.AccessKeySecret, e.BucketName)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
}
|
||||
return fileStore
|
||||
case QiNiuKodo:
|
||||
fileStore = new(QiNiuKODO)
|
||||
err := fileStore.Setup(e.Endpoint, e.AccessKeyID, e.AccessKeySecret, e.BucketName)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
}
|
||||
return fileStore
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
27
common/file_store/interface.go
Normal file
27
common/file_store/interface.go
Normal file
@ -0,0 +1,27 @@
|
||||
package file_store
|
||||
|
||||
// DriverType 驱动类型
|
||||
type DriverType string
|
||||
|
||||
const (
|
||||
// HuaweiOBS 华为云OBS
|
||||
HuaweiOBS DriverType = "HuaweiOBS"
|
||||
// AliYunOSS 阿里云OSS
|
||||
AliYunOSS DriverType = "AliYunOSS"
|
||||
// QiNiuKodo 七牛云kodo
|
||||
QiNiuKodo DriverType = "QiNiuKodo"
|
||||
)
|
||||
|
||||
type ClientOption map[string]interface{}
|
||||
|
||||
// TODO: FileStoreType名称待定
|
||||
|
||||
// FileStoreType OXS
|
||||
type FileStoreType interface {
|
||||
// Setup 装载 endpoint sss
|
||||
Setup(endpoint, accessKeyID, accessKeySecret, BucketName string, options ...ClientOption) error
|
||||
// UpLoad 上传
|
||||
UpLoad(yourObjectName string, localFile interface{}) error
|
||||
// GetTempToken 获取临时Token
|
||||
GetTempToken() (string, error)
|
||||
}
|
||||
111
common/file_store/kodo.go
Normal file
111
common/file_store/kodo.go
Normal file
@ -0,0 +1,111 @@
|
||||
package file_store
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"github.com/qiniu/go-sdk/v7/auth/qbox"
|
||||
"github.com/qiniu/go-sdk/v7/storage"
|
||||
)
|
||||
|
||||
type Zone string
|
||||
|
||||
const (
|
||||
// HuaDong 华东
|
||||
HuaDong Zone = "HuaDong"
|
||||
// HuaBei 华北
|
||||
HuaBei Zone = "HuaBei"
|
||||
// HuaNan 华南
|
||||
HuaNan Zone = "HuaNan"
|
||||
// BeiMei 北美
|
||||
BeiMei Zone = "BeiMei"
|
||||
// XinJiaPo 新加坡
|
||||
XinJiaPo Zone = "XinJiaPo"
|
||||
)
|
||||
|
||||
type QiNiuKODO struct {
|
||||
Client interface{}
|
||||
BucketName string
|
||||
cfg storage.Config
|
||||
options []ClientOption
|
||||
}
|
||||
|
||||
func (e *QiNiuKODO) getToken() string {
|
||||
putPolicy := storage.PutPolicy{
|
||||
Scope: e.BucketName,
|
||||
}
|
||||
if len(e.options) > 0 && e.options[0]["Expires"] != nil {
|
||||
putPolicy.Expires = e.options[0]["Expires"].(uint64)
|
||||
}
|
||||
upToken := putPolicy.UploadToken(e.Client.(*qbox.Mac))
|
||||
return upToken
|
||||
}
|
||||
|
||||
//Setup 装载
|
||||
//endpoint sss
|
||||
func (e *QiNiuKODO) Setup(endpoint, accessKeyID, accessKeySecret, BucketName string, options ...ClientOption) error {
|
||||
|
||||
mac := qbox.NewMac(accessKeyID, accessKeySecret)
|
||||
// 获取存储空间。
|
||||
cfg := storage.Config{}
|
||||
// 空间对应的机房
|
||||
e.setZoneORDefault(cfg, options...)
|
||||
// 是否使用https域名
|
||||
cfg.UseHTTPS = true
|
||||
// 上传是否使用CDN上传加速
|
||||
cfg.UseCdnDomains = false
|
||||
|
||||
e.Client = mac
|
||||
e.BucketName = BucketName
|
||||
e.cfg = cfg
|
||||
e.options = options
|
||||
return nil
|
||||
}
|
||||
|
||||
// setZoneORDefault 设置Zone或者默认华东
|
||||
func (e *QiNiuKODO) setZoneORDefault(cfg storage.Config, options ...ClientOption) {
|
||||
if len(options) > 0 && options[0]["Zone"] != nil {
|
||||
if _, ok := options[0]["Zone"].(Zone); !ok {
|
||||
cfg.Zone = &storage.ZoneHuadong
|
||||
}
|
||||
switch options[0]["Zone"].(Zone) {
|
||||
case HuaDong:
|
||||
cfg.Zone = &storage.ZoneHuadong
|
||||
case HuaBei:
|
||||
cfg.Zone = &storage.ZoneHuabei
|
||||
case HuaNan:
|
||||
cfg.Zone = &storage.ZoneHuanan
|
||||
case BeiMei:
|
||||
cfg.Zone = &storage.ZoneBeimei
|
||||
case XinJiaPo:
|
||||
cfg.Zone = &storage.ZoneXinjiapo
|
||||
default:
|
||||
cfg.Zone = &storage.ZoneHuadong
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// UpLoad 文件上传
|
||||
func (e *QiNiuKODO) UpLoad(yourObjectName string, localFile interface{}) error {
|
||||
|
||||
// 构建表单上传的对象
|
||||
formUploader := storage.NewFormUploader(&e.cfg)
|
||||
ret := storage.PutRet{}
|
||||
// 可选配置
|
||||
putExtra := storage.PutExtra{
|
||||
Params: map[string]string{
|
||||
"x:name": "github logo",
|
||||
},
|
||||
}
|
||||
err := formUploader.PutFile(context.Background(), &ret, e.getToken(), yourObjectName, localFile.(string), &putExtra)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
return err
|
||||
}
|
||||
fmt.Println(ret.Key, ret.Hash)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (e *QiNiuKODO) GetTempToken() (string, error) {
|
||||
token := e.getToken()
|
||||
return token, nil
|
||||
}
|
||||
23
common/file_store/kodo_test.go
Normal file
23
common/file_store/kodo_test.go
Normal file
@ -0,0 +1,23 @@
|
||||
package file_store
|
||||
|
||||
import (
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestKODOUpload(t *testing.T) {
|
||||
e := OXS{"", "", "", ""}
|
||||
var oxs = e.Setup(QiNiuKodo, map[string]interface{}{"Zone": "华东"})
|
||||
err := oxs.UpLoad("test.png", "./test.png")
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
t.Log("ok")
|
||||
}
|
||||
|
||||
func TestKODOGetTempToken(t *testing.T) {
|
||||
e := OXS{"", "", "", ""}
|
||||
var oxs = e.Setup(QiNiuKodo, map[string]interface{}{"Zone": "华东"})
|
||||
token, _ := oxs.GetTempToken()
|
||||
t.Log(token)
|
||||
t.Log("ok")
|
||||
}
|
||||
52
common/file_store/obs.go
Normal file
52
common/file_store/obs.go
Normal file
@ -0,0 +1,52 @@
|
||||
package file_store
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/huaweicloud/huaweicloud-sdk-go-obs/obs"
|
||||
"log"
|
||||
)
|
||||
|
||||
type HuaWeiOBS struct {
|
||||
Client interface{}
|
||||
BucketName string
|
||||
}
|
||||
|
||||
func (e *HuaWeiOBS) Setup(endpoint, accessKeyID, accessKeySecret, BucketName string, options ...ClientOption) error {
|
||||
// 创建ObsClient结构体
|
||||
client, err := obs.New(accessKeyID, accessKeySecret, endpoint)
|
||||
if err != nil {
|
||||
log.Println("Error:", err)
|
||||
return err
|
||||
}
|
||||
e.Client = client
|
||||
e.BucketName = BucketName
|
||||
return nil
|
||||
}
|
||||
|
||||
// UpLoad 文件上传
|
||||
// yourObjectName 文件路径名称,与objectKey是同一概念,表示断点续传上传文件到OSS时需要指定包含文件后缀在内的完整路径,例如abc/efg/123.jpg
|
||||
func (e *HuaWeiOBS) UpLoad(yourObjectName string, localFile interface{}) error {
|
||||
// 获取存储空间。
|
||||
input := &obs.PutFileInput{}
|
||||
input.Bucket = e.BucketName
|
||||
input.Key = yourObjectName
|
||||
input.SourceFile = localFile.(string)
|
||||
output, err := e.Client.(*obs.ObsClient).PutFile(input)
|
||||
|
||||
if err == nil {
|
||||
fmt.Printf("RequestId:%s\n", output.RequestId)
|
||||
fmt.Printf("ETag:%s, StorageClass:%s\n", output.ETag, output.StorageClass)
|
||||
} else {
|
||||
if obsError, ok := err.(obs.ObsError); ok {
|
||||
fmt.Println(obsError.Code)
|
||||
fmt.Println(obsError.Message)
|
||||
} else {
|
||||
fmt.Println(err)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (e *HuaWeiOBS) GetTempToken() (string, error) {
|
||||
return "", nil
|
||||
}
|
||||
15
common/file_store/obs_test.go
Normal file
15
common/file_store/obs_test.go
Normal file
@ -0,0 +1,15 @@
|
||||
package file_store
|
||||
|
||||
import (
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestOBSUpload(t *testing.T) {
|
||||
e := OXS{"", "", "", ""}
|
||||
var oxs = e.Setup(HuaweiOBS)
|
||||
err := oxs.UpLoad("test.png", "./test.png")
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
t.Log("ok")
|
||||
}
|
||||
48
common/file_store/oss.go
Normal file
48
common/file_store/oss.go
Normal file
@ -0,0 +1,48 @@
|
||||
package file_store
|
||||
|
||||
import (
|
||||
"github.com/aliyun/aliyun-oss-go-sdk/oss"
|
||||
"log"
|
||||
)
|
||||
|
||||
type ALiYunOSS struct {
|
||||
Client interface{}
|
||||
BucketName string
|
||||
}
|
||||
|
||||
//Setup 装载
|
||||
//endpoint sss
|
||||
func (e *ALiYunOSS) Setup(endpoint, accessKeyID, accessKeySecret, BucketName string, options ...ClientOption) error {
|
||||
client, err := oss.New(endpoint, accessKeyID, accessKeySecret)
|
||||
if err != nil {
|
||||
log.Println("Error:", err)
|
||||
return err
|
||||
}
|
||||
e.Client = client
|
||||
e.BucketName = BucketName
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// UpLoad 文件上传
|
||||
func (e *ALiYunOSS) UpLoad(yourObjectName string, localFile interface{}) error {
|
||||
// 获取存储空间。
|
||||
bucket, err := e.Client.(*oss.Client).Bucket(e.BucketName)
|
||||
if err != nil {
|
||||
log.Println("Error:", err)
|
||||
return err
|
||||
}
|
||||
// 设置分片大小为100 KB,指定分片上传并发数为3,并开启断点续传上传。
|
||||
// 其中<yourObjectName>与objectKey是同一概念,表示断点续传上传文件到OSS时需要指定包含文件后缀在内的完整路径,例如abc/efg/123.jpg。
|
||||
// "LocalFile"为filePath,100*1024为partSize。
|
||||
err = bucket.UploadFile(yourObjectName, localFile.(string), 100*1024, oss.Routines(3), oss.Checkpoint(true, ""))
|
||||
if err != nil {
|
||||
log.Println("Error:", err)
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (e *ALiYunOSS) GetTempToken() (string, error) {
|
||||
return "", nil
|
||||
}
|
||||
16
common/file_store/oss_test.go
Normal file
16
common/file_store/oss_test.go
Normal file
@ -0,0 +1,16 @@
|
||||
package file_store
|
||||
|
||||
import (
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestOSSUpload(t *testing.T) {
|
||||
// 打括号内填写自己的测试信息即可
|
||||
e := OXS{}
|
||||
var oxs = e.Setup(AliYunOSS)
|
||||
err := oxs.UpLoad("test.png", "./test.png")
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
t.Log("ok")
|
||||
}
|
||||
Reference in New Issue
Block a user