1 Star 0 Fork 0

h79/goutils

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
file.go 1.82 KB
一键复制 编辑 原始数据 按行查看 历史
huqiuyun 提交于 2023-09-17 11:39 . file path
package alyoss
import (
fileconfig "gitee.com/h79/goutils/common/file/config"
"gitee.com/h79/goutils/common/file/tools"
"gitee.com/h79/goutils/common/logger"
"gitee.com/h79/goutils/common/result"
"github.com/aliyun/aliyun-oss-go-sdk/oss"
"io"
"os"
)
// FileUpload 文件上传
func (of *OssFile) FileUpload(fileResult *fileconfig.Result, data *fileconfig.Base, bucket *oss.Bucket) error {
fd, err := os.Open(data.FileName)
defer fd.Close()
if err != nil {
return result.Errorf(result.ErrUpload, "[File]open file size : %s", err)
}
f, err := fd.Stat()
if err != nil {
return result.Errorf(result.ErrUpload, "[File]file stat %s", err)
}
if f.Size() <= of.config.MinChunkSize {
//直接文件上传
err = bucket.PutObjectFromFile(data.Key, data.FileName)
if err != nil {
return result.Errorf(result.ErrUpload, "[File]Bucket put file %s", err)
}
return nil
}
//分片处理
imur, err := bucket.InitiateMultipartUpload(data.Key)
if err != nil {
return result.Errorf(result.ErrUpload, "[File]Bucket InitiateMultipartUpload %s", err)
}
chunks := tools.Chunk(f.Size(), tools.ChunkNum(f.Size(), of.config.MinChunkSize))
// 对每个分片调用UploadPart方法上传。
var parts []oss.UploadPart
for _, chunk := range chunks {
_, err := fd.Seek(chunk.Offset, io.SeekStart)
if err != nil {
return result.Errorf(result.ErrUpload, "[File]Bucket part file seek %s", err)
}
part, err := bucket.UploadPart(imur, fd, chunk.Size, chunk.Number)
if err != nil {
return result.Errorf(result.ErrUpload, "[File]Bucket part file %s", err)
}
parts = append(parts, part)
}
// 完成分片上传
cmur, err := bucket.CompleteMultipartUpload(imur, parts)
if err != nil {
return result.Errorf(result.ErrUpload, "[File]Bucket complete part %s", err)
}
logger.Info("Upload: Complete part %v", cmur)
return nil
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/h79/goutils.git
git@gitee.com:h79/goutils.git
h79
goutils
goutils
v1.21.2

搜索帮助

0d507c66 1850385 C8b1a773 1850385