aws_s3.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. package oss
  2. import (
  3. "bytes"
  4. "crypto/tls"
  5. "eta_gn/eta_obs/global"
  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 S3Oss struct{}
  16. func (m *S3Oss) UploadFile(fileName, localFile, savePath string) (resourceUrl string, err error) {
  17. defer func() {
  18. if err != nil {
  19. fmt.Println(err.Error())
  20. }
  21. }()
  22. // 默认使用后端这个, 这里有两个配置的原因是
  23. // 前端上传跨域问题可能会使用反向代理来解决, 这样的话同一个endpoint就会导致一端正常另一端不正常
  24. endpoint := global.CONFIG.Oss.BackEndPoint
  25. if endpoint == "" {
  26. endpoint = global.CONFIG.Oss.EndPoint
  27. }
  28. accessKey := global.CONFIG.Oss.AccessKeyId
  29. secretKey := global.CONFIG.Oss.AccessKeySecret
  30. region := global.CONFIG.Oss.RegionId
  31. bucketName := global.CONFIG.Oss.BucketName
  32. uploadDir := global.CONFIG.Oss.UploadDir
  33. config := &aws.Config{
  34. Region: aws.String(region),
  35. Credentials: credentials.NewStaticCredentials(accessKey, secretKey, ""),
  36. Endpoint: aws.String(endpoint),
  37. S3ForcePathStyle: aws.Bool(global.CONFIG.Oss.ForceStyle),
  38. DisableSSL: aws.Bool(global.CONFIG.Oss.DisableSSL),
  39. }
  40. if global.CONFIG.Oss.DisableSSL {
  41. config.HTTPClient = &http.Client{
  42. Transport: &http.Transport{
  43. TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
  44. },
  45. }
  46. }
  47. //b, _ := json.Marshal(config)
  48. //fmt.Println(string(b))
  49. // 创建AWS会话
  50. sess, e := session.NewSession(config)
  51. if e != nil {
  52. err = fmt.Errorf("new session err: %s", e.Error())
  53. return
  54. }
  55. // 创建S3服务客户端
  56. client := s3.New(sess)
  57. // 读取文件内容
  58. fileContent, e := ioutil.ReadFile(localFile)
  59. if e != nil {
  60. err = fmt.Errorf("read file err: %s", e.Error())
  61. return
  62. }
  63. path := savePath
  64. if savePath == "" {
  65. path = uploadDir + time.Now().Format("200601/20060102/") + fileName
  66. }
  67. putObjectInput := &s3.PutObjectInput{
  68. Bucket: aws.String(bucketName),
  69. Key: aws.String(path),
  70. Body: bytes.NewReader(fileContent),
  71. }
  72. if global.CONFIG.Oss.OpenAcl {
  73. putObjectInput.ACL = aws.String(s3.ObjectCannedACLPublicRead)
  74. }
  75. fmt.Printf("put object input: %+v\n", putObjectInput)
  76. _, e = client.PutObject(putObjectInput)
  77. if e != nil {
  78. err = fmt.Errorf("put object err: %s", e.Error())
  79. return
  80. }
  81. resourceUrl = global.CONFIG.Oss.ImgHost + path
  82. if global.CONFIG.Oss.ResourceProxyUrl != "" {
  83. resourceUrl = global.CONFIG.Oss.ResourceProxyUrl + path
  84. }
  85. return
  86. }
  87. func (m *S3Oss) GetUploadToken() (token OssToken, err error) {
  88. token.Endpoint = global.CONFIG.Oss.EndPoint
  89. token.AccessKeyId = global.CONFIG.Oss.AccessKeyId
  90. token.AccessKeySecret = global.CONFIG.Oss.AccessKeySecret
  91. token.RegionId = global.CONFIG.Oss.RegionId
  92. token.Bucketname = global.CONFIG.Oss.BucketName
  93. token.ImgHost = global.CONFIG.Oss.ImgHost
  94. token.Port = global.CONFIG.Oss.Port
  95. token.S3ForceStyle = global.CONFIG.Oss.ForceStyle
  96. token.S3Protocol = global.CONFIG.Oss.Protocol
  97. return
  98. }