Giter Site home page Giter Site logo

wuchunfu / goss Goto Github PK

View Code? Open in Web Editor NEW

This project forked from eleven26/goss

0.0 1.0 1.0 280 KB

一个简洁的云存储 golang 库,支持阿里云、腾讯云、七牛云、华为云、aws s3。

Home Page: https://blog.baiguiren.com/goss/

License: MIT License

Go 96.09% Makefile 3.91%

goss's Introduction

goss

goss 是一个简洁的云存储 golang 库,兼容 amazon s3 协议,支持但不限于阿里云腾讯云七牛云华为云aws s3minio

Go Reference Go Report Card Go codecov GitHub license GitHub go.mod Go version GitHub release (latest by date)

🚀 安装

go get -u github.com/eleven26/goss/v4

⚙️ 配置

所有支持的配置项:

type Config struct {
    Endpoint          string `yaml:"endpoint"`
    AccessKey         string `yaml:"access_key"`
    SecretKey         string `yaml:"secret_key"`
    Region            string `yaml:"region"`
    Bucket            string `yaml:"bucket"`

    // 如果是使用 minio,并且没有使用 https,需要设置为 true
    UseSsl            *bool  `yaml:"use_ssl"`
    // 如果是使用 minio,需要设置为 true
    HostnameImmutable *bool  `yaml:"hostname_immutable"`
}

配置的方式,在创建实例的时候通过 WithConfig 来传递:

goss, err := goss.New(goss.WithConfig(&goss.Config{
    Endpoint: "",
    AccessKey: "",
    SecretKey: "",
    Region: "",
    Bucket: "",
}))

💡 基本用法

  1. 你可以通过下面的代码来导入 goss:
import "github.com/eleven26/goss/v4"
  1. 使用之前需要创建实例:
goss, err := goss.New(goss.WithConfig(&Config{
    Endpoint: "",
    AccessKey: "",
    SecretKey: "",
    Region: "",
    Bucket: "",
}))
  1. 使用
// goss.GetString 会获取路径指定的文件,返回字符串
fmt.Println(goss.GetString(context.TODO(), "test/foo.txt"))

📚 接口

goss 支持以下操作:

Put

上传文件到云存储。第一个参数是 key,第二个参数是 io.Reader

data := []byte("this is some data stored as a byte slice in Go Lang!")
r := bytes.NewReader(data)
err := goss.Put(context.TODO(), "test/test.txt", r)

PutFromFile

上传文件到云存储。第一个参数是 key,第二个参数是本地文件路径。

err := goss.PutFromFile(context.TODO(), "test/test.txt", "/path/to/test.txt")

Get

从云存储获取文件。参数是 key。返回值是 io.ReadClosererror

// rc 是 `io.ReadCloser`
rc, err := goss.Get(context.TODO(), "test/test.txt")
defer rc.Close()

bs, err := io.ReadAll(rc)
fmt.Println(string(bs))

GetString

从云存储获取文件。参数是 key。返回值是 stringerror

content, err := goss.GetString(context.TODO(), "test/test.txt")
fmt.Println(content)

GetBytes

从云存储获取文件。参数是 key。返回值是 []byteerror

bs, err := goss.GetBytes(context.TODO(), "test/test.txt")
fmt.Println(string(bs))

GetToFile

下载云存储文件到本地。第一个参数是 key,第二个参数是本地路径。

// 第一个参数是云端路径,第二个参数是本地路径
err := goss.GetToFile(context.TODO(), "test/test.txt", "/path/to/local")

Delete

删除云存储文件。

err := goss.Delete(context.TODO(), "test/test.txt")

Exists

判断云存储文件是否存在。

exists, err := goss.Exists(context.TODO(), "test/test.txt")

Files

根据前缀获取文件列表。

exists, err := goss.Files(context.TODO(), "test/")

Size

获取云存储文件大小。

size, err := goss.Size(context.TODO(), "test/test.txt")

参考文档

  1. 阿里云对象存储
  2. 腾讯云对象存储
  3. 七牛云对象存储
  4. 华为云对象存储
  5. aws s3
  6. minio

各云厂商对 s3 的支持

  1. 阿里云: OSS与Amazon S3的兼容性
  2. 腾讯云: 使用 AWS S3 SDK 访问 COS
  3. 七牛云: AWS S3 兼容
  4. 华为云:支持 s3,但是官网文档找不到相关关于 s3 兼容的相关描述
  5. minio: AWS S3 Compatibility

注意事项

  1. 七牛云的 endpointregion 配置请参考这个文档:AWS S3 兼容 - 服务域名

Changelog

[4.0.0] - 2023-09-28

Changed

  • 所有方法添加 context.Context 作为第一个参数。

goss's People

Contributors

eleven26 avatar guonaihong avatar exuan avatar

Watchers

 avatar

Forkers

xiaodela

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.