aws_s3.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. package services
  2. import (
  3. "bytes"
  4. "eta/eta_mobile/utils"
  5. "fmt"
  6. "github.com/aws/aws-sdk-go/aws"
  7. "github.com/aws/aws-sdk-go/aws/credentials"
  8. "github.com/aws/aws-sdk-go/aws/session"
  9. "github.com/aws/aws-sdk-go/service/s3"
  10. "io/ioutil"
  11. "time"
  12. )
  13. type OssClient interface {
  14. UploadFile(string, string, string) (string, error)
  15. GetUploadToken() (OssToken, error)
  16. }
  17. func NewOssClient() OssClient {
  18. switch utils.ObjectStorageClient {
  19. case utils.STORAGESOURCE_MINIO_NAME:
  20. return new(MinioOss)
  21. case utils.STORAGESOURCE_S3_NAME:
  22. return new(S3Oss)
  23. default:
  24. // 默认使用阿里云OSS
  25. return new(AliOss)
  26. }
  27. }
  28. // OssToken 此处为了兼容前端那边所以有重复的
  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. //AccessKeyId string
  39. AccessKeySecret string
  40. SecurityToken string
  41. ExpiredTime string
  42. //RegionId string
  43. //Bucketname string
  44. //Endpoint string
  45. Imghost string
  46. S3ForceStyle bool
  47. S3Protocol string
  48. }
  49. type S3Oss struct{}
  50. func (m *S3Oss) UploadFile(fileName, localFile, savePath string) (resourceUrl string, err error) {
  51. defer func() {
  52. if err != nil {
  53. fmt.Println(err.Error())
  54. }
  55. }()
  56. endpoint := utils.S3Endpoint
  57. accessKey := utils.S3AccessKeyId
  58. secretKey := utils.S3AccessKeySecret
  59. region := utils.S3Region
  60. bucketName := utils.S3BucketName
  61. uploadDir := utils.S3UploadDir
  62. resourceHost := utils.S3Host
  63. forceStyle := utils.S3ForceStyle
  64. hostStyle := true // 默认true, 使用`endpoint/bucket_name`这种HOST格式
  65. if forceStyle == "false" {
  66. hostStyle = false
  67. }
  68. // 创建AWS会话
  69. sess, e := session.NewSession(&aws.Config{
  70. Region: aws.String(region),
  71. Credentials: credentials.NewStaticCredentials(accessKey, secretKey, ""),
  72. Endpoint: aws.String(endpoint),
  73. S3ForcePathStyle: aws.Bool(hostStyle),
  74. })
  75. if e != nil {
  76. err = fmt.Errorf("new session err: %s", e.Error())
  77. return
  78. }
  79. // 创建S3服务客户端
  80. client := s3.New(sess)
  81. // 读取文件内容
  82. fileContent, e := ioutil.ReadFile(localFile)
  83. if e != nil {
  84. err = fmt.Errorf("read file err: %s", e.Error())
  85. return
  86. }
  87. path := savePath
  88. if savePath == "" {
  89. path = uploadDir + time.Now().Format("200601/20060102/") + fileName
  90. }
  91. _, e = client.PutObject(&s3.PutObjectInput{
  92. Bucket: aws.String(bucketName),
  93. Key: aws.String(path),
  94. Body: bytes.NewReader(fileContent),
  95. })
  96. if e != nil {
  97. err = fmt.Errorf("put object err: %s", e.Error())
  98. return
  99. }
  100. resourceUrl = resourceHost + path
  101. return
  102. }
  103. func (m *S3Oss) GetUploadToken() (token OssToken, err error) {
  104. token.Endpoint = utils.S3Endpoint
  105. token.AccessKeyId = utils.S3AccessKeyId
  106. token.AccessKeySecret = utils.S3AccessKeySecret
  107. token.RegionId = utils.S3Region
  108. token.Bucketname = utils.S3BucketName
  109. token.ImgHost = utils.S3Host
  110. token.Port = utils.S3EndpointPort
  111. hostStyle := true // 默认true, 使用`endpoint/bucket_name`这种HOST格式
  112. if utils.S3ForceStyle == "false" {
  113. hostStyle = false
  114. }
  115. token.S3ForceStyle = hostStyle
  116. token.S3Protocol = utils.S3Protocol
  117. return
  118. }