1 Star 0 Fork 0

h79/goutils

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
oss.go 3.20 KB
一键复制 编辑 原始数据 按行查看 历史
huqiuyun 提交于 2023-02-03 17:19 . file
package alyoss
import (
"gitee.com/h79/goutils/common/debug"
"gitee.com/h79/goutils/common/file"
"gitee.com/h79/goutils/common/filemgr/config"
"gitee.com/h79/goutils/common/logger"
"gitee.com/h79/goutils/common/result"
"gitee.com/h79/goutils/plugins"
"github.com/aliyun/aliyun-oss-go-sdk/oss"
)
type OssFile struct {
config config.Config
}
func NewOssFile(config config.Config) *OssFile {
return &OssFile{config: config}
}
func (of *OssFile) UploadFile(fileResult *file.Result, data *file.Name) error {
logger.Debug("Upload: data= %v", data)
// 阿里云主账号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, 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, d)
return err
}
return of.FileUpload(fileResult, data, bucket)
}
// IsObjectExist 文件是否存在
func (of *OssFile) IsObjectExist(data *file.Name) (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, 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, d)
return false, err
}
return bucket.IsObjectExist(data.Key)
}
func (of *OssFile) UploadStream(fileResult *file.Result, data *file.Stream) error {
//传入osskey, 文件路径, oss路径
logger.Debug("Upload: data=%v", data)
// 阿里云主账号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, 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, 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.8.46

搜索帮助

A270a887 8829481 3d7a4017 8829481