Documentation
¶
Overview ¶
Package cos 提供基于腾讯云 COS Go SDK 的多实例对象存储客户端配置中心。
配置模型(两层):
defaults —— 全局默认值(max_idle_conns / part_size 等) instances []Instance —— 命名实例(secret_id/key + bucket_url)
业务取用:
co := ioc.Container.GetConf(cos.ConfName).(*cos.COS)
pub := co.Client("public-cdn")
priv := co.Client("private-doc")
_ = co.Put(ctx, "public-cdn", "key", reader, nil)
_ = co.Upload(ctx, "private-doc", "key", "/path/file", nil)
Index ¶
- Constants
- Variables
- type COS
- func (c *COS) Client(name string) *cossdk.Client
- func (c *COS) Close(ctx context.Context) error
- func (c *COS) Delete(ctx context.Context, name, key string, opt ...*cossdk.ObjectDeleteOptions) error
- func (c *COS) Download(ctx context.Context, name, key, filePath string, ...) error
- func (c *COS) Get(ctx context.Context, name, key string, opt *cossdk.ObjectGetOptions) (io.ReadCloser, error)
- func (c *COS) GetToFile(ctx context.Context, name, key, filePath string, opt *cossdk.ObjectGetOptions) error
- func (c *COS) Head(ctx context.Context, name, key string, opt *cossdk.ObjectHeadOptions) (*cossdk.Response, error)
- func (c *COS) HealthCheck(ctx context.Context) error
- func (c *COS) HealthCheckOne(ctx context.Context, name string) error
- func (c *COS) Init(_ context.Context) error
- func (c *COS) IssueSTS(ctx context.Context, name string, scope STSScope) (*STSCredential, error)
- func (c *COS) IssueSTSCached(ctx context.Context, name string, scope STSScope) (*STSCredential, error)
- func (c *COS) List(ctx context.Context, name string, opt *cossdk.BucketGetOptions) (*cossdk.BucketGetResult, error)
- func (c *COS) Name() string
- func (c *COS) Names() []string
- func (c *COS) NewClientFromSTS(name string, cred *STSCredential) (*cossdk.Client, error)
- func (c *COS) PresignGet(ctx context.Context, name, key string, expire time.Duration) (*url.URL, error)
- func (c *COS) PresignHead(ctx context.Context, name, key string, expire time.Duration) (*url.URL, error)
- func (c *COS) PresignPut(ctx context.Context, name, key string, expire time.Duration) (*url.URL, error)
- func (c *COS) PresignURL(ctx context.Context, name, method, key string, expire time.Duration, ...) (*url.URL, error)
- func (c *COS) PresignURLWithCred(ctx context.Context, name string, cred *STSCredential, method, key string, ...) (*url.URL, error)
- func (c *COS) Put(ctx context.Context, name, key string, r io.Reader, ...) error
- func (c *COS) PutFromFile(ctx context.Context, name, key, filePath string, opt *cossdk.ObjectPutOptions) error
- func (c *COS) Upload(ctx context.Context, name, key, filePath string, ...) (*cossdk.CompleteMultipartUploadResult, error)
- type CosDefaults
- type CosInstance
- type STSCredential
- type STSScope
Constants ¶
const ConfName = "cos"
ConfName 注册到 IOC 容器中的名称
Variables ¶
var ( // ErrSTSScopeActionEmpty 触发场景:STSScope.Action 为空切片或全部元素为空字符串。 ErrSTSScopeActionEmpty = errors.New("sts scope: action is empty") // ErrSTSScopeActionTooMany 触发场景:去重后 STSScope.Action 长度 > stsActionMaxCount(20)。 ErrSTSScopeActionTooMany = errors.New("sts scope: action exceeds 20 entries") // ErrSTSScopeActionInvalid 触发场景:单条 Action 不以 "name/cos:" 或 "name/sts:" 开头、 // 包含通配符 "*"、含空白字符(空格 / 制表符 / 换行符)或其它控制字符。 ErrSTSScopeActionInvalid = errors.New("sts scope: action invalid (must start with name/cos: or name/sts:, no wildcard, no whitespace)") // ErrSTSScopeKeyMissing 触发场景:STSScope.Key 与 STSScope.KeyPrefix 同时为空。 ErrSTSScopeKeyMissing = errors.New("sts scope: key and key_prefix are both empty") // ErrSTSScopeKeyInvalid 触发场景:Key / KeyPrefix 含 ".."、"*",或 KeyPrefix 等于 // 单一 "/"(等价于全桶授权,被禁用)。 ErrSTSScopeKeyInvalid = errors.New("sts scope: key/key_prefix invalid (forbid '..', '*', single '/')") // ErrSTSAppIDMissing 触发场景:IssueSTS 阶段 resolvedInstance.STSAppID 为空, // 且无法从 BucketURL 推断(桶名末段非纯数字,或未配置 BucketURL)。 ErrSTSAppIDMissing = errors.New("sts: app_id is empty (configure sts_app_id or use bucket_url containing -{appid})") // ErrSTSRegionMissing 触发场景:IssueSTS 阶段 resolvedInstance.STSRegion 为空, // 且无法从 BucketURL 推断(host 不符合 *.cos.{region}.myqcloud.com 模式)。 ErrSTSRegionMissing = errors.New("sts: region is empty (configure sts_region or use a bucket_url containing region)") // ErrSTSBucketMissing 触发场景:IssueSTS 阶段 resolvedInstance.Bucket 为空, // 且无法从 BucketURL 推断。多用于配置错误(BucketURL 缺失的 service-only 实例)。 ErrSTSBucketMissing = errors.New("sts: bucket is empty (configure bucket_url to enable sts)") )
哨兵错误:STSScope 校验与 IssueSTS 上下文校验。
集中在本文件维护,便于排障时按错误码一站式定位触发场景。
Functions ¶
This section is empty.
Types ¶
type COS ¶
type COS struct {
CosDefaults `toml:",inline" yaml:",inline"`
Instances []CosInstance `toml:"instances" yaml:"instances" json:"instances"`
EnvInstance CosInstance `toml:"-" yaml:"-" json:"-"`
// contains filtered or unexported fields
}
COS 注册到 IoC 容器的顶层配置
func (*COS) Delete ¶
func (c *COS) Delete(ctx context.Context, name, key string, opt ...*cossdk.ObjectDeleteOptions) error
Delete 删除单个对象
func (*COS) Download ¶
func (c *COS) Download(ctx context.Context, name, key, filePath string, override *cossdk.MultiDownloadOptions) error
Download 大文件分块下载,自带断点续传
使用 SDK 内置的 ObjectService.Download,对于大对象推荐使用此方法。
func (*COS) Get ¶
func (c *COS) Get(ctx context.Context, name, key string, opt *cossdk.ObjectGetOptions) (io.ReadCloser, error)
Get 下载对象到 io.ReadCloser,调用方负责 Close
func (*COS) GetToFile ¶
func (c *COS) GetToFile(ctx context.Context, name, key, filePath string, opt *cossdk.ObjectGetOptions) error
GetToFile 下载对象到本地文件(小文件场景;大文件请使用 Download)
func (*COS) Head ¶
func (c *COS) Head(ctx context.Context, name, key string, opt *cossdk.ObjectHeadOptions) (*cossdk.Response, error)
Head 获取对象元信息,常用于探测对象是否存在
func (*COS) HealthCheckOne ¶
HealthCheckOne 仅探活指定实例
func (*COS) Init ¶
Init 初始化方法
完成的工作:
- 绑定私有 logger
- env-only 模式 fallback:把 EnvInstance 提升为 Instances[0]
- 校验:name 唯一 + secret/bucket_url 必填
- 立即为每个 instance 构建 *cossdk.Client(不发起 TCP 连接)
选择"立即构建 SDK Client"是为了:
- 配置错误(bucket_url 拼写、CA 文件不存在等)在启动期暴露
- SDK Client 创建本身不发起 HTTP 请求,不会拖慢启动
func (*COS) IssueSTS ¶
IssueSTS 向腾讯云 STS 服务申请临时凭证。
行为:
- scope 校验失败立即返回(不重试、不入网),错误链中含 ErrSTSScope* 哨兵;
- 网络错误 / 5xx / 命中 stsRetryableErrorCodes 的 Code → 业务层重试(runWithRetryFn);
- ctx 取消立即返回(SDK 本身不接受 ctx,内部用 goroutine + select 适配);
- 返回前 ExpiredTime 自动减去 30 秒安全垫,对应 Duration 也按相同安全垫推算。
并发安全:可被多个 goroutine 并发调用;底层 STS *Client 每次重新构造,零共享状态。
错误语义:
- ErrSTSScopeActionEmpty / ErrSTSScopeActionTooMany / ErrSTSScopeActionInvalid:scope.Action 不合法;
- ErrSTSScopeKeyMissing / ErrSTSScopeKeyInvalid:scope.Key / KeyPrefix 不合法;
- ErrSTSAppIDMissing / ErrSTSRegionMissing / ErrSTSBucketMissing:实例上下文不足;
- 其余错误经 wrapErr("sts.issue", "", err) 包装后返回。
func (*COS) IssueSTSCached ¶
func (c *COS) IssueSTSCached(ctx context.Context, name string, scope STSScope) (*STSCredential, error)
IssueSTSCached 带缓存的临时凭证签发(Phase 2:LRU + singleflight + canonical key)。
行为:
- 先按 (name, scope) canonical key 在 LRU 中查找;命中且剩余有效期 ≥ 30s 时直接返回缓存副本;
- 未命中则走 singleflight 合并并发调用 → 实际触发一次 IssueSTS;
- 签发成功后剩余有效期 ≥ 30s 时写入缓存;< 30s 时不写入(避免立即过期的凭证污染缓存)。
与 IssueSTS 的差异:
- 错误语义完全一致:scope 校验失败 / 实例配置缺失 / 网络错误均原样透传;
- 同时段内对相同 (name, scope) 的多次调用只会触发一次实际网络请求;
- 返回的 STSCredential 与 IssueSTS 同样已扣 30s 安全垫。
并发安全:可被多个 goroutine 并发调用;缓存与 singleflight 内部已加锁。
func (*COS) List ¶
func (c *COS) List(ctx context.Context, name string, opt *cossdk.BucketGetOptions) (*cossdk.BucketGetResult, error)
List 列举对象(封装 Bucket.Get)
func (*COS) NewClientFromSTS ¶
NewClientFromSTS 用临时凭证派生临时 *cossdk.Client,复用实例 transport。
行为:
- 复用 holder.transport.base 作为底层 RoundTripper(避免连接池碎片);
- 用 cossdk.AuthorizationTransport 注入 cred.SessionToken;
- 不修改原 holder 的 sdkClient,返回的是仅本次调用使用的全新 *cossdk.Client。
并发安全:可并发调用;返回的 *cossdk.Client 与 cos-go-sdk-v5 一致按读使用。
错误语义:cred 为 nil 或字段不全 → 立即返回,不会真实创建 client。
func (*COS) PresignGet ¶
func (c *COS) PresignGet(ctx context.Context, name, key string, expire time.Duration) (*url.URL, error)
PresignGet 简化版:生成预签名下载 URL
func (*COS) PresignHead ¶
func (c *COS) PresignHead(ctx context.Context, name, key string, expire time.Duration) (*url.URL, error)
PresignHead 简化版:生成预签名 HEAD URL(预检)
func (*COS) PresignPut ¶
func (c *COS) PresignPut(ctx context.Context, name, key string, expire time.Duration) (*url.URL, error)
PresignPut 简化版:生成预签名上传 URL(前端直传场景)
func (*COS) PresignURL ¶
func (c *COS) PresignURL(ctx context.Context, name, method, key string, expire time.Duration, opt *cossdk.PresignedURLOptions) (*url.URL, error)
PresignURL 生成预签名 URL
参数:
- name 实例名(在配置文件 instances 中声明)
- method GET / PUT / HEAD / DELETE 等标准 HTTP 方法
- key 对象 key
- expire 过期时长(>0),生产环境建议 <= 1h
- opt 可选 PresignedURLOptions(query / header 等)
func (*COS) PresignURLWithCred ¶
func (c *COS) PresignURLWithCred( ctx context.Context, name string, cred *STSCredential, method, key string, expire time.Duration, opt *cossdk.PresignedURLOptions, ) (*url.URL, error)
PresignURLWithCred 用指定临时凭证签预签名 URL,并把 SessionToken 写入查询参数。
与 PresignURL 的差异:
- 使用 cred.TmpSecretID / TmpSecretKey 计算签名;
- 签名后把 x-cos-security-token=cred.SessionToken 注入到 URL.RawQuery(COS 协议要求)。
适用场景:服务端用 IssueSTS 拿到临时凭证后,无需新建临时 *cossdk.Client, 直接签预签名 URL 下发给端侧。
并发安全:可并发调用,SDK GetPresignedURL 本身是纯 CPU 计算无共享状态。
func (*COS) Put ¶
func (c *COS) Put(ctx context.Context, name, key string, r io.Reader, opt *cossdk.ObjectPutOptions) error
Put 上传一个对象(小文件 / 流式)
参数:
- name 实例名(在配置文件 instances 中声明)
- key 对象 key(不带 bucket 前缀)
- r 数据源(任意 io.Reader)
- opt COS SDK 的 PutOptions(可为 nil)
行为:
- 自动包装重试(业务级,按实例的 MaxRetries / RetryBackoff)
- 重试条件:网络错误 / 5xx 服务端错误
- ctx 取消立即放弃
func (*COS) PutFromFile ¶
func (c *COS) PutFromFile(ctx context.Context, name, key, filePath string, opt *cossdk.ObjectPutOptions) error
PutFromFile 从本地文件上传(适合中小文件,>5GB 请使用 Upload)
func (*COS) Upload ¶
func (c *COS) Upload(ctx context.Context, name, key, filePath string, override *cossdk.MultiUploadOptions) (*cossdk.CompleteMultipartUploadResult, error)
Upload 大文件分块上传,自带断点续传
使用 SDK 内置的 ObjectService.Upload,对于 >5GB 的文件强烈推荐使用此方法。 通过实例的 PartSize / ThreadPoolSize / CheckPoint 等配置控制并发与续传。
type CosDefaults ¶
type CosDefaults struct {
// MaxIdleConns HTTP Transport 全局最大空闲连接数。
// 默认:200;范围:>=0;0 表示不限制。
MaxIdleConns *int `` /* 149-byte string literal not displayed */
// MaxIdleConnsPerHost 每个 host 最大空闲连接数。
// 默认:100;范围:>=0;建议 = MaxIdleConns / 桶数量。
MaxIdleConnsPerHost *int `` /* 158-byte string literal not displayed */
// MaxConnsPerHost 每个 host 最大并发连接数(含使用中)。
// 默认:200;范围:>=0;0 表示不限制。
MaxConnsPerHost *int `` /* 153-byte string literal not displayed */
// IdleConnTimeout 空闲连接最大保活时间。
// 单位:秒;默认:90;范围:>0。
IdleConnTimeout *int `` /* 152-byte string literal not displayed */
// DialTimeout TCP 拨号超时。
// 单位:秒;默认:10;范围:>0。
DialTimeout *int `` /* 147-byte string literal not displayed */
// KeepAlive TCP 长连接保活间隔。
// 单位:秒;默认:60。
KeepAlive *int `` /* 145-byte string literal not displayed */
// TLSHandshakeTimeout TLS 握手超时。
// 单位:秒;默认:10;范围:>0。
TLSHandshakeTimeout *int `` /* 156-byte string literal not displayed */
// ResponseHeaderTimeout 等待响应 header 的超时。
// 单位:秒;默认:30;范围:>0。
ResponseHeaderTimeout *int `` /* 158-byte string literal not displayed */
// ExpectContinueTimeout 100-continue 等待时间。
// 单位:秒;默认:1。
ExpectContinueTimeout *int `` /* 158-byte string literal not displayed */
// ClientTimeout HTTP 整体超时(含拨号、读、写)。
// 单位:秒;默认:60(兼顾一般 Put/Get 的免 Hang 兑底)。
// 大文件 Multipart 上传/下载场景请在实例级将本值调为 0(仅由 ctx 控制)或业务侧使用 ctx.WithTimeout 覆盖。
ClientTimeout *int `` /* 149-byte string literal not displayed */
// TLSInsecureSkipVerify 是否跳过证书校验。
// 默认:false;生产环境务必保持 false。
TLSInsecureSkipVerify *bool `` /* 162-byte string literal not displayed */
// TLSCAFile 自签 CA 证书路径(PEM)。
TLSCAFile *string `` /* 143-byte string literal not displayed */
// MaxRetries 最大重试次数。
// 默认:3;范围:>=0;0 表示不重试。
MaxRetries *int `` /* 146-byte string literal not displayed */
// RetryBackoff 重试退避基数(指数退避:base * 2^attempt)。
// 单位:毫秒;默认:200。
RetryBackoff *int `` /* 148-byte string literal not displayed */
// PartSize 分块上传单块大小。
// 单位:MB;默认:8;范围:>=1(COS 限制:单块 1~5GB,分块数 <= 10000)。
PartSize *int64 `` /* 144-byte string literal not displayed */
// ThreadPoolSize 分块上传/下载并发数。
// 默认:5;范围:>=1。
ThreadPoolSize *int `` /* 151-byte string literal not displayed */
// CheckPoint 是否启用断点续传。
// 默认:true。
CheckPoint *bool `` /* 146-byte string literal not displayed */
// ServiceURL 列举所有 bucket 用的服务域名。
// 默认:"https://service.cos.myqcloud.com"。
ServiceURL *string `` /* 143-byte string literal not displayed */
// STSEndpoint STS 服务接入点,仅填裸 host,不带协议;协议固定 https。
// 默认:"sts.tencentcloudapi.com"。
STSEndpoint *string `` /* 141-byte string literal not displayed */
// STSRegion 申请临时凭证时使用的地域。
// 留空时由 IssueSTS 从 BucketURL 推断的 Region 兜底。
STSRegion *string `` /* 139-byte string literal not displayed */
// STSDefaultDuration 临时凭证默认有效期(秒)。
// 默认:1800;范围:[60, 7200]。Scope.DurationSecs 为 0 时使用本值。
STSDefaultDuration *int `` /* 149-byte string literal not displayed */
// STSAppID 腾讯云开发者 AppID(纯数字字符串)。
// 留空时由 IssueSTS 从 BucketURL 末段推断;推断失败且未配置则签发阶段报错。
STSAppID *string `` /* 139-byte string literal not displayed */
}
CosDefaults 全局默认值(指针字段,nil = 继承)
type CosInstance ¶
type CosInstance struct {
// Name 实例唯一标识。业务通过 cosConf.Client(name) 取用,必填且不可重名。
Name string `toml:"name" yaml:"name" json:"name" env:"-"`
// SecretID 永久密钥 SecretId。可用 ${COS_SECRET_ID} 占位,生产环境强烈建议环境变量注入。
SecretID string `toml:"secret_id" yaml:"secret_id" json:"secret_id" env:"COS_SECRET_ID"`
// SecretKey 永久密钥 SecretKey。可用 ${COS_SECRET_KEY} 占位,生产环境强烈建议环境变量注入。
SecretKey string `toml:"secret_key" yaml:"secret_key" json:"secret_key" env:"COS_SECRET_KEY"`
// SessionToken 临时密钥 Token(CAM 角色场景)。永久密钥时留空。
SessionToken string `toml:"session_token" yaml:"session_token" json:"session_token" env:"COS_SESSION_TOKEN"`
// BucketURL 桶访问域名,必填。
// 例:https://my-bucket-1250000000.cos.ap-guangzhou.myqcloud.com
BucketURL string `toml:"bucket_url" yaml:"bucket_url" json:"bucket_url" env:"COS_BUCKET_URL"`
// ServiceURL 列举所有桶的服务域名。可继承默认值。
ServiceURL string `toml:"service_url" yaml:"service_url" json:"service_url" env:"COS_SERVICE_URL"`
// CIURL 数据万象(CI)域名。仅用图片处理时填写。
CIURL string `toml:"ci_url" yaml:"ci_url" json:"ci_url" env:"COS_CI_URL"`
// FetchURL 异步拉取域名。仅用 PutObjectFromURL 时填写。
FetchURL string `toml:"fetch_url" yaml:"fetch_url" json:"fetch_url" env:"COS_FETCH_URL"`
// Region 桶所在地域,例 ap-guangzhou。可空,从 BucketURL 推断。
Region string `toml:"region" yaml:"region" json:"region" env:"COS_REGION"`
// Bucket 桶名,可空,从 BucketURL 推断。
Bucket string `toml:"bucket" yaml:"bucket" json:"bucket" env:"COS_BUCKET"`
// 以下字段均与 CosDefaults 中的同名字段语义一致,nil = 继承默认。
MaxIdleConns *int `toml:"max_idle_conns" yaml:"max_idle_conns" json:"max_idle_conns" env:"COS_MAX_IDLE_CONNS"`
MaxIdleConnsPerHost *int `` /* 126-byte string literal not displayed */
MaxConnsPerHost *int `toml:"max_conns_per_host" yaml:"max_conns_per_host" json:"max_conns_per_host" env:"COS_MAX_CONNS_PER_HOST"`
IdleConnTimeout *int `toml:"idle_conn_timeout" yaml:"idle_conn_timeout" json:"idle_conn_timeout" env:"COS_IDLE_CONN_TIMEOUT"`
DialTimeout *int `toml:"dial_timeout" yaml:"dial_timeout" json:"dial_timeout" env:"COS_DIAL_TIMEOUT"`
KeepAlive *int `toml:"keep_alive" yaml:"keep_alive" json:"keep_alive" env:"COS_KEEP_ALIVE"`
TLSHandshakeTimeout *int `toml:"tls_handshake_timeout" yaml:"tls_handshake_timeout" json:"tls_handshake_timeout" env:"COS_TLS_HANDSHAKE_TIMEOUT"`
ResponseHeaderTimeout *int `` /* 126-byte string literal not displayed */
ExpectContinueTimeout *int `` /* 126-byte string literal not displayed */
ClientTimeout *int `toml:"client_timeout" yaml:"client_timeout" json:"client_timeout" env:"COS_CLIENT_TIMEOUT"`
TLSInsecureSkipVerify *bool `` /* 130-byte string literal not displayed */
TLSCAFile *string `toml:"tls_ca_file" yaml:"tls_ca_file" json:"tls_ca_file" env:"COS_TLS_CA_FILE"`
MaxRetries *int `toml:"max_retries" yaml:"max_retries" json:"max_retries" env:"COS_MAX_RETRIES"`
RetryBackoff *int `toml:"retry_backoff" yaml:"retry_backoff" json:"retry_backoff" env:"COS_RETRY_BACKOFF"`
PartSize *int64 `toml:"part_size" yaml:"part_size" json:"part_size" env:"COS_PART_SIZE"`
ThreadPoolSize *int `toml:"thread_pool_size" yaml:"thread_pool_size" json:"thread_pool_size" env:"COS_THREAD_POOL_SIZE"`
CheckPoint *bool `toml:"check_point" yaml:"check_point" json:"check_point" env:"COS_CHECK_POINT"`
// STS 临时凭证签发(实例级覆盖;语义同 CosDefaults 中的同名字段,nil = 继承默认)
STSEndpoint *string `toml:"sts_endpoint" yaml:"sts_endpoint" json:"sts_endpoint" env:"COS_STS_ENDPOINT"`
STSRegion *string `toml:"sts_region" yaml:"sts_region" json:"sts_region" env:"COS_STS_REGION"`
STSDefaultDuration *int `toml:"sts_default_duration" yaml:"sts_default_duration" json:"sts_default_duration" env:"COS_STS_DEFAULT_DURATION"`
STSAppID *string `toml:"sts_app_id" yaml:"sts_app_id" json:"sts_app_id" env:"COS_STS_APP_ID"`
}
CosInstance 一个命名 COS 实例(secret + bucket_url)
type STSCredential ¶
type STSCredential struct {
// TmpSecretID 临时凭证的 SecretId。
TmpSecretID string `json:"tmpSecretId"`
// TmpSecretKey 临时凭证的 SecretKey(敏感字段,禁止入日志)。
TmpSecretKey string `json:"tmpSecretKey"`
// SessionToken 临时凭证的会话令牌(敏感字段,禁止入日志)。
SessionToken string `json:"sessionToken"`
// StartTime 凭证生效起始时间(Unix 秒)。
StartTime int64 `json:"startTime"`
// ExpiredTime 凭证过期时间(Unix 秒),已扣除 30 秒安全垫。
ExpiredTime int64 `json:"expiredTime"`
// RequestID 腾讯云 STS 服务返回的请求 ID,仅用于排障,不下发端侧。
RequestID string `json:"-"`
}
STSCredential 临时凭证(透传给前端 / 子进程)。
安全约束:
- TmpSecretKey / SessionToken 属于敏感凭证,禁止写入日志或 trace;
- RequestID 仅用于服务端排障,不应下发给端侧(json:"-");
- ExpiredTime 已扣除 30 秒安全垫,调用方可直接据此判定有效性。
并发安全:作为返回值被调用方持有;本包不会再次修改其字段。
type STSScope ¶
type STSScope struct {
// Action COS 操作动作列表,如 ["name/cos:PutObject", "name/cos:GetObject"]。
Action []string
// Key 单对象授权(与 KeyPrefix 二选一)。
Key string
// KeyPrefix 前缀授权(与 Key 二选一),自动追加 "*" 通配。
KeyPrefix string
// DurationSecs 凭证有效期(秒),0 时使用实例的 STSDefaultDuration。
DurationSecs int
// MaxObjectSize 上传对象大小上限(字节),0 表示不限制。
MaxObjectSize int64
// ContentType 限定上传 MIME 类型,空表示不限制。
ContentType string
// SourceIP 限定调用来源 IP(CIDR 列表),空表示不限制。
SourceIP []string
}
STSScope 描述本次临时凭证的权限范围(最小权限原则)。
使用约束:
- Action 必填,每条以 "name/cos:" 或 "name/sts:" 开头,禁用 "*" 通配;
- Key 与 KeyPrefix 至少一个非空(互斥使用);
- DurationSecs 0 时使用 STSDefaultDuration,越界自动截断到 [60, 7200] 并 Warn;
- MaxObjectSize / ContentType / SourceIP 仅在非零时输出到 policy condition。
并发安全:可被多个 goroutine 并发传入 IssueSTS / IssueSTSCached, 内部不会修改入参;结构体本身可被复制使用。