package lib // 存储相关功能的引入包只有这两个,后面不再赘述 // 存储相关功能的引入包只有这两个,后面不再赘述 import ( "context" "fmt" "github.com/qiniu/go-sdk/v7/auth/qbox" "github.com/qiniu/go-sdk/v7/storage" ) var mac *qbox.Mac var ( //BUCKET是你在存储空间的名称 accessKey = "-8ezB_d-8-eUFTMvhOGbGzgeQRPeKQnaQ3DBcUxo" secretKey = "KFhkYxTAJ2ZPN3ZS3euTsfWk8-C92rKgkhAMkDRN" BUCKET = "bzdcoldoss" ) // 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 } bucket := BUCKET //key := "your file save key" // 使用 returnBody 自定义回复格式 putPolicy := storage.PutPolicy{ Scope: bucket, ReturnBody: `{"key":"$(key)","hash":"$(etag)","fsize":$(fsize),"bucket":"$(bucket)","name":"$(x:name)"}`, } mac := qbox.NewMac(accessKey, secretKey) upToken := putPolicy.UploadToken(mac) 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 { fmt.Println(err) return false } fmt.Println(ret.Bucket, ret.Key, ret.Fsize, ret.Hash, ret.Name) return true }