Documentation
¶
Overview ¶
Package remote 提供多种远程存储方式的统一接口。
Index ¶
- Constants
- Variables
- type COSClient
- func (c *COSClient) Close() error
- func (c *COSClient) Delete(ctx context.Context, remotePath string) error
- func (c *COSClient) Download(ctx context.Context, remotePath string) ([]byte, error)
- func (c *COSClient) List(ctx context.Context, options ListOptions) (ListResult, error)
- func (c *COSClient) Upload(ctx context.Context, remotePath string, data []byte) error
- type COSConfig
- type Client
- type Error
- type FTPClient
- func (c *FTPClient) Close() error
- func (c *FTPClient) Delete(ctx context.Context, remotePath string) error
- func (c *FTPClient) Download(ctx context.Context, remotePath string) ([]byte, error)
- func (c *FTPClient) List(ctx context.Context, options ListOptions) (ListResult, error)
- func (c *FTPClient) Upload(ctx context.Context, remotePath string, data []byte) error
- type FTPConfig
- type FileInfo
- type ListOptions
- type ListResult
- type OSSClient
- func (c *OSSClient) Close() error
- func (c *OSSClient) Delete(ctx context.Context, remotePath string) error
- func (c *OSSClient) Download(ctx context.Context, remotePath string) ([]byte, error)
- func (c *OSSClient) List(ctx context.Context, options ListOptions) (ListResult, error)
- func (c *OSSClient) PresignUpload(ctx context.Context, remotePath string, opts *PresignOptions) (string, map[string]string, error)
- func (c *OSSClient) Upload(ctx context.Context, remotePath string, data []byte) error
- type OSSConfig
- type PresignOptions
- type PresignUploader
- type SFTPClient
- func (c *SFTPClient) Close() error
- func (c *SFTPClient) Delete(ctx context.Context, remotePath string) error
- func (c *SFTPClient) Download(ctx context.Context, remotePath string) ([]byte, error)
- func (c *SFTPClient) List(ctx context.Context, options ListOptions) (ListResult, error)
- func (c *SFTPClient) Upload(ctx context.Context, remotePath string, data []byte) error
- type SFTPConfig
Constants ¶
const ( TypeFTP = "ftp" // FTP存储类型 TypeSFTP = "sftp" // SFTP存储类型 TypeCOS = "cos" // 腾讯云COS存储类型 TypeOSS = "oss" // 阿里云OSS存储类型 )
存储类型常量定义
Variables ¶
var ErrPresignNotSupported = errors.New("current storage type does not support presigned upload")
ErrPresignNotSupported 表示当前存储类型不支持预签名上传。
Functions ¶
This section is empty.
Types ¶
type COSClient ¶
type COSClient struct {
// contains filtered or unexported fields
}
COSClient 实现了腾讯云COS存储的客户端。 注意:COSClient是并发安全的,因为底层COS SDK客户端本身是并发安全的。
TODO: 实现 remote.PresignUploader 接口(PresignUpload 方法)以支持预签名上传; 实现后 Attachment.GetPresignURL 将自动放行 cos,无需修改其逻辑。
func NewCOSClient ¶
NewCOSClient 创建一个新的COS客户端。
func (*COSClient) List ¶
func (c *COSClient) List(ctx context.Context, options ListOptions) (ListResult, error)
List 实现了Client接口的List方法。
type Client ¶
type Client interface {
Upload(ctx context.Context, remotePath string, data []byte) error
Download(ctx context.Context, remotePath string) ([]byte, error)
Delete(ctx context.Context, remotePath string) error
List(ctx context.Context, options ListOptions) (ListResult, error)
Close() error
}
Client 定义了远程存储的统一接口。 注意:所有方法都应是并发安全的。
type FTPClient ¶
type FTPClient struct {
// contains filtered or unexported fields
}
FTPClient 实现了FTP存储的客户端。 注意:FTPClient是并发安全的。
func (*FTPClient) List ¶
func (c *FTPClient) List(ctx context.Context, options ListOptions) (ListResult, error)
List 实现了Client接口的List方法。 注意:FTP 的 Prefix 表示待列出内容的目录路径,非对象前缀。 列出前会检测/恢复连接,返回结果按文件名排序后做本地分页。
type FileInfo ¶
type FileInfo struct {
Name string // 文件名
Size int64 // 文件大小(字节)
ModTime time.Time // 修改时间
IsDir bool // 是否为目录
}
FileInfo 定义了文件的元数据信息
type ListOptions ¶
type ListOptions struct {
Prefix string // 文件名前缀过滤或目录路径,可选
Marker string // 分页标记,可选
MaxKeys int // 每次返回的最大文件数量,可选
}
ListOptions 定义了分页和过滤选项 注意:不同存储类型对 Prefix 的语义不同:
- COS/OSS:对象键前缀过滤
- FTP/SFTP:待列出内容的目录路径
type ListResult ¶
type ListResult struct {
Files []FileInfo // 文件信息列表
NextMarker string // 下一个分页标记
IsTruncated bool // 是否还有更多数据
}
ListResult 定义了List方法的返回结果
type OSSClient ¶
type OSSClient struct {
// contains filtered or unexported fields
}
OSSClient 实现了阿里云OSS存储的客户端。 注意:OSSClient是并发安全的,因为底层OSS SDK客户端本身是并发安全的。
func NewOSSClient ¶
NewOSSClient 创建一个新的OSS客户端。
func (*OSSClient) List ¶
func (c *OSSClient) List(ctx context.Context, options ListOptions) (ListResult, error)
List 实现了Client接口的List方法。
func (*OSSClient) PresignUpload ¶ added in v0.13.24
func (c *OSSClient) PresignUpload(ctx context.Context, remotePath string, opts *PresignOptions) (string, map[string]string, error)
PresignUpload 生成用于上传指定对象的预签名 URL。 - remotePath: 对象在存储桶中的路径(Key)。 - opts: 预签名选项,nil 时表示使用默认选项(仅签名 bucket 与 key,默认有效期 10 分钟)。 返回值:
- string: 预签名 URL。
- map[string]string: 需要在上传请求中携带的已签名请求头。
- error: 生成过程中出现的错误。
type PresignOptions ¶ added in v0.13.24
type PresignOptions struct {
Expires time.Duration // 预签名 URL 的有效期,为零时使用存储类型的默认有效期
ContentType string // 上传文件的 Content-Type,为空时不参与签名
Metadata map[string]string // 自定义元数据,为空时不参与签名
}
PresignOptions 定义了生成预签名 URL 的可选参数。 不同存储类型对字段的支持程度可能不同,不支持的字段会被忽略。
type PresignUploader ¶ added in v0.13.24
type PresignUploader interface {
// PresignUpload 生成用于上传指定对象的预签名 URL。
// - remotePath: 对象在存储桶中的路径(Key)。
// - opts: 预签名选项,nil 时表示使用默认选项。
// 返回值:
// - string: 预签名 URL。
// - map[string]string: 需要在上传请求中携带的已签名请求头。
// - error: 生成过程中出现的错误。
PresignUpload(ctx context.Context, remotePath string, opts *PresignOptions) (string, map[string]string, error)
}
PresignUploader 定义了支持预签名上传的存储类型需要实现的接口。 注意:该接口是可选的,FTP/SFTP 等不支持预签名 URL 的存储类型无需实现。
type SFTPClient ¶
type SFTPClient struct {
// contains filtered or unexported fields
}
SFTPClient 实现了SFTP存储的客户端。 注意:SFTPClient是并发安全的。
func NewSFTPClient ¶
func NewSFTPClient(config SFTPConfig, args ...ssh.HostKeyCallback) (*SFTPClient, error)
NewSFTPClient 创建一个新的SFTP客户端。 - config SFTP 连接配置 - args 可选的主机密钥回调,未提供时使用 ssh.InsecureIgnoreHostKey 返回值:
- 创建成功返回 *SFTPClient
- 地址格式错误、认证信息缺失或连接失败时返回错误
func (*SFTPClient) Close ¶
func (c *SFTPClient) Close() error
Close 实现了Client接口的Close方法。 关闭 SFTP 客户端和底层 SSH 连接,并将内部引用置空,标记客户端为已关闭状态。
func (*SFTPClient) Delete ¶
func (c *SFTPClient) Delete(ctx context.Context, remotePath string) error
Delete 实现了Client接口的Delete方法。 删除前会检测/恢复连接。
func (*SFTPClient) List ¶
func (c *SFTPClient) List(ctx context.Context, options ListOptions) (ListResult, error)
List 实现了Client接口的List方法。 注意:SFTP 的 Prefix 表示待列出内容的目录路径,非对象前缀。 列出前会检测/恢复连接,返回结果按文件名排序后做本地分页。