12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091 |
- package lib
- // 存储相关功能的引入包只有这两个,后面不再赘述
- import (
- "Cold_Api/conf"
- "github.com/beego/beego/v2/core/logs"
- "context"
- "github.com/qiniu/go-sdk/v7/auth/qbox"
- "github.com/qiniu/go-sdk/v7/storage"
- uuid "github.com/satori/go.uuid"
- "strconv"
- "time"
- )
- 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 * 100,
- MimeLimit: "image/*;application/pdf;application/octet-stream;application/zip;application/x-tar",
- }
- upToken := putPolicy.UploadToken(Qiniu)
- return upToken
- }
|