storage.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. package qiniu
  2. import (
  3. "context"
  4. "fmt"
  5. "git.bvbej.com/bvbej/base-golang/pkg/md5"
  6. "github.com/qiniu/go-sdk/v7/auth/qbox"
  7. "github.com/qiniu/go-sdk/v7/storage"
  8. "net/http"
  9. "net/url"
  10. "strings"
  11. "time"
  12. )
  13. var _ QiNiu = (*qiNiu)(nil)
  14. type QiNiu interface {
  15. i()
  16. GetUploadToken(ttl uint64) string
  17. GetPrivateURL(key string, ttl uint64) string
  18. VerifyCallback(req *http.Request) (bool, error)
  19. UploadFile(key, localFile string, zone *storage.Region) (*storage.PutRet, error)
  20. DelFile(key string, zone *storage.Region) error
  21. TimestampSecuritySign(path string) string
  22. }
  23. type qiNiu struct {
  24. mac *qbox.Mac
  25. bucket string
  26. domain string
  27. securityKey string
  28. md5 md5.MD5
  29. }
  30. type PutRet struct {
  31. Key string `json:"key"`
  32. Hash string `json:"hash"`
  33. Fsize int `json:"fsize"`
  34. }
  35. func New(accessKey, secretKey, bucket, domain, securityKey string) QiNiu {
  36. return &qiNiu{
  37. mac: qbox.NewMac(accessKey, secretKey),
  38. bucket: bucket,
  39. domain: domain,
  40. securityKey: securityKey,
  41. md5: md5.New(),
  42. }
  43. }
  44. func (q *qiNiu) i() {}
  45. func (q *qiNiu) GetUploadToken(ttl uint64) string {
  46. putPolicy := storage.PutPolicy{
  47. Scope: q.bucket,
  48. Expires: ttl,
  49. }
  50. return putPolicy.UploadToken(q.mac)
  51. }
  52. func (q *qiNiu) GetPrivateURL(key string, ttl uint64) string {
  53. deadline := time.Now().Add(time.Second * time.Duration(ttl)).Unix()
  54. return storage.MakePrivateURL(q.mac, q.domain, key, deadline)
  55. }
  56. func (q *qiNiu) VerifyCallback(req *http.Request) (bool, error) {
  57. return q.mac.VerifyCallback(req)
  58. }
  59. func (q *qiNiu) UploadFile(key, localFile string, zone *storage.Region) (*storage.PutRet, error) {
  60. upToken := q.GetUploadToken(60)
  61. cfg := storage.Config{
  62. Zone: zone, //空间对应的机房
  63. UseHTTPS: true, //是否使用https域名
  64. UseCdnDomains: true, //上传是否使用CDN上传加速
  65. }
  66. //构建表单上传的对象
  67. formUploader := storage.NewFormUploader(&cfg)
  68. ret := &storage.PutRet{}
  69. // 可选配置
  70. putExtra := storage.PutExtra{}
  71. err := formUploader.PutFile(context.Background(), ret, upToken, key, localFile, &putExtra)
  72. if err != nil {
  73. return nil, err
  74. }
  75. return ret, nil
  76. }
  77. func (q *qiNiu) DelFile(key string, zone *storage.Region) error {
  78. cfg := storage.Config{
  79. Zone: zone, //空间对应的机房
  80. UseHTTPS: true, //是否使用https域名
  81. UseCdnDomains: true, //上传是否使用CDN上传加速
  82. }
  83. bucketManager := storage.NewBucketManager(q.mac, &cfg)
  84. err := bucketManager.Delete(q.bucket, key)
  85. if err != nil {
  86. return err
  87. }
  88. return nil
  89. }
  90. func (q *qiNiu) TimestampSecuritySign(path string, ttl time.Duration) string {
  91. sep := "/"
  92. path = strings.Trim(path, sep)
  93. splits := strings.Split(path, sep)
  94. for i, split := range splits {
  95. splits[i] = url.QueryEscape(split)
  96. }
  97. path = sep + strings.Join(splits, sep)
  98. unix := time.Now().Add(ttl).Unix()
  99. hex := fmt.Sprintf("%x", unix)
  100. encrypt := q.md5.Encrypt(q.securityKey + path + hex)
  101. param := make(url.Values)
  102. param.Set("sign", encrypt)
  103. param.Set("t", hex)
  104. return param.Encode()
  105. }