对象存储

  • 对象存储 > 最佳实践 > 对象存储开源生态建设 > 使用 Go CDK 对接对象存储 Kodo >使用七牛 Go CDK Driver 对接对象存储 Kodo

    使用七牛 Go CDK Driver 对接对象存储 Kodo

    最近更新时间: 2024-01-12 15:26:53

    七牛 Go Cloud Development Kit (Go CDK) Driver 为 gocloud.dev 提供了 kodoblob 驱动,可以通过使用 blob 包对七牛 Bucket 中的 Blob 进行读写,列举或删除。

    代码案例

    打开一个七牛 Bucket

    package main
    
    import (
    	"context"
    	"fmt"
    	"os"
    
    	_ "github.com/qiniu/go-cdk-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 写入数据

    package main
    
    import (
    	"context"
    	"fmt"
    	"os"
    
    	_ "github.com/qiniu/go-cdk-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 读取数据

    package main
    
    import (
    	"context"
    	"fmt"
    	"os"
    
    	_ "github.com/qiniu/go-cdk-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 支持读取指定偏移量的数据。

    package main
    
    import (
    	"context"
    	"fmt"
    	"os"
    
    	_ "github.com/qiniu/go-cdk-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 删除数据

    package main
    
    import (
    	"context"
    	"fmt"
    	"os"
    
    	_ "github.com/qiniu/go-cdk-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)
    	}
    }
    
    以上内容是否对您有帮助?
  • Qvm free helper
    Close