Files
go-admin/common/file_store/obs.go
T
zhangwenjian fcbd9ae02e fix🐛: an unconfigured object store reports it instead of panicking
Each implementation keeps its provider client in an interface{} field that
Setup assigns, so an unconfigured store holds nil - and asserting nil to
the provider's client type panics:

  panic: interface conversion: interface {} is nil, not *oss.Client

The upload endpoint reaches that path for any request naming a provider
the deployment never configured.

Three more things were wrong in the same files. OXS.Setup printed a
failure and returned the store anyway, handing back exactly the broken
object that panics. HuaWeiOBS.UpLoad printed the provider's error and
returned nil, so a failed upload reported success. Both it and
QiNiuKODO.UpLoad asserted the local path was a string without checking.

The tests asked the reader to paste their own credentials, so they failed
for everyone who did not. They now cover the guards and skip the part
that needs a provider unless credentials are in the environment.
2026-08-24 13:23:07 +08:00

59 lines
1.6 KiB
Go

package file_store
import (
"fmt"
"github.com/huaweicloud/huaweicloud-sdk-go-obs/obs"
"log"
)
type HuaWeiOBS struct {
Client interface{}
BucketName string
}
func (e *HuaWeiOBS) Setup(endpoint, accessKeyID, accessKeySecret, BucketName string, options ...ClientOption) error {
// 创建ObsClient结构体
client, err := obs.New(accessKeyID, accessKeySecret, endpoint)
if err != nil {
log.Println("Error:", err)
return err
}
e.Client = client
e.BucketName = BucketName
return nil
}
// UpLoad 文件上传
// yourObjectName 文件路径名称,与objectKey是同一概念,表示断点续传上传文件到OSS时需要指定包含文件后缀在内的完整路径,例如abc/efg/123.jpg
func (e *HuaWeiOBS) UpLoad(yourObjectName string, localFile interface{}) error {
client, ok := e.Client.(*obs.ObsClient)
if !ok {
return notConfigured(HuaweiOBS)
}
source, ok := localFile.(string)
if !ok {
return fmt.Errorf("obs upload wants a path, got %T", localFile)
}
// 获取存储空间。
input := &obs.PutFileInput{}
input.Bucket = e.BucketName
input.Key = yourObjectName
input.SourceFile = source
output, err := client.PutFile(input)
if err != nil {
// The error used to be printed and nil returned, so a failed upload
// reported success to the caller.
if obsError, ok := err.(obs.ObsError); ok {
return fmt.Errorf("obs upload: %s: %s", obsError.Code, obsError.Message)
}
return fmt.Errorf("obs upload: %w", err)
}
log.Printf("obs upload ok, requestId=%s etag=%s", output.RequestId, output.ETag)
return nil
}
func (e *HuaWeiOBS) GetTempToken() (string, error) {
return "", nil
}