storage

package
v1.0.21 Latest Latest
Warning

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

Go to latest
Published: Oct 16, 2026 License: MIT Imports: 25 Imported by: 1

README

Storage Package

The storage package provides a unified filesystem abstraction based on afero.Fs, with support for local filesystem, OSS, and SFTP backends.

Features

  • Multi-Backend Support: Local FS, OSS, SFTP
  • Afero Integration: Unified read/write access through afero.Fs
  • Per-Key Factory Model: Build filesystem instances and public URL functions by storage key
  • Public URL Support: Generate public URLs for OSS or resource URLs for FS-based backends
  • Directory Management: Ensure directory existence through EnsureDir

Main APIs

Filesystem
  • CreateFs(key): Create a filesystem instance and its release function
  • EnsureDir(fs, dir): Create directory if it does not exist
Public URL
  • GetPublicURLFunc(key): Get a PublicURLFunc bound to the given storage key
  • GetPublicURLFuncDefault(): Get a PublicURLFunc bound to default key fileroot
  • CreatePublicURL(key, fullFileName): Generate a public URL with an explicit key
  • CreatePublicURLDefault(fullFileName): Generate a public URL with default key fileroot
  • CreatePublicURLWithFunc(fn, fullFileName): Generate a public URL with a previously resolved function
Backends
  • Local / SFTP: Return a resource URL such as /v1/resources/{xid} and serve file content through the built-in Gin route
  • OSS: Return the direct public object URL based on bucket + endpoint + fullFileName

Public Resource Route

For FS-based backends, the package registers a public resource route automatically:

  • Route: /v1/resources/:xid
  • Behavior: resolve xid to the original file path and stream file content through afero.Fs
  • Expiration: xid is cached for 24h
  • Failure: expired xid or missing file returns 404

If domain is configured, generated resource URLs will include the full domain; otherwise they will return a relative URI.

Default Key

Unless otherwise specified, the package uses the default storage key:

const defaultStorageKey = "fileroot"

If key is empty or not configured in Viper, APIs such as CreateFs and GetPublicURLFunc will fall back to fileroot.

Usage

Create Filesystem
fs, release, err := storage.CreateFs("fileroot")
if err != nil {
	return err
}
defer release()

if err := storage.EnsureDir(fs, "owner-a/blockedEpc/202607"); err != nil {
	return err
}

if err := afero.WriteFile(fs, "owner-a/blockedEpc/202607/epc.png", data, 0o644); err != nil {
	return err
}
Create Public URL With Explicit Key
publicURL, err := storage.CreatePublicURL("fileroot", "/data/upload/owner-a/blockedEpc/202607/epc.png")
if err != nil {
	return err
}
Create Public URL With Default Key
publicURL, err := storage.CreatePublicURLDefault("/data/upload/owner-a/blockedEpc/202607/epc.png")
if err != nil {
	return err
}
Reuse PublicURLFunc
fn, err := storage.GetPublicURLFunc("fileroot")
if err != nil {
	return err
}

publicURL, err := storage.CreatePublicURLWithFunc(fn, "/data/upload/owner-a/blockedEpc/202607/epc.png")
if err != nil {
	return err
}

Configuration

Configure storage backends via Viper.

Local
fileroot:
  type: local
  path: /data/upload
OSS
fileroot:
  type: oss
  endpoint: https://oss-cn-hangzhou.aliyuncs.com
  accessKey: your-access-key
  secretKey: your-secret-key
  bucket: my-bucket
  region: cn-hangzhou
  path: upload
SFTP
fileroot:
  type: sftp
  endpoint: sftp.example.com:22
  account: demo
  password: your-password
  path: /data/upload
Optional Base URL
baseUri: /v1
domain: https://example.com

Notes

  • The package no longer keeps a global filesystem cache; CreateFs returns a fresh instance on each call
  • For OSS, public URL generation is direct and does not go through /v1/resources/:xid
  • For FS-based backends, fullFileName should match the actual file path that can be resolved under the configured root

Dependencies

  • Afero for filesystem abstraction
  • FS-OSS for OSS integration
  • Viper for configuration
  • Zap for logging
  • Gin for public resource routing

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	FSFactories            = map[string]FSFactory{}
	PublicURLFuncFactories = map[string]PublicURLFuncFactory{}
)

Functions

func CreateDefaultPublicURL added in v1.0.10

func CreateDefaultPublicURL(fullFileName string) (string, error)

CreateDefaultPublicURL 使用默认存储键生成公共访问 URL。 Deprecated: 请使用 CreatePublicURLDefault。 fullFileName: 完整文件名或完整文件路径。 返回值:返回默认 `fileroot` 配置下的公共访问 URL 和错误信息。

func CreatePublicURL added in v1.0.10

func CreatePublicURL(key string, fullFileName string) (string, error)

CreatePublicURL 根据完整文件名生成可公开访问的 URL。 key: 存储配置键。 fullFileName: 完整文件名或完整文件路径。 返回值:返回公共访问 URL 和错误信息。

func CreatePublicURLDefault added in v1.0.10

func CreatePublicURLDefault(fullFileName string) (string, error)

CreatePublicURLDefault 使用默认存储键生成公共访问 URL。 fullFileName: 完整文件名或完整文件路径。 返回值:返回默认 `fileroot` 配置下的公共访问 URL 和错误信息。

func CreatePublicURLWithFunc added in v1.0.10

