storage.go 4.8 KB

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