storage.go 2.8 KB

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