2
0

minio_oss.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. package upload
  2. import (
  3. "bytes"
  4. "context"
  5. "errors"
  6. "io"
  7. "mime/multipart"
  8. "path/filepath"
  9. "strings"
  10. "time"
  11. "github.com/flipped-aurora/gin-vue-admin/server/global"
  12. "github.com/flipped-aurora/gin-vue-admin/server/utils"
  13. "github.com/minio/minio-go/v7"
  14. "github.com/minio/minio-go/v7/pkg/credentials"
  15. "go.uber.org/zap"
  16. )
  17. var MinioClient *Minio // 优化性能,但是不支持动态配置
  18. type Minio struct {
  19. Client *minio.Client
  20. bucket string
  21. }
  22. func GetMinio(endpoint, accessKeyID, secretAccessKey, bucketName string, useSSL bool) (*Minio, error) {
  23. if MinioClient != nil {
  24. return MinioClient, nil
  25. }
  26. // Initialize minio client object.
  27. minioClient, err := minio.New(endpoint, &minio.Options{
  28. Creds: credentials.NewStaticV4(accessKeyID, secretAccessKey, ""),
  29. Secure: useSSL, // Set to true if using https
  30. })
  31. if err != nil {
  32. return nil, err
  33. }
  34. // 尝试创建bucket
  35. err = minioClient.MakeBucket(context.Background(), bucketName, minio.MakeBucketOptions{})
  36. if err != nil {
  37. // Check to see if we already own this bucket (which happens if you run this twice)
  38. exists, errBucketExists := minioClient.BucketExists(context.Background(), bucketName)
  39. if errBucketExists == nil && exists {
  40. // log.Printf("We already own %s\n", bucketName)
  41. } else {
  42. return nil, err
  43. }
  44. }
  45. MinioClient = &Minio{Client: minioClient, bucket: bucketName}
  46. return MinioClient, nil
  47. }
  48. func (m *Minio) UploadFile(file *multipart.FileHeader) (filePathres, key string, uploadErr error) {
  49. f, openError := file.Open()
  50. // mutipart.File to os.File
  51. if openError != nil {
  52. global.GVA_LOG.Error("function file.Open() Failed", zap.Any("err", openError.Error()))
  53. return "", "", errors.New("function file.Open() Failed, err:" + openError.Error())
  54. }
  55. filecontent := bytes.Buffer{}
  56. _, err := io.Copy(&filecontent, f)
  57. if err != nil {
  58. global.GVA_LOG.Error("读取文件失败", zap.Any("err", err.Error()))
  59. return "", "", errors.New("读取文件失败, err:" + err.Error())
  60. }
  61. f.Close() // 创建文件 defer 关闭
  62. // 对文件名进行加密存储
  63. ext := filepath.Ext(file.Filename)
  64. filename := utils.MD5V([]byte(strings.TrimSuffix(file.Filename, ext))) + ext
  65. if global.GVA_CONFIG.Minio.BasePath == "" {
  66. filePathres = "uploads" + "/" + time.Now().Format("2006-01-02") + "/" + filename
  67. } else {
  68. filePathres = global.GVA_CONFIG.Minio.BasePath + "/" + time.Now().Format("2006-01-02") + "/" + filename
  69. }
  70. // 设置超时10分钟
  71. ctx, cancel := context.WithTimeout(context.Background(), time.Minute*10)
  72. defer cancel()
  73. // Upload the file with PutObject 大文件自动切换为分片上传
  74. info, err := m.Client.PutObject(ctx, global.GVA_CONFIG.Minio.BucketName, filePathres, &filecontent, file.Size, minio.PutObjectOptions{ContentType: "application/octet-stream"})
  75. if err != nil {
  76. global.GVA_LOG.Error("上传文件到minio失败", zap.Any("err", err.Error()))
  77. return "", "", errors.New("上传文件到minio失败, err:" + err.Error())
  78. }
  79. return global.GVA_CONFIG.Minio.BucketUrl + "/" + info.Key, filePathres, nil
  80. }
  81. func (m *Minio) DeleteFile(key string) error {
  82. ctx, cancel := context.WithTimeout(context.Background(), time.Second*5)
  83. defer cancel()
  84. // Delete the object from MinIO
  85. err := m.Client.RemoveObject(ctx, m.bucket, key, minio.RemoveObjectOptions{})
  86. return err
  87. }