storage.go 4.1 KB

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