bfs.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. package dao
  2. import (
  3. "bytes"
  4. "context"
  5. "crypto/hmac"
  6. "crypto/sha1"
  7. "encoding/base64"
  8. "fmt"
  9. "hash"
  10. "net/http"
  11. "strconv"
  12. "time"
  13. "go-common/library/ecode"
  14. "go-common/library/log"
  15. )
  16. const (
  17. _bucketName = "melloi"
  18. _melloiURI = "/bfs/" + _bucketName
  19. )
  20. // UploadImg upload img
  21. func (d *Dao) UploadImg(c context.Context, imgContent []byte, imgName string) (location string, err error) {
  22. var (
  23. req *http.Request
  24. reqURL = d.c.BfsConf.Host + _melloiURI + "/" + imgName
  25. resp *http.Response
  26. code int
  27. )
  28. buf := new(bytes.Buffer)
  29. if _, err = buf.Write(imgContent); err != nil {
  30. log.Error("Upload.buf.Write.error(%v)", err)
  31. return
  32. }
  33. if req, err = http.NewRequest("PUT", reqURL, buf); err != nil {
  34. err = ecode.RequestErr
  35. return
  36. }
  37. authorization := d.Authorize(d.c.BfsConf.AccessKey, d.c.BfsConf.AccessSecret, http.MethodPut, _bucketName, imgName, time.Now().Unix())
  38. req.Header.Set("Authorization", authorization)
  39. req.Header.Set("Content-Type", "image/png")
  40. if resp, err = d.client.Do(req); err != nil {
  41. log.Error("Upload.client.Do.error(%v)", err)
  42. return
  43. }
  44. defer resp.Body.Close()
  45. switch resp.StatusCode {
  46. case http.StatusOK:
  47. case http.StatusBadRequest:
  48. err = ecode.RequestErr
  49. return
  50. case http.StatusUnauthorized:
  51. // 验证不通过
  52. err = ecode.BfsUploadAuthErr
  53. return
  54. case http.StatusRequestEntityTooLarge:
  55. err = ecode.FileTooLarge
  56. return
  57. case http.StatusNotFound:
  58. err = ecode.NothingFound
  59. return
  60. case http.StatusMethodNotAllowed:
  61. err = ecode.MethodNotAllowed
  62. return
  63. case http.StatusServiceUnavailable:
  64. err = ecode.BfsUploadServiceUnavailable
  65. return
  66. case http.StatusInternalServerError:
  67. err = ecode.ServerErr
  68. return
  69. default:
  70. err = ecode.BfsUploadStatusErr
  71. return
  72. }
  73. if code, err = strconv.Atoi(resp.Header.Get("code")); err != nil || code != 200 {
  74. err = ecode.BfsUploadCodeErr
  75. return
  76. }
  77. location = resp.Header.Get("location")
  78. return
  79. }
  80. // Authorize .
  81. func (d *Dao) Authorize(key, secret, method, bucket, fileName string, expire int64) (authorization string) {
  82. var (
  83. content string
  84. mac hash.Hash
  85. signature string
  86. )
  87. content = fmt.Sprintf("%s\n%s\n%s\n%d\n", method, bucket, fileName, expire)
  88. mac = hmac.New(sha1.New, []byte(secret))
  89. mac.Write([]byte(content))
  90. signature = base64.StdEncoding.EncodeToString(mac.Sum(nil))
  91. authorization = fmt.Sprintf("%s:%s:%d", key, signature, expire)
  92. return
  93. }