Skip to content

Commit

Permalink
还原对七牛云的支持
Browse files Browse the repository at this point in the history
  • Loading branch information
eleven26 committed Mar 28, 2023
1 parent b293c3e commit 466b3da
Show file tree
Hide file tree
Showing 15 changed files with 594 additions and 9 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ integration:
go test -v ./drivers/aliyun -cover -tags=integration
go test -v ./drivers/huawei -cover -tags=integration
go test -v ./drivers/tencent -cover -tags=integration
go test -v ./drivers/qiniu -cover -tags=integration_qiniu
go test -v ./drivers/minio -cover -tags=integration
go test -v ./drivers/s3 -cover -tags=integration
go test -v ./goss/* -cover -tags=integration
Expand All @@ -32,4 +33,3 @@ all:
make check
make format
make test
make integration
22 changes: 17 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# goss

`goss` 是一个简洁的云存储 golang 库,支持**阿里云****腾讯云****华为云****aws s3****minio**
`goss` 是一个简洁的云存储 golang 库,支持**阿里云****腾讯云****七牛云****华为云****aws s3****minio**

[![Go Reference](https://pkg.go.dev/badge/github.com/eleven26/go-filesystem.svg)](https://pkg.go.dev/github.com/eleven26/goss)
[![Go Report Card](https://goreportcard.com/badge/github.com/eleven26/go-filesystem)](https://goreportcard.com/report/github.com/eleven26/goss)
Expand All @@ -24,7 +24,7 @@ go get -u github.com/eleven26/goss

```yaml
# 云存储类型
# 可选值为: aliyun、tencent、huawei、s3、minio
# 可选值为: aliyun、tencent、qiniu、huawei、s3、minio
driver: aliyun

# 阿里云 oss 配置
Expand All @@ -43,6 +43,17 @@ tencent:
secret_id:
secret_key:

# 七牛云 kodo 配置
qiniu:
# bucket 名称
bucket:
access_key:
secret_key:
# bucket 外链域名
domain:
# 是否是私有空间
private:

# 华为云 obs 配置
huawei:
endpoint:
Expand Down Expand Up @@ -228,6 +239,7 @@ size, err := storage.Size("test/test.txt")

1. [阿里云对象存储](https://help.aliyun.com/product/31815.html)
2. [腾讯云对象存储](https://cloud.tencent.com/document/product/436)
3. [华为云对象存储](https://support.huaweicloud.com/obs/index.html)
4. [aws s3](https://docs.aws.amazon.com/sdk-for-go/api/service/s3/)
5. [minio](https://github.com/minio/minio)
3. [七牛云对象存储](https://developer.qiniu.com/kodo)
4. [华为云对象存储](https://support.huaweicloud.com/obs/index.html)
5. [aws s3](https://docs.aws.amazon.com/sdk-for-go/api/service/s3/)
6. [minio](https://github.com/minio/minio)
6 changes: 5 additions & 1 deletion drivers/minio/minio_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,12 @@ func TestPut(t *testing.T) {
err = storage2.Put(key, f)
assert.Nil(t, err)

_, err = store.getObject(key)
obj, err := store.getObject(key)
assert.Nil(t, err)

bs, err := io.ReadAll(obj)
assert.Nil(t, err)
assert.Equal(t, "foo", string(bs))
}

func TestPutFromFile(t *testing.T) {
Expand Down
35 changes: 35 additions & 0 deletions drivers/qiniu/chunks.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package qiniu

import (
"github.com/eleven26/goss/core"
"github.com/qiniu/go-sdk/v7/storage"
)

type Chunks struct {
bucket string
prefix string
bucketManager *storage.BucketManager

nextMarker string
}

func NewChunks(bucket string, prefix string, bucketManager *storage.BucketManager) core.Chunks {
return &Chunks{
bucket: bucket,
prefix: prefix,
bucketManager: bucketManager,
}
}

func (c *Chunks) Chunk() (*core.ListObjectResult, error) {
// 参考文档:https://developer.qiniu.com/kodo/1284/list
// ListFiles 最后一个参数 limit 为单次列举的条目数,范围为1-1000。 默认值为1000。
entries, _, nextMarker, hasNext, err := c.bucketManager.ListFiles(c.bucket, c.prefix, "", c.nextMarker, 100)
if err != nil {
return nil, err
}

c.nextMarker = nextMarker

return NewListObjectResult(entries, hasNext), nil
}
23 changes: 23 additions & 0 deletions drivers/qiniu/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package qiniu

import "github.com/spf13/viper"

type config struct {
Bucket string
AccessKey string
SecretKey string
Zone string
Domain string
Private bool
}

func getConfig(viper *viper.Viper) *config {
return &config{
Bucket: viper.GetString("qiniu.bucket"),
AccessKey: viper.GetString("qiniu.access_key"),
SecretKey: viper.GetString("qiniu.secret_key"),
Zone: viper.GetString("qiniu.zone"),
Domain: viper.GetString("qiniu.domain"),
Private: viper.GetBool("qiniu.private"),
}
}
46 changes: 46 additions & 0 deletions drivers/qiniu/driver.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package qiniu

import (
"github.com/eleven26/goss/core"
"github.com/qiniu/go-sdk/v7/auth/qbox"
"github.com/qiniu/go-sdk/v7/storage"
"github.com/spf13/viper"
)

type Driver struct {
Viper *viper.Viper
}

func NewDriver(opts ...core.Option) core.Driver {
driver := &Driver{}

for _, option := range opts {
option(driver)
}

return driver
}

func (d *Driver) Storage() (core.Storage, error) {
conf := getConfig(d.Viper)

if conf.Bucket == "" || conf.AccessKey == "" || conf.SecretKey == "" {
return nil, core.ErrorConfigEmpty
}

mac := qbox.NewMac(conf.AccessKey, conf.SecretKey)
cfg := storage.Config{
UseHTTPS: true,
}

store := Store{
config: *conf,
bucketManager: storage.NewBucketManager(mac, &cfg),
}

return core.NewStorage(&store), nil
}

func (d Driver) Name() string {
return "qiniu"
}
32 changes: 32 additions & 0 deletions drivers/qiniu/file.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package qiniu

import (
"strconv"
"time"

"github.com/qiniu/go-sdk/v7/storage"
)

type File struct {
item storage.ListItem
}

func (f *File) Key() string {
return f.item.Key
}

func (f *File) Type() string {
return strconv.Itoa(f.item.Type)
}

func (f *File) Size() int64 {
return f.item.Fsize
}

func (f *File) ETag() string {
return f.item.Hash
}

func (f *File) LastModified() time.Time {
return time.UnixMicro(f.item.PutTime / 10)
}
Loading

0 comments on commit 466b3da

Please sign in to comment.