xyxie 1 год назад
Родитель
Сommit
d8619e9777
2 измененных файлов с 42 добавлено и 8 удалено
  1. 35 7
      services/aws_s3.go
  2. 7 1
      utils/config.go

+ 35 - 7
services/aws_s3.go

@@ -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
 }
 

+ 7 - 1
utils/config.go

@@ -53,6 +53,7 @@ var (
 // 对象存储客户端
 var (
 	ObjectStorageClient string // 目前有oss minio,默认oss
+	ResourceProxyUrl    string // 代理资源地址
 )
 
 // 阿里云配置
@@ -92,6 +93,8 @@ var (
 	S3ForceStyle      string
 	S3EndpointPort    string
 	S3Protocol        string
+	S3DisableSSL      string
+	S3OpenAcl         string
 )
 
 func init() {
@@ -184,7 +187,8 @@ func init() {
 
 	// 对象存储客户端
 	ObjectStorageClient = config["object_storage_client"]
-
+	// 代理资源地址
+	ResourceProxyUrl = config["resource_proxy_url"]
 	// OSS相关
 	{
 		Endpoint = config["endpoint"]
@@ -222,5 +226,7 @@ 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"]
 	}
 }