kodo.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. package file_store
  2. import (
  3. "context"
  4. "fmt"
  5. "github.com/qiniu/go-sdk/v7/auth/qbox"
  6. "github.com/qiniu/go-sdk/v7/storage"
  7. )
  8. type Zone string
  9. const (
  10. // HuaDong 华东
  11. HuaDong Zone = "HuaDong"
  12. // HuaBei 华北
  13. HuaBei Zone = "HuaBei"
  14. // HuaNan 华南
  15. HuaNan Zone = "HuaNan"
  16. // BeiMei 北美
  17. BeiMei Zone = "BeiMei"
  18. // XinJiaPo 新加坡
  19. XinJiaPo Zone = "XinJiaPo"
  20. )
  21. type QiNiuKODO struct {
  22. Client interface{}
  23. BucketName string
  24. cfg storage.Config
  25. options []ClientOption
  26. }
  27. func (e *QiNiuKODO) getToken() string {
  28. putPolicy := storage.PutPolicy{
  29. Scope: e.BucketName,
  30. }
  31. if len(e.options) > 0 && e.options[0]["Expires"] != nil {
  32. putPolicy.Expires = e.options[0]["Expires"].(uint64)
  33. }
  34. upToken := putPolicy.UploadToken(e.Client.(*qbox.Mac))
  35. return upToken
  36. }
  37. //Setup 装载
  38. //endpoint sss
  39. func (e *QiNiuKODO) Setup(endpoint, accessKeyID, accessKeySecret, BucketName string, options ...ClientOption) error {
  40. mac := qbox.NewMac(accessKeyID, accessKeySecret)
  41. // 获取存储空间。
  42. cfg := storage.Config{}
  43. // 空间对应的机房
  44. e.setZoneORDefault(cfg, options...)
  45. // 是否使用https域名
  46. cfg.UseHTTPS = true
  47. // 上传是否使用CDN上传加速
  48. cfg.UseCdnDomains = false
  49. e.Client = mac
  50. e.BucketName = BucketName
  51. e.cfg = cfg
  52. e.options = options
  53. return nil
  54. }
  55. // setZoneORDefault 设置Zone或者默认华东
  56. func (e *QiNiuKODO) setZoneORDefault(cfg storage.Config, options ...ClientOption) {
  57. if len(options) > 0 && options[0]["Zone"] != nil {
  58. if _, ok := options[0]["Zone"].(Zone); !ok {
  59. cfg.Zone = &storage.ZoneHuadong
  60. }
  61. switch options[0]["Zone"].(Zone) {
  62. case HuaDong:
  63. cfg.Zone = &storage.ZoneHuadong
  64. case HuaBei:
  65. cfg.Zone = &storage.ZoneHuabei
  66. case HuaNan:
  67. cfg.Zone = &storage.ZoneHuanan
  68. case BeiMei:
  69. cfg.Zone = &storage.ZoneBeimei
  70. case XinJiaPo:
  71. cfg.Zone = &storage.ZoneXinjiapo
  72. default:
  73. cfg.Zone = &storage.ZoneHuadong
  74. }
  75. }
  76. }
  77. // UpLoad 文件上传
  78. func (e *QiNiuKODO) UpLoad(yourObjectName string, localFile interface{}) error {
  79. // 构建表单上传的对象
  80. formUploader := storage.NewFormUploader(&e.cfg)
  81. ret := storage.PutRet{}
  82. // 可选配置
  83. putExtra := storage.PutExtra{
  84. Params: map[string]string{
  85. "x:name": "github logo",
  86. },
  87. }
  88. err := formUploader.PutFile(context.Background(), &ret, e.getToken(), yourObjectName, localFile.(string), &putExtra)
  89. if err != nil {
  90. fmt.Println(err)
  91. return err
  92. }
  93. fmt.Println(ret.Key, ret.Hash)
  94. return nil
  95. }
  96. func (e *QiNiuKODO) GetTempToken() (string, error) {
  97. token := e.getToken()
  98. return token, nil
  99. }