migu_server/common/file_store/obs.go
2024-10-18 23:46:54 +08:00

53 lines
1.4 KiB
Go
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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 {
// 获取存储空间。
input := &obs.PutFileInput{}
input.Bucket = e.BucketName
input.Key = yourObjectName
input.SourceFile = localFile.(string)
output, err := e.Client.(*obs.ObsClient).PutFile(input)
if err == nil {
fmt.Printf("RequestId:%s\n", output.RequestId)
fmt.Printf("ETag:%s, StorageClass:%s\n", output.ETag, output.StorageClass)
} else {
if obsError, ok := err.(obs.ObsError); ok {
fmt.Println(obsError.Code)
fmt.Println(obsError.Message)
} else {
fmt.Println(err)
}
}
return nil
}
func (e *HuaWeiOBS) GetTempToken() (string, error) {
return "", nil
}