service-minio.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. package api
  2. import (
  3. "context"
  4. "fmt"
  5. "log"
  6. "mesh/conf"
  7. "path"
  8. "strings"
  9. "time"
  10. "github.com/gin-gonic/gin"
  11. "github.com/minio/minio-go/v7"
  12. "github.com/minio/minio-go/v7/pkg/credentials"
  13. )
  14. func createMinioClient(conf *conf.AppConf) *minio.Client {
  15. endpoint := conf.Minio.Endpoint
  16. accessKeyID := conf.Minio.AccessKey
  17. secretAccessKey := conf.Minio.AccessSec
  18. useSSL := false
  19. // Initialize minio client object.
  20. minioClient, err := minio.New(endpoint, &minio.Options{
  21. Creds: credentials.NewStaticV4(accessKeyID, secretAccessKey, ""),
  22. Secure: useSSL,
  23. })
  24. if err != nil {
  25. log.Fatalln(err)
  26. return nil
  27. }
  28. return minioClient
  29. }
  30. func MinioCreateUserPolicy(c *gin.Context, apictx *ApiSession) (interface{}, error) {
  31. body := struct {
  32. Key string
  33. }{}
  34. err := c.ShouldBindJSON(&body)
  35. if err != nil {
  36. return nil, NewError("参数解析错误!")
  37. }
  38. if len(body.Key) < 1 {
  39. return nil, NewError("Object Key不能为空")
  40. }
  41. // Initialize policy condition config.
  42. policy := minio.NewPostPolicy()
  43. // Apply upload policy restrictions:
  44. policy.SetBucket(apictx.Svc.Conf.Minio.Bucket)
  45. fkey := body.Key //fmt.Sprintf("u/%s/%s", apictx.User.Parent, body.Key)
  46. policy.SetKey(fkey)
  47. policy.SetExpires(time.Now().UTC().Add(time.Second * 600)) // expires in 10 days
  48. // Only allow 'png' images.
  49. // policy.SetContentType("image/png")
  50. // Only allow content size in range 1KB to 1MB.
  51. policy.SetContentLengthRange(0, 5*1024*1024)
  52. // Add a user metadata using the key "custom" and value "user"
  53. // policy.SetUserMetadata("custom", "user")
  54. client := createMinioClient(apictx.Svc.Conf)
  55. url, err := client.PresignedPutObject(
  56. context.Background(), apictx.Svc.Conf.Minio.Bucket, fkey, time.Second*600,
  57. )
  58. //_, formdata, err := client.PresignedPostPolicy(context.Background(), policy)
  59. outUrl := fmt.Sprintf("%s://%s%s?%s", url.Scheme, url.Host, url.Path, url.RawQuery)
  60. if err != nil {
  61. return nil, err
  62. }
  63. out := map[string]interface{}{
  64. "uploadUrl": outUrl,
  65. "url": fmt.Sprintf("%s/%s/%s", apictx.Svc.Conf.Minio.PubHost, apictx.Svc.Conf.Minio.Bucket, fkey),
  66. "saveType": conf.SaveType_Minio,
  67. }
  68. return out, nil
  69. }
  70. func MinioUploadFile(client *minio.Client, bucketName string, fpath string, key string) (string, int64) {
  71. info, err := client.FPutObject(context.Background(), bucketName, key, fpath, minio.PutObjectOptions{})
  72. fmt.Println(info)
  73. if err != nil {
  74. return "", 0
  75. }
  76. return info.Key, info.Size
  77. }
  78. func MinioUploadLocalFile(client *minio.Client, bucketName string, fpath string, minioDir string) (string, int64) {
  79. _, name := path.Split(fpath)
  80. keyFormat := "%s/%s"
  81. if strings.HasSuffix(minioDir, "/") {
  82. keyFormat = "%s%s"
  83. }
  84. minioKey := fmt.Sprintf(keyFormat, minioDir, name)
  85. opts := minio.PutObjectOptions{}
  86. fpathExt := path.Ext(fpath)
  87. isGzFile := fpathExt == ".gz"
  88. if isGzFile {
  89. opts.ContentType = "text/plain"
  90. opts.ContentEncoding = "gzip"
  91. }
  92. info, err := client.FPutObject(context.Background(), bucketName, minioKey, fpath, opts)
  93. fmt.Println(info)
  94. if err != nil {
  95. info, err = client.FPutObject(context.Background(), bucketName, minioKey, fpath, opts)
  96. if err != nil {
  97. return "", 0
  98. }
  99. }
  100. return minioKey, info.Size
  101. }