Qiniu.go 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. package lib
  2. // 存储相关功能的引入包只有这两个,后面不再赘述
  3. // 存储相关功能的引入包只有这两个,后面不再赘述
  4. import (
  5. "context"
  6. "fmt"
  7. "github.com/qiniu/go-sdk/v7/auth/qbox"
  8. "github.com/qiniu/go-sdk/v7/storage"
  9. )
  10. var mac *qbox.Mac
  11. var (
  12. //BUCKET是你在存储空间的名称
  13. accessKey = "-8ezB_d-8-eUFTMvhOGbGzgeQRPeKQnaQ3DBcUxo"
  14. secretKey = "KFhkYxTAJ2ZPN3ZS3euTsfWk8-C92rKgkhAMkDRN"
  15. BUCKET = "bzdcoldoss"
  16. )
  17. // if !lib.Pload_qiniu("ofile/"+timeStr+".xlsx","ofile/"+timeStr+".xlsx"){
  18. // c.Data["json"] = lib.JSONS{Code: 203, Msg: "oss!"}
  19. // c.ServeJSON()
  20. // return
  21. // }
  22. func Pload_qiniu(localFile string, name string) bool {
  23. //localFile := "C:\\Users\\Administrator\\Downloads\\kodo-browser-Windows-x64-v1.0.15.zip"
  24. //key := "kodo-browser-Windows-x64-v1.0.15.zip"
  25. // 自定义返回值结构体
  26. type MyPutRet struct {
  27. Key string
  28. Hash string
  29. Fsize int
  30. Bucket string
  31. Name string
  32. }
  33. bucket := BUCKET
  34. //key := "your file save key"
  35. // 使用 returnBody 自定义回复格式
  36. putPolicy := storage.PutPolicy{
  37. Scope: bucket,
  38. ReturnBody: `{"key":"$(key)","hash":"$(etag)","fsize":$(fsize),"bucket":"$(bucket)","name":"$(x:name)"}`,
  39. }
  40. mac := qbox.NewMac(accessKey, secretKey)
  41. upToken := putPolicy.UploadToken(mac)
  42. cfg := storage.Config{}
  43. formUploader := storage.NewFormUploader(&cfg)
  44. ret := MyPutRet{}
  45. putExtra := storage.PutExtra{
  46. Params: map[string]string{
  47. "x:name": "github logo",
  48. },
  49. }
  50. err := formUploader.PutFile(context.Background(), &ret, upToken, name, localFile, &putExtra)
  51. if err != nil {
  52. fmt.Println(err)
  53. return false
  54. }
  55. fmt.Println(ret.Bucket, ret.Key, ret.Fsize, ret.Hash, ret.Name)
  56. return true
  57. }