1 Star 0 Fork 0

h79/goutils

加入 Gitee
与超过 1400万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
oss.go 2.19 KB
一键复制 编辑 原始数据 按行查看 历史
huqiuyun 提交于 2025-08-06 15:41 +08:00 . oss 分片上传
package alyoss
import (
"context"
fileconfig "gitee.com/h79/goutils/common/file/config"
filestream "gitee.com/h79/goutils/common/file/stream"
"github.com/aliyun/aliyun-oss-go-sdk/oss"
)
const minChunkSize = 5 * 1024 * 1024
type OssFile struct {
config *fileconfig.Config
bf *fileconfig.BucketFile
}
func NewOssFile(config *fileconfig.Config, file *fileconfig.BucketFile) *OssFile {
if config.MinChunkSize <= minChunkSize {
config.MinChunkSize = minChunkSize
}
return &OssFile{config: config, bf: file}
}
func (of *OssFile) UploadFile(ctx context.Context, fileResult *fileconfig.Result) error {
// 阿里云主账号AccessKey拥有所有API的访问权限,风险很高。强烈建议您创建并使用RAM账号进行API访问或日常运维,请登录 https://ram.console.aliyun.com 创建RAM账号。
client, err := oss.New(of.config.EndPoint, of.config.AcsKeyID, of.config.AcsSecret)
if err != nil {
return err
}
bucket, err := client.Bucket(of.bf.Bucket)
if err != nil {
return err
}
return of.FileUpload(ctx, bucket, fileResult)
}
// IsObjectExist 文件是否存在
func (of *OssFile) IsObjectExist() (bool, error) {
// 阿里云主账号AccessKey拥有所有API的访问权限,风险很高。强烈建议您创建并使用RAM账号进行API访问或日常运维,请登录 https://ram.console.aliyun.com 创建RAM账号。
client, err := oss.New(of.config.EndPoint, of.config.AcsKeyID, of.config.AcsSecret)
if err != nil {
return false, err
}
bucket, err := client.Bucket(of.bf.Bucket)
if err != nil {
return false, err
}
return bucket.IsObjectExist(of.bf.Key)
}
func (of *OssFile) UploadStream(ctx context.Context, stream *filestream.Stream, fileResult *fileconfig.Result) error {
// 传入oss key, 文件路径
// 阿里云主账号AccessKey拥有所有API的访问权限,风险很高。强烈建议您创建并使用RAM账号进行API访问或日常运维,请登录 https://ram.console.aliyun.com 创建RAM账号。
client, err := oss.New(of.config.EndPoint, of.config.AcsKeyID, of.config.AcsSecret)
if err != nil {
return err
}
bucket, err := client.Bucket(of.bf.Bucket)
if err != nil {
return err
}
return of.StreamUpload(ctx, bucket, stream, fileResult)
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/h79/goutils.git
git@gitee.com:h79/goutils.git
h79
goutils
goutils
v1.33.0

搜索帮助