1 Star 0 Fork 0

h79 / goutils

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
oss.go 3.23 KB
一键复制 编辑 原始数据 按行查看 历史
huqiuyun 提交于 2023-09-17 11:39 . file path
package alyoss
import (
"context"
"gitee.com/h79/goutils/common/debug"
fileconfig "gitee.com/h79/goutils/common/file/config"
"gitee.com/h79/goutils/common/result"
"gitee.com/h79/goutils/plugins"
"github.com/aliyun/aliyun-oss-go-sdk/oss"
)
type OssFile struct {
config fileconfig.Config
}
func NewOssFile(config fileconfig.Config) *OssFile {
return &OssFile{config: config}
}
func (of *OssFile) UploadFile(fileResult *fileconfig.Result, data *fileconfig.Base) error {
// 阿里云主账号AccessKey拥有所有API的访问权限,风险很高。强烈建议您创建并使用RAM账号进行API访问或日常运维,请登录 https://ram.console.aliyun.com 创建RAM账号。
client, err := oss.New(of.config.EndPoint, of.config.AccessKey, of.config.SecretKey)
if err != nil {
d := debug.NewStack(result.ErrUpload).
WithDetail("Upload: oss new failure").
WithError(err).
WithLevel(debug.DTightLevel)
_ = plugins.DoWithError(plugins.KAlarm, context.Background(), d)
return err
}
bucket, err := client.Bucket(data.Bucket)
if err != nil {
d := debug.NewStack(result.ErrUpload).
WithDetail("Upload: new bucket failure").
WithError(err).
WithLevel(debug.DTightLevel)
_ = plugins.DoWithError(plugins.KAlarm, context.Background(), d)
return err
}
return of.FileUpload(fileResult, data, bucket)
}
// IsObjectExist 文件是否存在
func (of *OssFile) IsObjectExist(data *fileconfig.Base) (bool, error) {
// 阿里云主账号AccessKey拥有所有API的访问权限,风险很高。强烈建议您创建并使用RAM账号进行API访问或日常运维,请登录 https://ram.console.aliyun.com 创建RAM账号。
client, err := oss.New(of.config.EndPoint, of.config.AccessKey, of.config.SecretKey)
if err != nil {
d := debug.NewStack(result.ErrUpload).
WithDetail("Upload: oss new failure").
WithError(err).
WithLevel(debug.DTightLevel)
_ = plugins.DoWithError(plugins.KAlarm, context.Background(), d)
return false, err
}
bucket, err := client.Bucket(data.Bucket)
if err != nil {
d := debug.NewStack(result.ErrUpload).
WithDetail("Upload: new bucket failure").
WithError(err).
WithLevel(debug.DTightLevel)
_ = plugins.DoWithError(plugins.KAlarm, context.Background(), d)
return false, err
}
return bucket.IsObjectExist(data.Key)
}
func (of *OssFile) UploadStream(fileResult *fileconfig.Result, data *fileconfig.Stream) error {
//传入osskey, 文件路径, oss路径
// 阿里云主账号AccessKey拥有所有API的访问权限,风险很高。强烈建议您创建并使用RAM账号进行API访问或日常运维,请登录 https://ram.console.aliyun.com 创建RAM账号。
client, err := oss.New(of.config.EndPoint, of.config.AccessKey, of.config.SecretKey)
if err != nil {
d := debug.NewStack(result.ErrUpload).
WithDetail("Upload: oss new").
WithError(err).
WithLevel(debug.DTightLevel)
_ = plugins.DoWithError(plugins.KAlarm, context.Background(), d)
return err
}
bucket, err := client.Bucket(data.Bucket)
if err != nil {
d := debug.NewStack(result.ErrUpload).
WithDetail("Upload: new bucket").
WithError(err).
WithLevel(debug.DTightLevel)
_ = plugins.DoWithError(plugins.KAlarm, context.Background(), d)
return err
}
return of.StreamUpload(fileResult, data, bucket)
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/h79/goutils.git
git@gitee.com:h79/goutils.git
h79
goutils
goutils
v1.9.9

搜索帮助

344bd9b3 5694891 D2dac590 5694891