hsun 1 年之前
父节点
当前提交
791f90dce8
共有 3 个文件被更改,包括 74 次插入59 次删除
  1. 16 5
      services/aws_s3.go
  2. 54 54
      services/minio.go
  3. 4 0
      utils/config.go

+ 16 - 5
services/aws_s3.go

@@ -46,7 +46,8 @@ type OssToken struct {
 	//RegionId        string
 	//Bucketname      string
 	//Endpoint        string
-	Imghost string
+	Imghost      string
+	S3ForceStyle bool
 }
 
 type S3Oss struct{}
@@ -65,13 +66,18 @@ func (m *S3Oss) UploadFile(fileName, localFile, savePath string) (resourceUrl st
 	bucketName := utils.S3BucketName
 	uploadDir := utils.S3UploadDir
 	resourceHost := utils.S3Host
+	forceStyle := utils.S3ForceStyle
+	hostStyle := true // 默认true, 使用`endpoint/bucket_name`这种HOST格式
+	if forceStyle == "false" {
+		hostStyle = false
+	}
 
 	// 创建AWS会话
 	sess, e := session.NewSession(&aws.Config{
 		Region:           aws.String(region),
 		Credentials:      credentials.NewStaticCredentials(accessKey, secretKey, ""),
 		Endpoint:         aws.String(endpoint),
-		S3ForcePathStyle: aws.Bool(false),
+		S3ForcePathStyle: aws.Bool(hostStyle),
 	})
 	if e != nil {
 		err = fmt.Errorf("new session err: %s", e.Error())
@@ -102,16 +108,21 @@ func (m *S3Oss) UploadFile(fileName, localFile, savePath string) (resourceUrl st
 		return
 	}
 	resourceUrl = resourceHost + path
-	fmt.Println(resourceUrl)
 	return
 }
 
 func (m *S3Oss) GetUploadToken() (token OssToken, err error) {
-	token.Endpoint = utils.Endpoint
+	token.Endpoint = utils.S3Endpoint
 	token.AccessKeyId = utils.S3AccessKeyId
 	token.AccessKeySecret = utils.S3AccessKeySecret
 	token.RegionId = utils.S3Region
-	token.Bucketname = utils.Bucketname
+	token.Bucketname = utils.S3BucketName
 	token.ImgHost = utils.S3Host
+	token.Port = utils.S3EndpointPort
+	hostStyle := true // 默认true, 使用`endpoint/bucket_name`这种HOST格式
+	if utils.S3ForceStyle == "false" {
+		hostStyle = false
+	}
+	token.S3ForceStyle = hostStyle
 	return
 }

+ 54 - 54
services/minio.go

@@ -10,61 +10,61 @@ import (
 	"time"
 )
 
-func GetMinIOSTSToken() (item *Token, err error) {
-	// MinIO服务的访问信息
-	item = new(Token)
-	//useSSL := false
-	//if utils.MinIoUseSSL == "true" {
-	//	useSSL = true
-	//}
-	// 创建MinIO客户端
-	//minioClient, err := minio.New(utils.MinIoEndpoint, &minio.Options{
-	//	Creds:  credentials.NewStaticV4(utils.MinIoAccessKeyId, utils.MinIoAccessKeySecret, ""),
-	//	Secure: useSSL,
-	//})
-	//if err != nil {
-	//	return nil, err
-	//}
-	// 设置STS凭证请求参数
-	//policy := `{
-	//    "Version": "2012-10-17",
-	//    "Statement": [
-	//        {
-	//            "Sid": "",
-	//            "Effect": "Allow",
-	//            "Principal": {"AWS": "arn:aws:iam::1234567890:root"},
-	//            "Action": "s3:GetObject",
-	//            "Resource": "arn:aws:s3:::<YourBucketName>/*"
-	//        }
-	//    ]
-	//}`
-	//expiry := time.Hour * 24 // STS凭证的过期时间
-	//获取STS凭证
-	//stsCredentials, err := minioClient.PresignedPutObject(context.Background(), "etastatic", "myobject", expiry)
-	//if err != nil {
-	//	return
-	//}
-	item.AccessKeyId = utils.MinIoAccessKeyId
-	item.SecretKeyId = utils.MinIoAccessKeySecret
-	item.Endpoint = utils.MinIoEndpoint
-	item.ImgHost = utils.MinIoImghost
-	item.Bucketname = utils.MinIoBucketname
-	item.UseSSL = utils.MinIoUseSSL
-	item.RegionId = utils.MinIoRegion
-	item.Port = utils.MinIoPort
-	return
-}
+//func GetMinIOSTSToken() (item *Token, err error) {
+//	// MinIO服务的访问信息
+//	item = new(Token)
+//	//useSSL := false
+//	//if utils.MinIoUseSSL == "true" {
+//	//	useSSL = true
+//	//}
+//	// 创建MinIO客户端
+//	//minioClient, err := minio.New(utils.MinIoEndpoint, &minio.Options{
+//	//	Creds:  credentials.NewStaticV4(utils.MinIoAccessKeyId, utils.MinIoAccessKeySecret, ""),
+//	//	Secure: useSSL,
+//	//})
+//	//if err != nil {
+//	//	return nil, err
+//	//}
+//	// 设置STS凭证请求参数
+//	//policy := `{
+//	//    "Version": "2012-10-17",
+//	//    "Statement": [
+//	//        {
+//	//            "Sid": "",
+//	//            "Effect": "Allow",
+//	//            "Principal": {"AWS": "arn:aws:iam::1234567890:root"},
+//	//            "Action": "s3:GetObject",
+//	//            "Resource": "arn:aws:s3:::<YourBucketName>/*"
+//	//        }
+//	//    ]
+//	//}`
+//	//expiry := time.Hour * 24 // STS凭证的过期时间
+//	//获取STS凭证
+//	//stsCredentials, err := minioClient.PresignedPutObject(context.Background(), "etastatic", "myobject", expiry)
+//	//if err != nil {
+//	//	return
+//	//}
+//	item.AccessKeyId = utils.MinIoAccessKeyId
+//	item.SecretKeyId = utils.MinIoAccessKeySecret
+//	item.Endpoint = utils.MinIoEndpoint
+//	item.ImgHost = utils.MinIoImghost
+//	item.Bucketname = utils.MinIoBucketname
+//	item.UseSSL = utils.MinIoUseSSL
+//	item.RegionId = utils.MinIoRegion
+//	item.Port = utils.MinIoPort
+//	return
+//}
 
-type Token struct {
-	AccessKeyId string
-	SecretKeyId string
-	RegionId    string
-	Bucketname  string
-	Endpoint    string
-	ImgHost     string
-	UseSSL      string
-	Port        string
-}
+//type Token struct {
+//	AccessKeyId string
+//	SecretKeyId string
+//	RegionId    string
+//	Bucketname  string
+//	Endpoint    string
+//	ImgHost     string
+//	UseSSL      string
+//	Port        string
+//}
 
 //func UploadMinIo() {
 //	ctx := context.Background()

+ 4 - 0
utils/config.go

@@ -222,6 +222,8 @@ var (
 	S3AccessKeySecret string
 	S3Host            string
 	S3Region          string
+	S3ForceStyle      string
+	S3EndpointPort    string
 )
 
 func init() {
@@ -491,6 +493,8 @@ func init() {
 		S3AccessKeySecret = config["s3_access_key_secret"]
 		S3UploadDir = config["s3_upload_dir"]
 		S3Region = config["s3_region"]
+		S3ForceStyle = config["s3_force_style"]
+		S3EndpointPort = config["s3_endpoint_port"]
 	}
 
 	// 生成长图服务地址