minio.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. package config
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "mime/multipart"
  6. "time"
  7. "github.com/aws/aws-sdk-go/aws"
  8. "github.com/aws/aws-sdk-go/aws/credentials"
  9. "github.com/aws/aws-sdk-go/aws/session"
  10. "github.com/aws/aws-sdk-go/service/s3/s3manager"
  11. "github.com/pkg/errors"
  12. )
  13. // File 文件存储配置
  14. type File struct {
  15. OssType string `mapstructure:"oss-type" json:"oss-type" yaml:"oss-type"`
  16. Path string `mapstructure:"path" json:"path" yaml:"path"`
  17. ProxyPath string `mapstructure:"proxy-path" json:"proxy-path" yaml:"proxy-path"`
  18. StorePath string `mapstructure:"store-path" json:"store-path" yaml:"store-path"`
  19. OriginConf map[string]interface{} `mapstructure:"origin-conf" json:"origin-conf" yaml:"origin-conf"`
  20. }
  21. // AwsS3 AWS S3配置(兼容MinIO)
  22. type AwsS3 struct {
  23. Bucket string `mapstructure:"bucket" json:"bucket" yaml:"bucket"`
  24. Region string `mapstructure:"region" json:"region" yaml:"region"`
  25. Endpoint string `mapstructure:"endpoint" json:"endpoint" yaml:"endpoint"`
  26. SecretID string `mapstructure:"secret-id" json:"secret-id" yaml:"secret-id"`
  27. SecretKey string `mapstructure:"secret-key" json:"secret-key" yaml:"secret-key"`
  28. BaseURL string `mapstructure:"base-url" json:"base-url" yaml:"base-url"`
  29. PathPrefix string `mapstructure:"path-prefix" json:"path-prefix" yaml:"path-prefix"`
  30. S3ForcePathStyle bool `mapstructure:"s3-force-path-style" json:"s3-force-path-style" yaml:"s3-force-path-style"`
  31. DisableSSL bool `mapstructure:"disable-ssl" json:"disable-ssl" yaml:"disable-ssl"`
  32. }
  33. // newSession 创建S3 session
  34. func (a *AwsS3) newSession() *session.Session {
  35. sess, _ := session.NewSession(&aws.Config{
  36. Region: aws.String(a.Region),
  37. Endpoint: aws.String(a.Endpoint),
  38. S3ForcePathStyle: aws.Bool(a.S3ForcePathStyle),
  39. DisableSSL: aws.Bool(a.DisableSSL),
  40. Credentials: credentials.NewStaticCredentials(
  41. a.SecretID,
  42. a.SecretKey,
  43. "",
  44. ),
  45. })
  46. return sess
  47. }
  48. // UploadFile 上传文件
  49. func (a *AwsS3) UploadFile(file *multipart.FileHeader) (string, string, error) {
  50. _session := a.newSession()
  51. uploader := s3manager.NewUploader(_session)
  52. // 生成文件名
  53. fileKey := fmt.Sprintf("%d%s", time.Now().Unix(), file.Filename)
  54. filename := a.PathPrefix + "/" + fileKey
  55. f, openError := file.Open()
  56. if openError != nil {
  57. return "", "", errors.New("function file.Open() failed, err:" + openError.Error())
  58. }
  59. defer f.Close()
  60. _, err := uploader.Upload(&s3manager.UploadInput{
  61. Bucket: aws.String(a.Bucket),
  62. Key: aws.String(filename),
  63. Body: f,
  64. })
  65. if err != nil {
  66. return "", "", err
  67. }
  68. return a.BaseURL + "/" + filename, fileKey, nil
  69. }
  70. // UploadFileToStorage 上传文件到存储
  71. func UploadFileToStorage(file *multipart.FileHeader) (string, error) {
  72. conf := EnvConf().File
  73. if conf == nil {
  74. return "", fmt.Errorf("file config not found")
  75. }
  76. if conf.OssType != "aws" {
  77. return "", fmt.Errorf("only aws/minio storage type is supported")
  78. }
  79. // 解析 OriginConf 到 AwsS3 结构
  80. engine := new(AwsS3)
  81. byteData, err := json.Marshal(conf.OriginConf)
  82. if err != nil {
  83. return "", err
  84. }
  85. err = json.Unmarshal(byteData, engine)
  86. if err != nil {
  87. return "", err
  88. }
  89. viewPath, _, err := engine.UploadFile(file)
  90. if err != nil {
  91. return "", err
  92. }
  93. return viewPath, nil
  94. }