aws_s3.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. package services
  2. import (
  3. "bytes"
  4. "crypto/tls"
  5. "eta/eta_report/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. // 默认使用后端这个, 这里有两个配置的原因是
  59. // 前端上传跨域问题可能会使用反向代理来解决, 这样的话同一个endpoint就会导致一端正常另一端不正常
  60. endpoint := utils.S3Endpoint
  61. accessKey := utils.S3AccessKeyId
  62. secretKey := utils.S3AccessKeySecret
  63. region := utils.S3Region
  64. bucketName := utils.S3BucketName
  65. uploadDir := utils.S3UploadDir
  66. resourceHost := utils.S3Host
  67. forceStyle := utils.S3ForceStyle
  68. hostStyle := true // 默认true, 使用`endpoint/bucket_name`这种HOST格式
  69. if forceStyle == "false" {
  70. hostStyle = false
  71. }
  72. disableSSL := true // 默认true, 跳过SSL
  73. if utils.S3DisableSSL == "false" {
  74. disableSSL = false
  75. }
  76. //fmt.Println("disableSSL: ", disableSSL)
  77. config := &aws.Config{
  78. Region: aws.String(region),
  79. Credentials: credentials.NewStaticCredentials(accessKey, secretKey, ""),
  80. Endpoint: aws.String(endpoint),
  81. S3ForcePathStyle: aws.Bool(hostStyle),
  82. DisableSSL: aws.Bool(disableSSL),
  83. }
  84. if disableSSL {
  85. config.HTTPClient = &http.Client{
  86. Transport: &http.Transport{
  87. TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
  88. },
  89. }
  90. }
  91. //b, _ := json.Marshal(config)
  92. //fmt.Println(string(b))
  93. // 创建AWS会话
  94. sess, e := session.NewSession(config)
  95. if e != nil {
  96. err = fmt.Errorf("new session err: %s", e.Error())
  97. return
  98. }
  99. // 创建S3服务客户端
  100. client := s3.New(sess)
  101. // 读取文件内容
  102. fileContent, e := ioutil.ReadFile(localFile)
  103. if e != nil {
  104. err = fmt.Errorf("read file err: %s", e.Error())
  105. return
  106. }
  107. path := savePath
  108. if savePath == "" {
  109. path = uploadDir + time.Now().Format("200601/20060102/") + fileName
  110. }
  111. putObjectInput := &s3.PutObjectInput{
  112. Bucket: aws.String(bucketName),
  113. Key: aws.String(path),
  114. Body: bytes.NewReader(fileContent),
  115. }
  116. if utils.S3OpenAcl == "1" {
  117. putObjectInput.ACL = aws.String(s3.ObjectCannedACLPublicRead)
  118. }
  119. fmt.Printf("put object input: %+v\n", putObjectInput)
  120. _, e = client.PutObject(putObjectInput)
  121. if e != nil {
  122. err = fmt.Errorf("put object err: %s", e.Error())
  123. return
  124. }
  125. resourceUrl = resourceHost + path
  126. if utils.ResourceProxyUrl != "" {
  127. resourceUrl = utils.ResourceProxyUrl + path
  128. }
  129. return
  130. }
  131. func (m *S3Oss) GetUploadToken() (token OssToken, err error) {
  132. token.Endpoint = utils.S3Endpoint
  133. token.AccessKeyId = utils.S3AccessKeyId
  134. token.AccessKeySecret = utils.S3AccessKeySecret
  135. token.RegionId = utils.S3Region
  136. token.Bucketname = utils.S3BucketName
  137. token.ImgHost = utils.S3Host
  138. token.Port = utils.S3EndpointPort
  139. hostStyle := true // 默认true, 使用`endpoint/bucket_name`这种HOST格式
  140. if utils.S3ForceStyle == "false" {
  141. hostStyle = false
  142. }
  143. token.S3ForceStyle = hostStyle
  144. token.S3Protocol = utils.S3Protocol
  145. return
  146. }