aws_s3.go 4.0 KB

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