feat: add s3 boot check option

This commit is contained in:
2026-01-17 22:16:52 +08:00
parent bb62cf4280
commit 3728a921d2
6 changed files with 20 additions and 2 deletions

View File

@@ -12,4 +12,6 @@ type Config struct {
Bucket string
Endpoint string
PathStyle bool
// CheckOnBoot 启动时检查 bucket 可用性(仅 s3
CheckOnBoot bool
}

View File

@@ -39,9 +39,20 @@ func Provide(opts ...opt.Option) error {
return container.Container.Provide(func() (*Storage, error) {
store := &Storage{Config: &config}
if store.storageType() == "s3" {
if _, err := store.s3ClientForUse(); err != nil {
client, err := store.s3ClientForUse()
if err != nil {
return nil, err
}
if store.Config.CheckOnBoot {
// 启动时可选检查 bucket 是否可用,便于尽早暴露配置问题。
exists, err := client.BucketExists(context.Background(), store.Config.Bucket)
if err != nil {
return nil, fmt.Errorf("storage bucket check failed: %w", err)
}
if !exists {
return nil, fmt.Errorf("storage bucket not found: %s", store.Config.Bucket)
}
}
}
return store, nil
}, o.DiOptions()...)