aws_s3.go 4.0 KB

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