feat: add s3 boot check option
This commit is contained in:
@@ -12,4 +12,6 @@ type Config struct {
|
||||
Bucket string
|
||||
Endpoint string
|
||||
PathStyle bool
|
||||
// CheckOnBoot 启动时检查 bucket 可用性(仅 s3)。
|
||||
CheckOnBoot bool
|
||||
}
|
||||
|
||||
@@ -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()...)
|
||||
|
||||
Reference in New Issue
Block a user