storage

package
v1.40.0 Latest Latest
Warning

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

Go to latest
Published: Aug 16, 2026 License: MIT Imports: 7 Imported by: 0

Documentation

Overview

Package storage 提供对象存储集成(对标 Spring Cloud 的 OSS/对象存储抽象, 兼容 S3 协议的 MinIO/阿里云 OSS/腾讯云 COS 等):常用操作封装 + 原生客户端暴露。

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func SetGlobal added in v1.36.0

func SetGlobal(client *Client)

SetGlobal 设置全局客户端(NewClient 成功后自动调用)。

Types

type Client

type Client struct {
	// contains filtered or unexported fields
}

Client 是对象存储模板实现。

func Get added in v1.36.0

func Get() *Client

Get 获取全局对象存储客户端;未初始化时返回 nil。

func NewClient

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

NewClient 创建对象存储客户端(兼容 S3 协议)。 Endpoint 为空时返回错误。

func (*Client) Client

func (c *Client) Client() *minio.Client

Client 返回原生 MinIO 客户端。

func (*Client) Delete

func (c *Client) Delete(ctx context.Context, bucket, objectName string) error

Delete 删除对象。

func (*Client) EnsureBucket

func (c *Client) EnsureBucket(ctx context.Context, bucket string) error

EnsureBucket 确保桶存在。

func (*Client) Get

func (c *Client) Get(ctx context.Context, bucket, objectName string) (io.ReadCloser, *ObjectInfo, error)

Get 下载对象。

func (*Client) GetBytes

func (c *Client) GetBytes(ctx context.Context, bucket, objectName string) ([]byte, error)

GetBytes 下载为字节(小文件)。

func (*Client) List

func (c *Client) List(ctx context.Context, bucket, prefix string, limit int) ([]ObjectInfo, error)

List 列举对象。

func (*Client) Ping

func (c *Client) Ping(ctx context.Context) error

Ping 连通性检查。

func (*Client) PresignedGet

func (c *Client) PresignedGet(ctx context.Context, bucket, objectName string, ttl time.Duration) (string, error)

PresignedGet 预签名下载 URL。

func (*Client) PresignedPut

func (c *Client) PresignedPut(ctx context.Context, bucket, objectName string, ttl time.Duration) (string, error)

PresignedPut 预签名上传 URL。

func (*Client) Put

func (c *Client) Put(ctx context.Context, bucket, objectName string, reader io.Reader, size int64, contentType string) error

Put 上传对象。

func (*Client) PutBytes

func (c *Client) PutBytes(ctx context.Context, bucket, objectName string, data []byte, contentType string) error

PutBytes 上传字节数据。

func (*Client) Stat

func (c *Client) Stat(ctx context.Context, bucket, objectName string) (*ObjectInfo, error)

Stat 对象信息。

type Config

type Config struct {
	// Endpoint 服务地址(如 127.0.0.1:9000 或 oss-cn-hangzhou.aliyuncs.com)。
	Endpoint string
	// AccessKey/SecretKey 访问密钥。
	AccessKey string
	SecretKey string
	// UseSSL 是否启用 HTTPS。
	UseSSL bool
	// Region 区域(OSS 必需,如 oss-cn-hangzhou)。
	Region string
	// Bucket 默认桶(可空,操作时显式指定)。
	Bucket string
}

Config 对象存储配置(兼容 S3 协议:MinIO/OSS/COS 等)。

type ObjectInfo

type ObjectInfo struct {
	Key          string    `json:"key"`
	Size         int64     `json:"size"`
	ContentType  string    `json:"content_type"`
	LastModified time.Time `json:"last_modified"`
	ETag         string    `json:"etag"`
}

ObjectInfo 对象信息。

type StorageTemplate

type StorageTemplate interface {
	// Ping 连通性检查(列举默认桶或根)。
	Ping(ctx context.Context) error
	// EnsureBucket 确保桶存在(不存在则创建)。
	EnsureBucket(ctx context.Context, bucket string) error
	// Put 上传对象(reader 流式上传)。
	Put(ctx context.Context, bucket, objectName string, reader io.Reader, size int64, contentType string) error
	// PutBytes 上传字节数据。
	PutBytes(ctx context.Context, bucket, objectName string, data []byte, contentType string) error
	// Get 下载对象(返回读取流,调用方负责 Close)。
	Get(ctx context.Context, bucket, objectName string) (io.ReadCloser, *ObjectInfo, error)
	// GetBytes 下载对象为字节(小文件场景)。
	GetBytes(ctx context.Context, bucket, objectName string) ([]byte, error)
	// Delete 删除对象(不存在不报错)。
	Delete(ctx context.Context, bucket, objectName string) error
	// Stat 对象信息(不存在返回错误)。
	Stat(ctx context.Context, bucket, objectName string) (*ObjectInfo, error)
	// List 列举前缀下的对象(最多 limit 个,limit<=0 时全部)。
	List(ctx context.Context, bucket, prefix string, limit int) ([]ObjectInfo, error)
	// PresignedPut 生成预签名上传 URL(临时直传,防暴露密钥)。
	PresignedPut(ctx context.Context, bucket, objectName string, ttl time.Duration) (string, error)
	// PresignedGet 生成预签名下载 URL。
	PresignedGet(ctx context.Context, bucket, objectName string, ttl time.Duration) (string, error)
	// Client 返回原生 MinIO 客户端(高级操作入口)。
	Client() *minio.Client
}

StorageTemplate 高频操作层:上传/下载/删除/列举/签名 + 原生客户端。

Jump to

Keyboard shortcuts

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