JM-WechatMini/utils/upload/obs.go

68 lines
1.7 KiB
Go
Raw Normal View History

2023-11-02 04:34:46 +08:00
package upload
import (
"mime/multipart"
"github.com/huaweicloud/huaweicloud-sdk-go-obs/obs"
"github.com/pkg/errors"
"miniapp/global"
)
var HuaWeiObs = new(Obs)
type Obs struct{}
func NewHuaWeiObsClient() (client *obs.ObsClient, err error) {
return obs.New(global.GVA_CONFIG.HuaWeiObs.AccessKey, global.GVA_CONFIG.HuaWeiObs.SecretKey, global.GVA_CONFIG.HuaWeiObs.Endpoint)
}
func (o *Obs) UploadFile(file *multipart.FileHeader) (string, string, error) {
// var open multipart.File
open, err := file.Open()
if err != nil {
return "", "", err
}
defer open.Close()
filename := file.Filename
input := &obs.PutObjectInput{
PutObjectBasicInput: obs.PutObjectBasicInput{
ObjectOperationInput: obs.ObjectOperationInput{
Bucket: global.GVA_CONFIG.HuaWeiObs.Bucket,
Key: filename,
},
ContentType: file.Header.Get("content-type"),
},
Body: open,
}
var client *obs.ObsClient
client, err = NewHuaWeiObsClient()
if err != nil {
return "", "", errors.Wrap(err, "获取华为对象存储对象失败!")
}
_, err = client.PutObject(input)
if err != nil {
return "", "", errors.Wrap(err, "文件上传失败!")
}
filepath := global.GVA_CONFIG.HuaWeiObs.Path + "/" + filename
return filepath, filename, err
}
func (o *Obs) DeleteFile(key string) error {
client, err := NewHuaWeiObsClient()
if err != nil {
return errors.Wrap(err, "获取华为对象存储对象失败!")
}
input := &obs.DeleteObjectInput{
Bucket: global.GVA_CONFIG.HuaWeiObs.Bucket,
Key: key,
}
var output *obs.DeleteObjectOutput
output, err = client.DeleteObject(input)
if err != nil {
return errors.Wrapf(err, "删除对象(%s)失败!, output: %v", key, output)
}
return nil
}