func CreatePublicURLWithFunc(publicURLFunc PublicURLFunc, fullFileName string) (string, error)

CreatePublicURLWithFunc 使用公共 URL 生成函数处理完整文件名。 publicURLFunc: 已根据 key 解析得到的公共 URL 生成函数。 fullFileName: 完整文件名或完整文件路径。 返回值:返回公共访问 URL 和错误信息。

func CreatePublicURLWithInit added in v1.0.10

func CreatePublicURLWithInit(publicURLFunc PublicURLFunc, fullFileName string) (string, error)

CreatePublicURLWithInit 使用公共 URL 生成函数处理完整文件名。 Deprecated: 请使用 CreatePublicURLWithFunc。 publicURLFunc: 已根据 key 解析得到的公共 URL 生成函数。 fullFileName: 完整文件名或完整文件路径。 返回值:返回公共访问 URL 和错误信息。

func EnsureDir added in v0.11.9

func EnsureDir(fs afero.Fs, dir string) error

func SanitizeFileName added in v1.0.14

func SanitizeFileName(name string) string

SanitizeFileName 清理单个文件名(路径段)中的非法字符。 处理范围:控制字符、Windows 非法字符、NUL、Windows 保留名、末尾空格/点号。 name: 原始文件名。 返回值:返回清理后的安全文件名。

func SanitizeFilePath added in v1.0.14

func SanitizeFilePath(filePath string) string

SanitizeFilePath 清理完整文件路径中的非法字符,保留路径分隔符 '/'。 对每个路径段分别应用 SanitizeFileName 规则。 filePath: 原始文件路径。 返回值:返回清理后的安全文件路径。

Types

type FSFactory added in v1.0.10

type FSFactory func(key string) (afero.Fs, Release, error)

FSFactory 定义按配置键创建文件系统的工厂函数。 key: 存储配置键。 返回值:返回 afero.Fs、释放函数和错误信息。

type OssSettings added in v0.11.9

type OssSettings struct {
	Endpoint  string
	AccessKey string
	SecretKey string
	Bucket    string
	Region    string
	Path      string
}

type PublicResourceComponent added in v1.0.10

type PublicResourceComponent struct {
	ginshared.DefaultComponent
}

PublicResourceComponent 注册文件资源公开访问路由。 负责按 xid 查找缓存中的资源描述,并通过 afero.Fs 返回文件内容。

func (*PublicResourceComponent) OnEngineInited added in v1.0.10

func (c *PublicResourceComponent) OnEngineInited(r *gin.Engine) error

OnEngineInited 在 Gin 引擎初始化完成后注册资源路由。 r: Gin 路由引擎。 返回值:返回注册过程中的错误信息。

type PublicResourceEntry added in v1.0.10

type PublicResourceEntry struct {
	Key          string
	FullFileName string
}

PublicResourceEntry 定义公开资源缓存条目。 Key: 存储配置键。 FullFileName: 调用方传入的完整文件名。

type PublicURLFunc added in v1.0.10

type PublicURLFunc func(fullFileName string) (string, error)

PublicURLFunc 定义根据完整文件名生成公共访问 URL 的函数。 fullFileName: 完整文件名或完整文件路径。 返回值:返回公共访问 URL 和错误信息。

func GetDefaultPublicURLFunc added in v1.0.10

func GetDefaultPublicURLFunc() (PublicURLFunc, error)

GetDefaultPublicURLFunc 返回默认存储键对应的公共 URL 生成函数。 Deprecated: 请使用 GetPublicURLFuncDefault。 返回值:返回默认 `fileroot` 配置对应的公共 URL 生成函数和错误信息。

func GetPublicURLFunc added in v1.0.10

func GetPublicURLFunc(key string) (PublicURLFunc, error)

GetPublicURLInit 根据存储键返回公共 URL 生成函数。 key: 存储配置键。 返回值:返回公共 URL 生成函数和错误信息。

func GetPublicURLFuncDefault added in v1.0.10

func GetPublicURLFuncDefault() (PublicURLFunc, error)

GetPublicURLFuncDefault 返回默认存储键对应的公共 URL 生成函数。 返回值:返回默认 `fileroot` 配置对应的公共 URL 生成函数和错误信息。

func GetPublicURLInit added in v1.0.10

func GetPublicURLInit(key string) (PublicURLFunc, error)

GetPublicURLInit 根据存储键返回公共 URL 生成函数。 Deprecated: 请使用 GetPublicURLFunc。 key: 存储配置键。 返回值:返回公共 URL 生成函数和错误信息。

type PublicURLFuncFactory added in v1.0.10

type PublicURLFuncFactory func(key string) PublicURLFunc

PublicURLFuncFactory 定义按配置键返回公共 URL 生成函数的工厂函数。 key: 存储配置键。 返回值:返回与当前 key 绑定的公共 URL 生成函数。

type Release

type Release func()

func CreateFs

func CreateFs(key string) (afero.Fs, Release, error)

CreateFs 根据配置键创建文件系统实例。 key: 存储配置键,未配置时回退到默认 `fileroot`。 返回值:返回 afero.Fs、释放函数和错误信息。

func InitSftpRoot added in v0.11.9

func InitSftpRoot(key string) (afero.Fs, Release, error)

type SftpSettings added in v0.11.9

type SftpSettings struct {
	Endpoint       string
	Account        string
	Password       string
	PrivateKeypath string
	Path           string
}

Jump to

Keyboard shortcuts

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