Browse Source

Merge branch 'hotfix/s3_0129' into debug

xyxie 1 year ago
parent
commit
093ce94461
3 changed files with 42 additions and 7 deletions
  1. 1 1
      controllers/cloud_disk.go
  2. 35 6
      services/aws_s3.go
  3. 6 0
      utils/config.go

+ 1 - 1
controllers/cloud_disk.go

@@ -580,7 +580,7 @@ func (this *CloudDiskController) ResourceUpload() {
 		_ = os.Remove(filePath)
 	}()
 	// 上传到阿里云
-	ossDir := "static/cloud_disk/"
+	ossDir := utils.STATIC_DIR + "cloud_disk/"
 
 	resourceUrl := ``
 	//上传到阿里云 和 minio

+ 35 - 6
services/aws_s3.go

@@ -2,6 +2,7 @@ package services
 
 import (
 	"bytes"
+	"crypto/tls"
 	"eta/eta_api/utils"
 	"fmt"
 	"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/service/s3"
 	"io/ioutil"
+	"net/http"
 	"time"
 )
 
@@ -60,7 +62,12 @@ func (m *S3Oss) UploadFile(fileName, localFile, savePath string) (resourceUrl st
 		}
 	}()
 
-	endpoint := utils.S3Endpoint
+	// 默认使用后端这个, 这里有两个配置的原因是
+	// 前端上传跨域问题可能会使用反向代理来解决, 这样的话同一个endpoint就会导致一端正常另一端不正常
+	endpoint := utils.S3BackEndpoint
+	if endpoint == "" {
+		endpoint = utils.S3Endpoint
+	}
 	accessKey := utils.S3AccessKeyId
 	secretKey := utils.S3AccessKeySecret
 	region := utils.S3Region
@@ -72,14 +79,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
@@ -99,11 +123,16 @@ 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

+ 6 - 0
utils/config.go

@@ -218,6 +218,7 @@ var (
 // S3配置
 var (
 	S3Endpoint        string
+	S3BackEndpoint    string
 	S3BucketName      string
 	S3UploadDir       string
 	S3AccessKeyId     string
@@ -227,6 +228,8 @@ var (
 	S3ForceStyle      string
 	S3EndpointPort    string
 	S3Protocol        string
+	S3DisableSSL      string
+	S3OpenAcl         string
 )
 
 func init() {
@@ -501,6 +504,7 @@ func init() {
 	// S3-OSS相关
 	{
 		S3Endpoint = config["s3_endpoint"]
+		S3BackEndpoint = config["s3_back_endpoint"]
 		S3BucketName = config["s3_bucket_name"]
 		S3Host = config["s3_host"]
 		S3AccessKeyId = config["s3_access_key_id"]
@@ -510,6 +514,8 @@ func init() {
 		S3ForceStyle = config["s3_force_style"]
 		S3EndpointPort = config["s3_endpoint_port"]
 		S3Protocol = config["s3_protocol"]
+		S3DisableSSL = config["s3_disable_ssl"]
+		S3OpenAcl = config["s3_open_acl"]
 	}
 
 	// 生成长图服务地址