bfs.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. package dao
  2. import (
  3. "bytes"
  4. "context"
  5. "crypto/hmac"
  6. "crypto/sha1"
  7. "encoding/base64"
  8. "fmt"
  9. "hash"
  10. "io/ioutil"
  11. "net/http"
  12. "strconv"
  13. "time"
  14. "go-common/app/admin/main/member/conf"
  15. "go-common/library/ecode"
  16. "github.com/pkg/errors"
  17. )
  18. // UploadImage upload bfs.
  19. func (d *Dao) UploadImage(c context.Context, fileType string, bs []byte, bfsConf *conf.BFS) (location string, err error) {
  20. url := bfsConf.URL + bfsConf.Bucket + "/"
  21. req, err := http.NewRequest("PUT", url, bytes.NewBuffer(bs))
  22. if err != nil {
  23. err = errors.Wrap(err, " UploadImage http.NewRequest() failed")
  24. return
  25. }
  26. expire := time.Now().Unix()
  27. authorization := authorize(bfsConf.Key, bfsConf.Secret, "PUT", bfsConf.Bucket, "", expire)
  28. req.Header.Set("Host", url)
  29. req.Header.Add("Date", fmt.Sprint(expire))
  30. req.Header.Add("Authorization", authorization)
  31. req.Header.Add("Content-Type", fileType)
  32. // timeout
  33. ctx, cancel := context.WithTimeout(c, time.Duration(bfsConf.Timeout))
  34. req = req.WithContext(ctx)
  35. defer cancel()
  36. resp, err := d.bfsclient.Do(req)
  37. if err != nil {
  38. err = errors.Wrapf(ecode.BfsUploadServiceUnavailable, "d.bfsclient.Do error(%v) | url(%s)", err, url)
  39. return
  40. }
  41. if resp.StatusCode != http.StatusOK {
  42. err = errors.Errorf("UploadImage failed,http code:%d", resp.StatusCode)
  43. return
  44. }
  45. header := resp.Header
  46. code := header.Get("Code")
  47. if code != strconv.Itoa(http.StatusOK) {
  48. err = errors.Wrap(ecode.String(code), "UploadImage failed")
  49. return
  50. }
  51. location = header.Get("Location")
  52. return
  53. }
  54. // DelImage del bfs image.
  55. func (d *Dao) DelImage(c context.Context, fileName string, bfsConf *conf.BFS) (err error) {
  56. url := bfsConf.URL + bfsConf.Bucket + "/" + fileName
  57. req, err := http.NewRequest("DELETE", url, nil)
  58. if err != nil {
  59. err = errors.Wrap(err, "DelImage http.NewRequest() failed")
  60. return
  61. }
  62. expire := time.Now().Unix()
  63. authorization := authorize(bfsConf.Key, bfsConf.Secret, "DELETE", bfsConf.Bucket, fileName, expire)
  64. req.Header.Set("Host", url)
  65. req.Header.Add("Date", fmt.Sprint(expire))
  66. req.Header.Add("Authorization", authorization)
  67. // timeout
  68. ctx, cancel := context.WithTimeout(c, time.Duration(bfsConf.Timeout))
  69. req = req.WithContext(ctx)
  70. defer cancel()
  71. resp, err := d.bfsclient.Do(req)
  72. if err != nil {
  73. err = errors.Wrapf(ecode.BfsUploadServiceUnavailable, "d.bfsclient.Do error(%v) | url(%s)", err, url)
  74. return
  75. }
  76. if resp.StatusCode != http.StatusOK {
  77. err = errors.Errorf("DelImage failed,http code:%d", resp.StatusCode)
  78. return
  79. }
  80. header := resp.Header
  81. code := header.Get("Code")
  82. if code != strconv.Itoa(http.StatusOK) {
  83. err = errors.Errorf("DelImage failed,res code:%s", code)
  84. return
  85. }
  86. return
  87. }
  88. // authorize returns authorization for upload file to bfs
  89. func authorize(key, secret, method, bucket, fileName string, expire int64) (authorization string) {
  90. var (
  91. content string
  92. mac hash.Hash
  93. signature string
  94. )
  95. content = fmt.Sprintf("%s\n%s\n%s\n%d\n", method, bucket, fileName, expire)
  96. mac = hmac.New(sha1.New, []byte(secret))
  97. mac.Write([]byte(content))
  98. signature = base64.StdEncoding.EncodeToString(mac.Sum(nil))
  99. authorization = fmt.Sprintf("%s:%s:%d", key, signature, expire)
  100. return
  101. }
  102. //Image image.
  103. func (d *Dao) Image(url string) ([]byte, error) {
  104. resp, err := d.bfsclient.Get(url)
  105. if err != nil {
  106. return nil, err
  107. }
  108. if resp.StatusCode != http.StatusOK {
  109. return nil, errors.Errorf("Image url(%s) resp.StatusCode code(%v)", url, resp.StatusCode)
  110. }
  111. defer resp.Body.Close()
  112. return ioutil.ReadAll(resp.Body)
  113. }