googlecloud

package
v1.0.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 11, 2025 License: Apache-2.0 Imports: 11 Imported by: 0

README

Google Cloud Storage

Google Cloud Storage 的存储后端实现

使用方法

import "github.com/smart-unicom/oss/googlecloud"

func main() {
  storage := googlecloud.New(&googlecloud.Config{
    Bucket:           "your_bucket_name",
    ProjectID:        "your_project_id",
    CredentialsFile:  "/path/to/service-account.json", // 可选
    Endpoint:         "https://storage.googleapis.com", // 可选
  })

  // 保存文件到存储
  storage.Put("/sample.txt", reader)

  // 根据路径获取文件
  storage.Get("/sample.txt")

  // 获取文件流
  storage.GetStream("/sample.txt")

  // 删除文件
  storage.Delete("/sample.txt")

  // 列出指定路径下的所有对象
  storage.List("/")

  // 获取公共访问URL
  storage.GetURL("/sample.txt")
}

配置说明

  • Bucket: Google Cloud Storage存储桶名称
  • ProjectID: Google Cloud项目ID
  • CredentialsFile: 服务账户JSON密钥文件路径(可选)
  • Endpoint: 自定义端点(可选)

认证方式

1. 服务账户密钥文件
storage := googlecloud.New(&googlecloud.Config{
  Bucket:          "your_bucket_name",
  ProjectID:       "your_project_id",
  CredentialsFile: "/path/to/service-account.json",
})
2. 环境变量认证
export GOOGLE_APPLICATION_CREDENTIALS="/path/to/service-account.json"
3. 默认凭据(在GCP环境中)

在Google Cloud环境中运行时,会自动使用默认凭据。

环境变量配置

测试时可以通过以下环境变量配置:

export GOOGLE_CLOUD_PROJECT="your_project_id"
export GOOGLE_CLOUD_BUCKET="your_bucket_name"
export GOOGLE_APPLICATION_CREDENTIALS="/path/to/service-account.json"
export GOOGLE_CLOUD_ENDPOINT="https://storage.googleapis.com"

运行测试

go test ./googlecloud

注意事项

  • 确保服务账户具有相应的Cloud Storage权限
  • 存储桶名称必须全局唯一
  • 建议在生产环境中使用IAM角色而非密钥文件

Documentation

Overview

Package googlecloud Google Cloud存储服务实现 提供Google Cloud Storage的存储接口实现

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Client

type Client struct {
	// Config 客户端配置信息
	Config *Config
	// BucketHandle 存储桶句柄
	BucketHandle *storage.BucketHandle
}

Client Google Cloud存储客户端 封装Google Cloud Storage的操作接口

func New

func New(config *Config) (*Client, error)

New 初始化Google Cloud存储客户端 参数:

  • config: Google Cloud配置信息

返回:

  • *Client: Google Cloud存储客户端实例
  • error: 错误信息

func (Client) Delete

func (client Client) Delete(path string) error

Delete 删除指定路径的文件 参数:

  • path: 文件路径

返回:

  • error: 错误信息

func (Client) Get

func (client Client) Get(path string) (file *os.File, err error)

Get 获取指定路径的文件 参数:

  • path: 文件路径

返回:

  • *os.File: 文件对象
  • error: 错误信息

func (Client) GetEndpoint

func (client Client) GetEndpoint() string

GetEndpoint 获取存储服务的端点地址 返回:

  • string: 端点地址

func (Client) GetStream

func (client Client) GetStream(path string) (io.ReadCloser, error)

GetStream 获取指定路径文件的流 参数:

  • path: 文件路径

返回:

  • io.ReadCloser: 可读流
  • error: 错误信息

func (Client) GetURL

func (client Client) GetURL(path string) (url string, err error)

GetURL 获取指定路径文件的访问URL 参数:

  • path: 文件路径

返回:

  • string: 访问URL
  • error: 错误信息

func (Client) List

func (client Client) List(path string) ([]*oss.Object, error)

List 列出指定路径下的所有对象 参数:

  • path: 路径前缀

返回:

  • []*oss.Object: 对象列表
  • error: 错误信息

func (Client) Put

func (client Client) Put(urlPath string, reader io.Reader) (*oss.Object, error)

Put 上传文件到指定路径 参数:

  • urlPath: 目标路径
  • reader: 文件内容读取器

返回:

  • *oss.Object: 上传后的对象信息
  • error: 错误信息

func (Client) ToRelativePath

func (client Client) ToRelativePath(urlPath string) string

ToRelativePath 将路径转换为相对路径 参数:

  • urlPath: 原始路径

返回:

  • string: 相对路径

type Config

type Config struct {
	// ServiceAccountJson 服务账户JSON密钥
	ServiceAccountJson string
	// Bucket 存储桶名称
	Bucket string
	// Endpoint 服务端点
	Endpoint string
}

Config Google Cloud客户端配置 包含连接Google Cloud Storage所需的所有配置参数

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL