Skip to content

Commit

Permalink
init kodoblob
Browse files Browse the repository at this point in the history
  • Loading branch information
bachue committed Oct 16, 2023
1 parent 3b452be commit 434f383
Show file tree
Hide file tree
Showing 9 changed files with 1,642 additions and 2 deletions.
34 changes: 34 additions & 0 deletions .github/workflows/ci-test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
name: tests
on: [push, pull_request]
jobs:
build:
strategy:
fail-fast: false
matrix:
os: [ubuntu-latest, macos-latest]
go-version: [1.21.x]
include:
- go-version: 1.20.x
os: ubuntu-latest
runs-on: ${{ matrix.os }}
steps:
- name: Install Go
uses: actions/setup-go@v2
with:
go-version: ${{ matrix.go-version }}
- name: Checkout code
uses: actions/checkout@v2
with:
ref: ${{ github.ref }}
- name: Format
run: if [ "$(gofmt -s -l . | wc -l)" -gt 0 ]; then exit 1; fi
- name: Golint
run: |
set -e
go install honnef.co/go/tools/cmd/staticcheck@latest
staticcheck -go 1.20 ./...
- name: Run Tests
run: |
bash -e
go test -v ./...
shell: bash
202 changes: 200 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,200 @@
# go-cloud-dev-blob-qiniu-driver
Qiniu gocloud.dev blob driver
# Qiniu gocloud.dev blob driver

