mirror of
https://github.com/go-admin-team/go-admin.git
synced 2026-09-22 02:27:57 +00:00
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.
53 lines
1.4 KiB
Go
53 lines
1.4 KiB
Go
package file_store
|
||
|
||
import (
|
||
"github.com/aliyun/aliyun-oss-go-sdk/oss"
|
||
"log"
|
||
)
|
||
|
||
type ALiYunOSS struct {
|
||
Client interface{}
|
||
BucketName string
|
||
}
|
||
|
||
//Setup 装载
|
||
//endpoint sss
|
||
func (e *ALiYunOSS) Setup(endpoint, accessKeyID, accessKeySecret, BucketName string, options ...ClientOption) error {
|
||
client, err := oss.New(endpoint, accessKeyID, accessKeySecret)
|
||
if err != nil {
|
||
log.Println("Error:", err)
|
||
return err
|
||
}
|
||
e.Client = client
|
||
e.BucketName = BucketName
|
||
|
||
return nil
|
||
}
|
||
|
||
// UpLoad 文件上传
|
||
func (e *ALiYunOSS) UpLoad(yourObjectName string, localFile interface{}) error {
|
||
client, ok := e.Client.(*oss.Client)
|
||
if !ok {
|
||
return notConfigured(AliYunOSS)
|
||
}
|
||
// 获取存储空间。
|
||
bucket, err := client.Bucket(e.BucketName)
|
||
if err != nil {
|
||
log.Println("Error:", err)
|
||
return err
|
||
}
|
||
// 设置分片大小为100 KB,指定分片上传并发数为3,并开启断点续传上传。
|
||
// 其中<yourObjectName>与objectKey是同一概念,表示断点续传上传文件到OSS时需要指定包含文件后缀在内的完整路径,例如abc/efg/123.jpg。
|
||
// "LocalFile"为filePath,100*1024为partSize。
|
||
err = bucket.UploadFile(yourObjectName, localFile.(string), 100*1024, oss.Routines(3), oss.Checkpoint(true, ""))
|
||
if err != nil {
|
||
log.Println("Error:", err)
|
||
return err
|
||
}
|
||
return nil
|
||
}
|
||
|
||
func (e *ALiYunOSS) GetTempToken() (string, error) {
|
||
return "", nil
|
||
}
|