1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495 |
- package lib
- // 存储相关功能的引入包只有这两个,后面不再赘述
- // 存储相关功能的引入包只有这两个,后面不再赘述
- import (
- "ColdVerify_server/conf"
- "ColdVerify_server/logs"
- "ColdVerify_server/models/System"
- "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: 203, 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(FuncName(), err)
- System.Add_Logs("七牛云", "上传文件失败"+localFile, err.Error())
- return false
- }
- logs.Println(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 * 30,
- MimeLimit: "image/*;application/pdf",
- }
- upToken := putPolicy.UploadToken(Qiniu)
- return upToken
- }
|