七牛 kodoblob 为 [gocloud.dev](https://gocloud.dev/) 提供了驱动,可以通过使用 [blob](https://gocloud.dev/blob) 包对七牛 Bucket 中的 Blob 进行读写,列举或删除。

## 代码案例

### 打开一个七牛 Bucket

```go
package main

import (
"context"
"fmt"
"os"

_ "github.com/bachue/go-cloud-dev-qiniu-driver/kodoblob"
"gocloud.dev/blob"
)

func main() {
bucket, err := blob.OpenBucket(context.Background(), "kodo://<Qiniu Access Key>:<Qiniu Secret Key>@<Qiniu Bucket Name>?useHttps")
if err != nil {
fmt.Fprintf(os.Stderr, "could not open bucket: %v\n", err)
os.Exit(1)
}
defer bucket.Close()

// 对 bucket 进行操作
}
```

这里的 URL 必须遵循以下格式

```
kodo://<Qiniu Access Key>:<Qiniu Secret Key>@<Qiniu Bucket Name>?<Options>
```

其中 `Options` 以 URL 查询的形式设置,支持以下选项:

| 名称 | 值类型 | 备注 |
|---|---|---|
| `useHttps` | 布尔值 | 是否使用 HTTPS 协议,默认不使用 |
| `downloadDomain` | 字符串列表 | 下载域名,如果不配置则使用默认源站域名,可以配置多个下载域名 |
| `signDownloadUrl` | 布尔值 | 是否对下载 URL 签名,对于私有空间来说,这是必须的,默认不签名 |
| `bucketHost` | 字符串列表 | 设置 Bucket 域名,可以配置多个 Bucket 域名,默认使用公有云 Bucket 域名 |
| `srcUpHost` | 字符串列表 | 设置上传源站域名,可以配置多个上传源站域名,默认通过 Bucket 域名查询获取 |
| `cdnUpHost` | 字符串列表 | 设置上传加速域名,可以配置多个上传加速域名,默认通过 Bucket 域名查询获取 |
| `rsHost` | 字符串列表 | 设置 RS 域名,可以配置多个 RS 域名,默认通过 Bucket 域名查询获取 |
| `rsfHost` | 字符串列表 | 设置 RSF 域名,可以配置多个 RSF 域名,默认通过 Bucket 域名查询获取 |
| `apiHost` | 字符串列表 | 设置 API 域名,可以配置多个 API 域名,默认通过 Bucket 域名查询获取 |

### 向七牛 Bucket 写入数据

```go
package main

import (
"context"
"fmt"
"os"

_ "github.com/bachue/go-cloud-dev-qiniu-driver/kodoblob"
"gocloud.dev/blob"
)

func main() {
bucket, err := blob.OpenBucket(context.Background(), "kodo://<Qiniu Access Key>:<Qiniu Secret Key>@<Qiniu Bucket Name>?useHttps")
if err != nil {
fmt.Fprintf(os.Stderr, "could not open bucket: %v\n", err)
os.Exit(1)
}
defer bucket.Close()

w, err := bucket.NewWriter(context.Background(), "<Key>", nil)
if err != nil {
fmt.Fprintf(os.Stderr, "could not open object for writing: %v\n", err)
os.Exit(1)
}
defer w.Close()

// 对 w 写入数据
}

```

### 从七牛 Bucket 读取数据

```go
package main

import (
"context"
"fmt"
"os"

_ "github.com/bachue/go-cloud-dev-qiniu-driver/kodoblob"
"gocloud.dev/blob"
)

func main() {
bucket, err := blob.OpenBucket(context.Background(), "kodo://<Qiniu Access Key>:<Qiniu Secret Key>@<Qiniu Bucket Name>?useHttps")
if err != nil {
fmt.Fprintf(os.Stderr, "could not open bucket: %v\n", err)
os.Exit(1)
}
defer bucket.Close()

r, err := bucket.NewReader(context.Background(), "<Key>", nil)
if err != nil {
fmt.Fprintf(os.Stderr, "could not open object for reading: %v\n", err)
os.Exit(1)
}
defer r.Close()

// 从 r 读取数据
}
```

### 从七牛 Bucket 读取范围数据

`gocloud.dev/blob` 支持读取指定偏移量的数据。

```go
package main

import (
"context"
"fmt"
"os"

_ "github.com/bachue/go-cloud-dev-qiniu-driver/kodoblob"
"gocloud.dev/blob"
)

func main() {
bucket, err := blob.OpenBucket(context.Background(), "kodo://<Qiniu Access Key>:<Qiniu Secret Key>@<Qiniu Bucket Name>?useHttps")
if err != nil {
fmt.Fprintf(os.Stderr, "could not open bucket: %v\n", err)
os.Exit(1)
}
defer bucket.Close()

r, err := bucket.NewRangeReader(ctx, "<Key>", 1024, 4096, nil)
if err != nil {
fmt.Fprintf(os.Stderr, "could not open object for reading: %v\n", err)
os.Exit(1)
}
defer r.Close()

// 从 r 读取数据
}
```

### 从七牛 Bucket 删除数据

```go
package main

import (
"context"
"fmt"
"os"

_ "github.com/bachue/go-cloud-dev-qiniu-driver/kodoblob"
"gocloud.dev/blob"
)

func main() {
bucket, err := blob.OpenBucket(context.Background(), "kodo://<Qiniu Access Key>:<Qiniu Secret Key>@<Qiniu Bucket Name>?useHttps")
if err != nil {
fmt.Fprintf(os.Stderr, "could not open bucket: %v\n", err)
os.Exit(1)
}
defer bucket.Close()

if err = bucket.Delete(context.Background(), "1G.65"); err != nil {
fmt.Fprintf(os.Stderr, "could not delete object: %v\n", err)
os.Exit(1)
}
}
```

## 贡献记录

- [所有贡献者](https://github.com/bachue/go-cloud-dev-qiniu-driver/contributors)

## 联系我们

- 如果需要帮助,请提交工单(在portal右侧点击咨询和建议提交工单,或者直接向 [email protected] 发送邮件)
- 如果有什么问题,可以到问答社区提问,[问答社区](http://qiniu.segmentfault.com/)
- 更详细的文档,见[官方文档站](http://developer.qiniu.com/)
- 如果发现了bug, 欢迎提交 [issue](https://github.com/bachue/go-cloud-dev-qiniu-driver/issues)
- 如果有功能需求,欢迎提交 [issue](https://github.com/bachue/go-cloud-dev-qiniu-driver/issues)
- 如果要提交代码,欢迎提交 [pull request](https://github.com/bachue/go-cloud-dev-qiniu-driver/pulls)
- 欢迎关注我们的[微信](http://www.qiniu.com/#weixin) [微博](http://weibo.com/qiniutek),及时获取动态信息。

## 代码许可

The Apache License v2.0. 详情见 [License 文件](https://github.com/bachue/go-cloud-dev-qiniu-driver/blob/master/LICENSE).
32 changes: 32 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
module github.com/bachue/go-cloud-dev-qiniu-driver

go 1.21

require (
github.com/onsi/ginkgo/v2 v2.12.0
github.com/onsi/gomega v1.27.10
github.com/qiniu/go-sdk/v7 v7.18.0
gocloud.dev v0.34.0
)

require (
github.com/go-logr/logr v1.2.4 // indirect
github.com/go-task/slim-sprig v0.0.0-20230315185526-52ccab3ef572 // indirect
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
github.com/golang/protobuf v1.5.3 // indirect
github.com/google/go-cmp v0.5.9 // indirect
github.com/google/pprof v0.0.0-20210407192527-94a9f03dee38 // indirect
github.com/googleapis/gax-go/v2 v2.12.0 // indirect
go.opencensus.io v0.24.0 // indirect
golang.org/x/net v0.14.0 // indirect
golang.org/x/sync v0.3.0 // indirect
golang.org/x/sys v0.11.0 // indirect
golang.org/x/text v0.12.0 // indirect
golang.org/x/tools v0.12.0 // indirect
golang.org/x/xerrors v0.0.0-20220907171357-04be3eba64a2 // indirect
google.golang.org/api v0.134.0 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20230731193218-e0aa005b6bdf // indirect
google.golang.org/grpc v1.57.0 // indirect
google.golang.org/protobuf v1.31.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
Loading

0 comments on commit 434f383

Please sign in to comment.