storage.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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, ttl time.Duration) string
  22. GetFileInfo(key string) *storage.FileInfo
  23. }
  24. type qiNiu struct {
  25. mac *qbox.Mac
  26. bucket string
  27. domain string
  28. securityKey string
  29. md5 md5.MD5
  30. }
  31. type PutRet struct {
  32. Key string `json:"key"`
  33. Hash string `json:"hash"`
  34. Fsize int `json:"fsize"`
  35. }
  36. func New(accessKey, secretKey, bucket, domain, securityKey string) QiNiu {
  37. return &qiNiu{
  38. mac: qbox.NewMac(accessKey, secretKey),
  39. bucket: bucket,
  40. domain: domain,
  41. securityKey: securityKey,
  42. md5: md5.New(),
  43. }
  44. }
  45. func (q *qiNiu) i() {}
  46. func (q *qiNiu) GetUploadToken(ttl uint64) string {
  47. putPolicy := storage.PutPolicy{
  48. Scope: q.bucket,
  49. Expires: ttl,
  50. }
  51. return putPolicy.UploadToken(q.mac)
  52. }
  53. func (q *qiNiu) GetPrivateURL(key string, ttl uint64) string {
  54. deadline := time.Now().Add(time.Second * time.Duration(ttl)).Unix()
  55. return storage.MakePrivateURL(q.mac, q.domain, key, deadline)
  56. }
  57. func (q *qiNiu) VerifyCallback(req *http.Request) (bool, error) {
  58. return q.mac.VerifyCallback(req)
  59. }
  60. func (q *qiNiu) UploadFile(key, localFile string, zone *storage.Region) (*storage.PutRet, error) {
  61. upToken := q.GetUploadToken(60)
  62. cfg := storage.Config{
  63. Zone: zone, //空间对应的机房
  64. UseHTTPS: true, //是否使用https域名
  65. UseCdnDomains: true, //上传是否使用CDN上传加速
  66. }
  67. //构建表单上传的对象
  68. formUploader := storage.NewFormUploader(&cfg)
  69. ret := &storage.PutRet{}
  70. // 可选配置
  71. putExtra := storage.PutExtra{}
  72. err := formUploader.PutFile(context.Background(), ret, upToken, key, localFile, &putExtra)
  73. if err != nil {
  74. return nil, err
  75. }
  76. return ret, nil
  77. }
  78. func (q *qiNiu) DelFile(key string, zone *storage.Region) error {
  79. cfg := storage.Config{
  80. Zone: zone, //空间对应的机房
  81. UseHTTPS: true, //是否使用https域名
  82. UseCdnDomains: true, //上传是否使用CDN上传加速
  83. }
  84. bucketManager := storage.NewBucketManager(q.mac, &cfg)
  85. err := bucketManager.Delete(q.bucket, key)
  86. if err != nil {
  87. return err
  88. }
  89. return nil
  90. }
  91. func (q *qiNiu) TimestampSecuritySign(path string, ttl time.Duration) string {
  92. sep := "/"
  93. path = strings.Trim(path, sep)
  94. splits := strings.Split(path, sep)
  95. for i, split := range splits {
  96. splits[i] = url.QueryEscape(split)
  97. }
  98. path = sep + strings.Join(splits, sep)
  99. unix := time.Now().Add(ttl).Unix()
  100. hex := fmt.Sprintf("%x", unix)
  101. encrypt := q.md5.Encrypt(q.securityKey + path + hex)
  102. param := make(url.Values)
  103. param.Set("sign", encrypt)
  104. param.Set("t", hex)
  105. return param.Encode()
  106. }
  107. func (q *qiNiu) GetFileInfo(key string) *storage.FileInfo {
  108. cfg := storage.Config{
  109. UseHTTPS: true, // 是否使用https域名进行资源管理
  110. }
  111. bucketManager := storage.NewBucketManager(q.mac, &cfg)
  112. fileInfo, sErr := bucketManager.Stat(q.bucket, key)
  113. if sErr != nil {
  114. return nil
  115. }
  116. return &fileInfo
  117. }