| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175 |
- package lib
- // 存储相关功能的引入包只有这两个,后面不再赘述
- import (
- "Cold_Api/conf"
- "context"
- "fmt"
- "io"
- "mime/multipart"
- "os"
- "path/filepath"
- "strconv"
- "time"
- "github.com/beego/beego/v2/core/logs"
- "github.com/qiniu/go-sdk/v7/auth/qbox"
- "github.com/qiniu/go-sdk/v7/storage"
- uuid "github.com/satori/go.uuid"
- )
- var Qiniu *qbox.Mac
- // var (
- //
- // //BUCKET是你在存储空间的名称
- // accessKey = "-8ezB_d-8-eUFTMvhOGbGzgeQRPeKQnaQ3DBcUxo"
- // secretKey = "KFhkYxTAJ2ZPN3ZS3euTsfWk8-C92rKgkhAMkDRN"
- // BUCKET = "bzdcoldoss"
- //
- // )
- func init() {
- Qiniu = qbox.NewMac(conf.Qiniu_AccessKey, conf.Qiniu_SecretKey)
- }
- // if !lib.Pload_qiniu("ofile/"+timeStr+".xlsx","ofile/"+timeStr+".xlsx"){
- // c.Data["json"] = lib.JSONS{Code: 202, Msg: "oss!"}
- // c.ServeJSON()
- // return
- // }
- func Pload_qiniu(localFile string, name string) bool {
- //localFile := "C:\\Users\\Administrator\\Downloads\\kodo-browser-Windows-x64-v1.0.15.zip"
- //key := "kodo-browser-Windows-x64-v1.0.15.zip"
- // 自定义返回值结构体
- type MyPutRet struct {
- Key string
- Hash string
- Fsize int
- Bucket string
- Name string
- }
- //key := "your file save key"
- // 使用 returnBody 自定义回复格式
- putPolicy := storage.PutPolicy{
- Scope: conf.Qiniu_BUCKET,
- ReturnBody: `{"key":"$(key)","hash":"$(etag)","fsize":$(fsize),"bucket":"$(bucket)","name":"$(x:name)"}`,
- }
- upToken := putPolicy.UploadToken(Qiniu)
- cfg := storage.Config{}
- formUploader := storage.NewFormUploader(&cfg)
- ret := MyPutRet{}
- putExtra := storage.PutExtra{
- Params: map[string]string{
- "x:name": "github logo",
- },
- }
- err := formUploader.PutFile(context.Background(), &ret, upToken, name, localFile, &putExtra)
- if err != nil {
- logs.Error("Pload_qiniu", err)
- return false
- }
- logs.Info("七牛云", ret.Bucket, ret.Key, ret.Fsize, ret.Hash, ret.Name)
- return true
- }
- func UploadToken(T_suffix string) string {
- Tokey := strconv.FormatInt(time.Now().Unix(), 10) + uuid.NewV4().String()
- if len(T_suffix) == 0 {
- T_suffix = "png"
- }
- putPolicy := storage.PutPolicy{
- Scope: conf.Qiniu_BUCKET,
- InsertOnly: 1, // 仅能以新增模式上传文件。
- Expires: 7200, //示例2小时有效期
- ReturnBody: `{"key":"` + conf.OssQiniu + `/$(key)","hash":"$(etag)","fsize":$(fsize),"bucket":"$(bucket)"}`,
- //{"key":"github-x.png","hash":"FqKXVdTvIx_mPjOYdjDyUSy_H1jr","fsize":6091,"bucket":"if-pbl","name":"github logo"}
- //{"key":"` + conf.Oss + `/$(key)","hash":"$(etag)","fsize":$(fsize),"bucket":"$(bucket)","name":"$(x:name)"}
- ForceSaveKey: true,
- SaveKey: "UpImage/" + Tokey + "." + T_suffix,
- FsizeLimit: 1024 * 1024 * 500, // 500M 文件大小限制,后续可注释此行不限制大小
- // MimeLimit: "image/*;application/pdf;application/octet-stream;application/zip;application/x-tar;application/msword;video/mp4", // 增加MP4格式支持,后续可注释此行不限制类型
- }
- upToken := putPolicy.UploadToken(Qiniu)
- return upToken
- }
- // UploadFileToQiniu 公共文件上传方法,上传文件到七牛云
- func UploadFileToQiniu(file *multipart.FileHeader, keyPrefix string) (string, error) {
- // 打开文件
- src, err := file.Open()
- if err != nil {
- return "", err
- }
- defer src.Close()
- // 生成唯一文件名
- var key string
- if keyPrefix == "" {
- key = "uploads/" + strconv.FormatInt(time.Now().Unix(), 10) + "_" + file.Filename
- } else {
- key = keyPrefix + strconv.FormatInt(time.Now().Unix(), 10) + "_" + file.Filename
- }
- // 创建临时文件
- tempDir := os.TempDir()
- tempFile := filepath.Join(tempDir, filepath.Base(key))
- // 确保目录存在
- dir := filepath.Dir(tempFile)
- if err := os.MkdirAll(dir, 0755); err != nil {
- return "", fmt.Errorf("创建临时目录失败: %v", err)
- }
- // 将上传的文件写入临时文件
- dst, err := os.Create(tempFile)
- if err != nil {
- return "", fmt.Errorf("创建临时文件失败: %v", err)
- }
- defer dst.Close()
- defer os.Remove(tempFile) // 上传后删除临时文件
- _, err = io.Copy(dst, src)
- if err != nil {
- return "", fmt.Errorf("写入临时文件失败: %v", err)
- }
- // 直接使用七牛云SDK上传
- return uploadFileDirectToQiniu(tempFile, key)
- }
- // uploadFileDirectToQiniu 直接上传文件到七牛云
- func uploadFileDirectToQiniu(localFile string, key string) (string, error) {
- // 自定义返回值结构体
- type QiniuUploadResult struct {
- Key string `json:"key"`
- Hash string `json:"hash"`
- Fsize int64 `json:"fsize"`
- Bucket string `json:"bucket"`
- }
- // 使用 returnBody 自定义回复格式
- putPolicy := storage.PutPolicy{
- Scope: conf.Qiniu_BUCKET,
- ReturnBody: `{"key":"$(key)","hash":"$(etag)","fsize":$(fsize),"bucket":"$(bucket)"}`,
- FsizeLimit: 1024 * 1024 * 500, // 500M 文件大小限制,后续可注释此行不限制大小
- // MimeLimit: "image/*;application/pdf;application/octet-stream;application/zip;application/x-tar;application/msword;video/mp4", // 增加MP4格式支持,后续可注释此行不限制类型
- }
- upToken := putPolicy.UploadToken(Qiniu)
- cfg := storage.Config{}
- formUploader := storage.NewFormUploader(&cfg)
- ret := QiniuUploadResult{}
- putExtra := storage.PutExtra{}
- err := formUploader.PutFile(context.Background(), &ret, upToken, key, localFile, &putExtra)
- if err != nil {
- logs.Error("UploadFileToQiniu", "上传文件失败 "+localFile, err.Error())
- return "", fmt.Errorf("上传文件到七牛云失败: %v", err)
- }
- logs.Info("七牛云上传成功", ret.Bucket, ret.Key, ret.Fsize, ret.Hash)
- return conf.Qiniu_Url + ret.Key, nil
- }
|