aws_s3.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. package services
  2. import (
  3. "bytes"
  4. "crypto/tls"
  5. "eta/eta_api/utils"
  6. "fmt"
  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"
  11. "io/ioutil"
  12. "net/http"
  13. "time"
  14. )
  15. type OssClient interface {
  16. UploadFile(string, string, string) (string, error)
  17. GetUploadToken() (OssToken, error)
  18. }
  19. func NewOssClient() OssClient {
  20. switch utils.ObjectStorageClient {
  21. case utils.STORAGESOURCE_MINIO_NAME:
  22. return new(MinioOss)
  23. case utils.STORAGESOURCE_S3_NAME:
  24. return new(S3Oss)
  25. default:
  26. // 默认使用阿里云OSS
  27. return new(AliOss)
  28. }
  29. }
  30. // OssToken 此处为了兼容前端那边所以有重复的
  31. type OssToken struct {
  32. AccessKeyId string
  33. SecretKeyId string
  34. RegionId string
  35. Bucketname string
  36. Endpoint string
  37. ImgHost string
  38. UseSSL string
  39. Port string
  40. //AccessKeyId string
  41. AccessKeySecret string
  42. SecurityToken string
  43. ExpiredTime string
  44. //RegionId string
  45. //Bucketname string
  46. //Endpoint string
  47. Imghost string
  48. S3ForceStyle bool
  49. S3Protocol string
  50. }
  51. type S3Oss struct{}
  52. func (m *S3Oss) UploadFile(fileName, localFile, savePath string) (resourceUrl string, err error) {
  53. defer func() {
  54. if err != nil {
  55. fmt.Println(err.Error())
  56. }
  57. }()
  58. endpoint := utils.S3Endpoint
  59. accessKey := utils.S3AccessKeyId
  60. secretKey := utils.S3AccessKeySecret
  61. region := utils.S3Region
  62. bucketName := utils.S3BucketName
  63. uploadDir := utils.S3UploadDir
  64. resourceHost := utils.S3Host
  65. forceStyle := utils.S3ForceStyle
  66. hostStyle := true // 默认true, 使用`endpoint/bucket_name`这种HOST格式
  67. if forceStyle == "false" {
  68. hostStyle = false
  69. }
  70. disableSSL := true // 默认true, 跳过SSL
  71. if utils.S3DisableSSL == "false" {
  72. disableSSL = false
  73. }
  74. //fmt.Println("disableSSL: ", disableSSL)
  75. config := &aws.Config{
  76. Region: aws.String(region),
  77. Credentials: credentials.NewStaticCredentials(accessKey, secretKey, ""),
  78. Endpoint: aws.String(endpoint),
  79. S3ForcePathStyle: aws.Bool(hostStyle),
  80. DisableSSL: aws.Bool(disableSSL),
  81. }
  82. if disableSSL {
  83. config.HTTPClient = &http.Client{
  84. Transport: &http.Transport{
  85. TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
  86. },
  87. }
  88. }
  89. //b, _ := json.Marshal(config)
  90. //fmt.Println(string(b))
  91. // 创建AWS会话
  92. sess, e := session.NewSession(config)
  93. if e != nil {
  94. err = fmt.Errorf("new session err: %s", e.Error())
  95. return
  96. }
  97. // 创建S3服务客户端
  98. client := s3.New(sess)
  99. // 读取文件内容
  100. fileContent, e := ioutil.ReadFile(localFile)
  101. if e != nil {
  102. err = fmt.Errorf("read file err: %s", e.Error())
  103. return
  104. }
  105. path := savePath
  106. if savePath == "" {
  107. path = uploadDir + time.Now().Format("200601/20060102/") + fileName
  108. }
  109. _, e = client.PutObject(&s3.PutObjectInput{
  110. Bucket: aws.String(bucketName),
  111. Key: aws.String(path),
  112. Body: bytes.NewReader(fileContent),
  113. })
  114. if e != nil {
  115. err = fmt.Errorf("put object err: %s", e.Error())
  116. return
  117. }
  118. resourceUrl = resourceHost + path
  119. return
  120. }
  121. func (m *S3Oss) GetUploadToken() (token OssToken, err error) {
  122. token.Endpoint = utils.S3Endpoint
  123. token.AccessKeyId = utils.S3AccessKeyId
  124. token.AccessKeySecret = utils.S3AccessKeySecret
  125. token.RegionId = utils.S3Region
  126. token.Bucketname = utils.S3BucketName
  127. token.ImgHost = utils.S3Host
  128. token.Port = utils.S3EndpointPort
  129. hostStyle := true // 默认true, 使用`endpoint/bucket_name`这种HOST格式
  130. if utils.S3ForceStyle == "false" {
  131. hostStyle = false
  132. }
  133. token.S3ForceStyle = hostStyle
  134. token.S3Protocol = utils.S3Protocol
  135. return
  136. }