aws_s3.go 3.5 KB

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