Browse Source

s3跳过SSL

hsun 1 year ago
parent
commit
0257a05189
2 changed files with 24 additions and 3 deletions
  1. 22 3
      services/aws_s3.go
  2. 2 0
      utils/config.go

+ 22 - 3
services/aws_s3.go

@@ -2,6 +2,7 @@ package services
 
 
 import (
 import (
 	"bytes"
 	"bytes"
+	"crypto/tls"
 	"eta/eta_api/utils"
 	"eta/eta_api/utils"
 	"fmt"
 	"fmt"
 	"github.com/aws/aws-sdk-go/aws"
 	"github.com/aws/aws-sdk-go/aws"
@@ -9,6 +10,7 @@ import (
 	"github.com/aws/aws-sdk-go/aws/session"
 	"github.com/aws/aws-sdk-go/aws/session"
 	"github.com/aws/aws-sdk-go/service/s3"
 	"github.com/aws/aws-sdk-go/service/s3"
 	"io/ioutil"
 	"io/ioutil"
+	"net/http"
 	"time"
 	"time"
 )
 )
 
 
@@ -72,14 +74,31 @@ func (m *S3Oss) UploadFile(fileName, localFile, savePath string) (resourceUrl st
 	if forceStyle == "false" {
 	if forceStyle == "false" {
 		hostStyle = 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),
 		Region:           aws.String(region),
 		Credentials:      credentials.NewStaticCredentials(accessKey, secretKey, ""),
 		Credentials:      credentials.NewStaticCredentials(accessKey, secretKey, ""),
 		Endpoint:         aws.String(endpoint),
 		Endpoint:         aws.String(endpoint),
 		S3ForcePathStyle: aws.Bool(hostStyle),
 		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 {
 	if e != nil {
 		err = fmt.Errorf("new session err: %s", e.Error())
 		err = fmt.Errorf("new session err: %s", e.Error())
 		return
 		return

+ 2 - 0
utils/config.go

@@ -225,6 +225,7 @@ var (
 	S3ForceStyle      string
 	S3ForceStyle      string
 	S3EndpointPort    string
 	S3EndpointPort    string
 	S3Protocol        string
 	S3Protocol        string
+	S3DisableSSL      string
 )
 )
 
 
 func init() {
 func init() {
@@ -497,6 +498,7 @@ func init() {
 		S3ForceStyle = config["s3_force_style"]
 		S3ForceStyle = config["s3_force_style"]
 		S3EndpointPort = config["s3_endpoint_port"]
 		S3EndpointPort = config["s3_endpoint_port"]
 		S3Protocol = config["s3_protocol"]
 		S3Protocol = config["s3_protocol"]
+		S3DisableSSL = config["s3_disable_ssl"]
 	}
 	}
 
 
 	// 生成长图服务地址
 	// 生成长图服务地址