aws_s3.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. package services
  2. import (
  3. "bytes"
  4. "crypto/tls"
  5. "eta_gn/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. return new(AliOss)
  28. }
  29. }
  30. type OssToken struct {
  31. AccessKeyId string
  32. SecretKeyId string
  33. RegionId string
  34. Bucketname string
  35. Endpoint string
  36. ImgHost string
  37. UseSSL string
  38. Port string
  39. AccessKeySecret string
  40. SecurityToken string
  41. ExpiredTime string
  42. Imghost string
  43. S3ForceStyle bool
  44. S3Protocol string
  45. }
  46. type S3Oss struct{}
  47. func (m *S3Oss) UploadFile(fileName, localFile, savePath string) (resourceUrl string, err error) {
  48. defer func() {
  49. if err != nil {
  50. fmt.Println(err.Error())
  51. }
  52. _ = os.Remove(localFile)
  53. }()
  54. endpoint := utils.S3Endpoint
  55. accessKey := utils.S3AccessKeyId
  56. secretKey := utils.S3AccessKeySecret
  57. region := utils.S3Region
  58. bucketName := utils.S3BucketName
  59. uploadDir := utils.S3UploadDir
  60. resourceHost := utils.S3Host
  61. forceStyle := utils.S3ForceStyle
  62. hostStyle := true // 默认true, 使用`endpoint/bucket_name`这种HOST格式
  63. if forceStyle == "false" {
  64. hostStyle = false
  65. }
  66. disableSSL := true // 默认true, 跳过SSL
  67. if utils.S3DisableSSL == "false" {
  68. disableSSL = false
  69. }
  70. config := &aws.Config{
  71. Region: aws.String(region),
  72. Credentials: credentials.NewStaticCredentials(accessKey, secretKey, ""),
  73. Endpoint: aws.String(endpoint),
  74. S3ForcePathStyle: aws.Bool(hostStyle),
  75. DisableSSL: aws.Bool(disableSSL),
  76. }
  77. if disableSSL {
  78. config.HTTPClient = &http.Client{
  79. Transport: &http.Transport{
  80. TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
  81. },
  82. }
  83. }
  84. sess, e := session.NewSession(config)
  85. if e != nil {
  86. err = fmt.Errorf("new session err: %s", e.Error())
  87. return
  88. }
  89. client := s3.New(sess)
  90. fileContent, e := ioutil.ReadFile(localFile)
  91. if e != nil {
  92. err = fmt.Errorf("read file err: %s", e.Error())
  93. return
  94. }
  95. path := savePath
  96. if savePath == "" {
  97. path = uploadDir + time.Now().Format("200601/20060102/") + fileName
  98. }
  99. putObjectInput := &s3.PutObjectInput{
  100. Bucket: aws.String(bucketName),
  101. Key: aws.String(path),
  102. Body: bytes.NewReader(fileContent),
  103. }
  104. if utils.S3OpenAcl == "1" {
  105. putObjectInput.ACL = aws.String(s3.ObjectCannedACLPublicRead)
  106. }
  107. fmt.Printf("put object input: %+v\n", putObjectInput)
  108. _, e = client.PutObject(putObjectInput)
  109. if e != nil {
  110. err = fmt.Errorf("put object err: %s", e.Error())
  111. return
  112. }
  113. resourceUrl = resourceHost + path
  114. if utils.ResourceProxyUrl != "" {
  115. resourceUrl = utils.ResourceProxyUrl + path
  116. }
  117. return
  118. }
  119. func (m *S3Oss) GetUploadToken() (token OssToken, err error) {
  120. token.Endpoint = utils.S3Endpoint
  121. token.AccessKeyId = utils.S3AccessKeyId
  122. token.AccessKeySecret = utils.S3AccessKeySecret
  123. token.RegionId = utils.S3Region
  124. token.Bucketname = utils.S3BucketName
  125. token.ImgHost = utils.S3Host
  126. token.Port = utils.S3EndpointPort
  127. hostStyle := true // 默认true, 使用`endpoint/bucket_name`这种HOST格式
  128. if utils.S3ForceStyle == "false" {
  129. hostStyle = false
  130. }
  131. token.S3ForceStyle = hostStyle
  132. token.S3Protocol = utils.S3Protocol
  133. return
  134. }