|
@@ -2,6 +2,7 @@ package services
|
|
|
|
|
|
import (
|
|
|
"bytes"
|
|
|
+ "crypto/tls"
|
|
|
"eta/eta_report/utils"
|
|
|
"fmt"
|
|
|
"github.com/aws/aws-sdk-go/aws"
|
|
@@ -9,7 +10,7 @@ import (
|
|
|
"github.com/aws/aws-sdk-go/aws/session"
|
|
|
"github.com/aws/aws-sdk-go/service/s3"
|
|
|
"io/ioutil"
|
|
|
- "os"
|
|
|
+ "net/http"
|
|
|
"time"
|
|
|
)
|
|
|
|
|
@@ -59,10 +60,12 @@ func (m *S3Oss) UploadFile(fileName, localFile, savePath string) (resourceUrl st
|
|
|
if err != nil {
|
|
|
fmt.Println(err.Error())
|
|
|
}
|
|
|
- _ = os.Remove(localFile)
|
|
|
}()
|
|
|
|
|
|
+ // 默认使用后端这个, 这里有两个配置的原因是
|
|
|
+ // 前端上传跨域问题可能会使用反向代理来解决, 这样的话同一个endpoint就会导致一端正常另一端不正常
|
|
|
endpoint := utils.S3Endpoint
|
|
|
+
|
|
|
accessKey := utils.S3AccessKeyId
|
|
|
secretKey := utils.S3AccessKeySecret
|
|
|
region := utils.S3Region
|
|
@@ -74,14 +77,31 @@ func (m *S3Oss) UploadFile(fileName, localFile, savePath string) (resourceUrl st
|
|
|
if forceStyle == "false" {
|
|
|
hostStyle = false
|
|
|
}
|
|
|
+ disableSSL := true // 默认true, 跳过SSL
|
|
|
+ if utils.S3DisableSSL == "false" {
|
|
|
+ disableSSL = false
|
|
|
+ }
|
|
|
+ //fmt.Println("disableSSL: ", disableSSL)
|
|
|
|
|
|
- // 创建AWS会话
|
|
|
- sess, e := session.NewSession(&aws.Config{
|
|
|
+ config := &aws.Config{
|
|
|
Region: aws.String(region),
|
|
|
Credentials: credentials.NewStaticCredentials(accessKey, secretKey, ""),
|
|
|
Endpoint: aws.String(endpoint),
|
|
|
S3ForcePathStyle: aws.Bool(hostStyle),
|
|
|
- })
|
|
|
+ DisableSSL: aws.Bool(disableSSL),
|
|
|
+ }
|
|
|
+ if disableSSL {
|
|
|
+ config.HTTPClient = &http.Client{
|
|
|
+ Transport: &http.Transport{
|
|
|
+ TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
|
|
|
+ },
|
|
|
+ }
|
|
|
+ }
|
|
|
+ //b, _ := json.Marshal(config)
|
|
|
+ //fmt.Println(string(b))
|
|
|
+
|
|
|
+ // 创建AWS会话
|
|
|
+ sess, e := session.NewSession(config)
|
|
|
if e != nil {
|
|
|
err = fmt.Errorf("new session err: %s", e.Error())
|
|
|
return
|
|
@@ -101,16 +121,24 @@ func (m *S3Oss) UploadFile(fileName, localFile, savePath string) (resourceUrl st
|
|
|
if savePath == "" {
|
|
|
path = uploadDir + time.Now().Format("200601/20060102/") + fileName
|
|
|
}
|
|
|
- _, e = client.PutObject(&s3.PutObjectInput{
|
|
|
+ putObjectInput := &s3.PutObjectInput{
|
|
|
Bucket: aws.String(bucketName),
|
|
|
Key: aws.String(path),
|
|
|
Body: bytes.NewReader(fileContent),
|
|
|
- })
|
|
|
+ }
|
|
|
+ if utils.S3OpenAcl == "1" {
|
|
|
+ putObjectInput.ACL = aws.String(s3.ObjectCannedACLPublicRead)
|
|
|
+ }
|
|
|
+ fmt.Printf("put object input: %+v\n", putObjectInput)
|
|
|
+ _, e = client.PutObject(putObjectInput)
|
|
|
if e != nil {
|
|
|
err = fmt.Errorf("put object err: %s", e.Error())
|
|
|
return
|
|
|
}
|
|
|
resourceUrl = resourceHost + path
|
|
|
+ if utils.ResourceProxyUrl != "" {
|
|
|
+ resourceUrl = utils.ResourceProxyUrl + path
|
|
|
+ }
|
|
|
return
|
|
|
}
|
|
|
|