storage.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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. "path"
  11. "strings"
  12. "time"
  13. )
  14. var _ QiNiu = (*qiNiu)(nil)
  15. type QiNiu interface {
  16. i()
  17. SetDefaultUploadTokenTTL(ttl uint64)
  18. GetCallbackUploadToken(ttl uint64, callbackURL string) string
  19. GetUploadToken(ttl uint64) string
  20. GetPrivateURL(key string, ttl uint64) string
  21. VerifyCallback(req *http.Request) (bool, error)
  22. UploadFile(key, localFile string) (*PutRet, error)
  23. ResumeUploadFile(key, localFile string) (*PutRet, error)
  24. DelFile(key string) error
  25. TimestampSecuritySign(path string, ttl time.Duration) string
  26. GetFileInfo(key string) *storage.FileInfo
  27. }
  28. type qiNiu struct {
  29. mac *qbox.Mac
  30. conf *storage.Config
  31. bucket string
  32. domain string
  33. securityKey string
  34. md5 md5.MD5
  35. uploadTokenTTL uint64
  36. }
  37. type PutRet struct {
  38. Key string `json:"key"`
  39. Hash string `json:"hash"`
  40. Fsize int `json:"fsize"`
  41. Fname string `json:"fname"`
  42. Ext string `json:"ext"`
  43. Unique string `json:"unique"`
  44. }
  45. func New(accessKey, secretKey, bucket, domain, securityKey string) QiNiu {
  46. region, _ := storage.GetRegion(accessKey, bucket)
  47. return &qiNiu{
  48. mac: qbox.NewMac(accessKey, secretKey),
  49. bucket: bucket,
  50. domain: domain,
  51. securityKey: securityKey,
  52. conf: &storage.Config{
  53. Region: region, //空间所在的存储区域
  54. UseHTTPS: true, //是否使用https域名
  55. UseCdnDomains: true, //上传是否使用CDN上传加速
  56. },
  57. md5: md5.New(),
  58. uploadTokenTTL: 3600,
  59. }
  60. }
  61. func (q *qiNiu) i() {}
  62. func (q *qiNiu) SetDefaultUploadTokenTTL(ttl uint64) {
  63. q.uploadTokenTTL = ttl
  64. }
  65. func (q *qiNiu) GetUploadToken(ttl uint64) string {
  66. putPolicy := storage.PutPolicy{
  67. Scope: q.bucket,
  68. Expires: ttl,
  69. }
  70. return putPolicy.UploadToken(q.mac)
  71. }
  72. func (q *qiNiu) GetCallbackUploadToken(ttl uint64, callbackURL string) string {
  73. putPolicy := storage.PutPolicy{
  74. Scope: q.bucket,
  75. CallbackURL: callbackURL,
  76. CallbackBody: `{"key":"$(key)","hash":"$(etag)","fname":"$(fname)","fsize":"$(fsize)","ext":"$(ext)","unique":"$(x:unique)"}`,
  77. CallbackBodyType: "application/json",
  78. Expires: ttl,
  79. }
  80. return putPolicy.UploadToken(q.mac)
  81. }
  82. func (q *qiNiu) GetPrivateURL(key string, ttl uint64) string {
  83. deadline := time.Now().Add(time.Second * time.Duration(ttl)).Unix()
  84. return storage.MakePrivateURL(q.mac, q.domain, key, deadline)
  85. }
  86. func (q *qiNiu) VerifyCallback(req *http.Request) (bool, error) {
  87. return q.mac.VerifyCallback(req)
  88. }
  89. func (q *qiNiu) UploadFile(key, localFile string) (*PutRet, error) {
  90. upToken := q.GetUploadToken(q.uploadTokenTTL)
  91. //构建表单上传的对象
  92. formUploader := storage.NewFormUploader(q.conf)
  93. //请求参数
  94. filename := path.Base(key)
  95. fileSuffix := path.Ext(key)
  96. filePrefix := filename[0 : len(filename)-len(fileSuffix)]
  97. putExtra := &storage.PutExtra{
  98. Params: map[string]string{"x:unique": filePrefix},
  99. }
  100. //自定义返回body
  101. ret := new(PutRet)
  102. err := formUploader.PutFile(context.Background(), ret, upToken, key, localFile, putExtra)
  103. if err != nil {
  104. return nil, err
  105. }
  106. return ret, nil
  107. }
  108. func (q *qiNiu) ResumeUploadFile(key, localFile string) (*PutRet, error) {
  109. upToken := q.GetUploadToken(q.uploadTokenTTL)
  110. //构建分片上传的对象
  111. resumeUploader := storage.NewResumeUploaderV2(q.conf)
  112. //请求参数
  113. filename := path.Base(key)
  114. fileSuffix := path.Ext(key)
  115. filePrefix := filename[0 : len(filename)-len(fileSuffix)]
  116. putExtra := &storage.RputV2Extra{
  117. CustomVars: map[string]string{"x:unique": filePrefix},
  118. }
  119. //自定义返回body
  120. ret := new(PutRet)
  121. err := resumeUploader.PutFile(context.Background(), ret, upToken, key, localFile, putExtra)
  122. if err != nil {
  123. return nil, err
  124. }
  125. return ret, nil
  126. }
  127. func (q *qiNiu) DelFile(key string) error {
  128. bucketManager := storage.NewBucketManager(q.mac, q.conf)
  129. err := bucketManager.Delete(q.bucket, key)
  130. if err != nil {
  131. return err
  132. }
  133. return nil
  134. }
  135. func (q *qiNiu) TimestampSecuritySign(path string, ttl time.Duration) string {
  136. sep := "/"
  137. path = strings.Trim(path, sep)
  138. splits := strings.Split(path, sep)
  139. for i, split := range splits {
  140. splits[i] = url.QueryEscape(split)
  141. }
  142. path = sep + strings.Join(splits, sep)
  143. unix := time.Now().Add(ttl).Unix()
  144. hex := fmt.Sprintf("%x", unix)
  145. encrypt := q.md5.Encrypt(q.securityKey + path + hex)
  146. param := make(url.Values)
  147. param.Set("sign", encrypt)
  148. param.Set("t", hex)
  149. return param.Encode()
  150. }
  151. func (q *qiNiu) GetFileInfo(key string) *storage.FileInfo {
  152. bucketManager := storage.NewBucketManager(q.mac, q.conf)
  153. fileInfo, sErr := bucketManager.Stat(q.bucket, key)
  154. if sErr != nil {
  155. return nil
  156. }
  157. return &fileInfo
  158. }