Browse Source

支持阿里云

kobe6258 4 weeks ago
parent
commit
48618f3098
5 changed files with 132 additions and 2 deletions
  1. 4 0
      go.mod
  2. 16 0
      go.sum
  3. 50 0
      utils/aliyun_oss.go
  4. 51 0
      utils/config.go
  5. 11 2
      utils/oss_client.go

+ 4 - 0
go.mod

@@ -4,6 +4,7 @@ go 1.21.7
 
 require (
 	github.com/PuerkitoBio/goquery v1.9.2
+	github.com/aws/aws-sdk-go v1.55.7
 	github.com/beego/bee/v2 v2.1.0
 	github.com/beego/beego/v2 v2.1.4
 	github.com/chromedp/cdproto v0.0.0-20240202021202-6d0b6a386732
@@ -22,6 +23,8 @@ require (
 )
 
 require (
+	github.com/aliyun/alibabacloud-oss-go-sdk-v2 v1.2.2 // indirect
+	github.com/aliyun/aliyun-oss-go-sdk v3.0.2+incompatible // indirect
 	github.com/dustin/go-humanize v1.0.1 // indirect
 	github.com/go-ini/ini v1.67.0 // indirect
 	github.com/goccy/go-json v0.10.3 // indirect
@@ -30,6 +33,7 @@ require (
 	github.com/klauspost/cpuid/v2 v2.2.8 // indirect
 	github.com/minio/md5-simd v1.1.2 // indirect
 	github.com/rs/xid v1.5.0 // indirect
+	golang.org/x/time v0.4.0 // indirect
 )
 
 require (

+ 16 - 0
go.sum

@@ -8,9 +8,17 @@ github.com/alecthomas/units v0.0.0-20151022065526-2efee857e7cf/go.mod h1:ybxpYRF
 github.com/alecthomas/units v0.0.0-20190717042225-c3de453c63f4/go.mod h1:ybxpYRFXyAe+OPACYpWeL0wqObRcbAqCMya13uyzqw0=
 github.com/alicebob/gopher-json v0.0.0-20180125190556-5a6b3ba71ee6/go.mod h1:SGnFV6hVsYE877CKEZ6tDNTjaSXYUk6QqoIK6PrAtcc=
 github.com/alicebob/miniredis v2.5.0+incompatible/go.mod h1:8HZjEj4yU0dwhYHky+DxYx+6BMjkBbe5ONFIF1MXffk=
+github.com/aliyun/alibabacloud-oss-go-sdk-v2 v1.2.2 h1:YNsVAVaO3xAfNqnsjcOeZx+htocoFR2Oh8onhVp7PcM=
+github.com/aliyun/alibabacloud-oss-go-sdk-v2 v1.2.2/go.mod h1:FTzydeQVmR24FI0D6XWUOMKckjXehM/jgMn1xC+DA9M=
+github.com/aliyun/aliyun-oss-go-sdk v3.0.2+incompatible h1:8psS8a+wKfiLt1iVDX79F7Y6wUM49Lcha2FMXt4UM8g=
+github.com/aliyun/aliyun-oss-go-sdk v3.0.2+incompatible/go.mod h1:T/Aws4fEfogEE9v+HPhhw+CntffsBHJ8nXQCwKr0/g8=
+github.com/aliyun/aliyun-oss-go-sdk/v3 v3.0.0 h1:2nASaRzv3sew6ekk5Csxgfo3GKjhlLuzXq0qg30fhvc=
+github.com/aliyun/aliyun-oss-go-sdk/v3 v3.0.0/go.mod h1:Ssn0UMjRcWoLWREpAkxvL2QNgKAsxQWiN0ZL+NXJQSc=
 github.com/andybalholm/cascadia v1.3.2 h1:3Xi6Dw5lHF15JtdcmAHD3i1+T8plmv7BQ/nsViSLyss=
 github.com/andybalholm/cascadia v1.3.2/go.mod h1:7gtRlve5FxPPgIgX36uWBX58OdBsSS6lUvCFb+h7KvU=
 github.com/astaxie/beego v1.12.3/go.mod h1:p3qIm0Ryx7zeBHLljmd7omloyca1s4yu1a8kM1FkpIA=
+github.com/aws/aws-sdk-go v1.55.7 h1:UJrkFq7es5CShfBwlWAC8DA077vp8PyVbQd3lqLiztE=
+github.com/aws/aws-sdk-go v1.55.7/go.mod h1:eRwEWoyTWFMVYVQzKMNHWP5/RV4xIUGMQfXQHfHkpNU=
 github.com/beego/bee/v2 v2.1.0 h1:4WngbAnkvVOyKy74WXcRH3clon76wkjhuzrV2mx2fQU=
 github.com/beego/bee/v2 v2.1.0/go.mod h1:wDhKy5TNxv46LHKsK2gyxo38ObCOm9PbCN89lWHK3EU=
 github.com/beego/beego/v2 v2.1.4 h1:99d8+sUmQyfaArQIjjzWGE+KYU9M1GkfWec74nXQxFY=
@@ -112,6 +120,10 @@ github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+
 github.com/hashicorp/golang-lru v0.5.4 h1:YDjusn29QI/Das2iO9M0BHnIbxPeyuCHsjMW+lJfyTc=
 github.com/hashicorp/golang-lru v0.5.4/go.mod h1:iADmTwqILo4mZ8BN3D2Q6+9jd8WM5uGBxy+E8yxSoD4=
 github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU=
+github.com/jmespath/go-jmespath v0.4.0 h1:BEgLn5cpjn8UN1mAw4NjwDrS35OdebyEtFe+9YPoQUg=
+github.com/jmespath/go-jmespath v0.4.0/go.mod h1:T8mJZnbsbmF+m6zOOFylbeCJqk5+pHWvzYPziyZiYoo=
+github.com/jmespath/go-jmespath/internal/testify v1.5.1 h1:shLQSRRSCCPj3f2gpwzGwWFoC7ycTf1rcQZHOlsJ6N8=
+github.com/jmespath/go-jmespath/internal/testify v1.5.1/go.mod h1:L3OGu8Wl2/fWfCI6z80xFu9LTZmf1ZRjMHUOPmWr69U=
 github.com/josharian/intern v1.0.0 h1:vlS4z54oSdjm0bgjRigI+G1HpF+tI+9rE5LLzOg8HmY=
 github.com/josharian/intern v1.0.0/go.mod h1:5DoeVV0s6jJacbCEi61lwdGj/aVlrQvzHFFd8Hwg//Y=
 github.com/json-iterator/go v1.1.6/go.mod h1:+SdeFBvtyEkXs7REEP0seUULqWtbJapLOCVDaaPEHmU=
@@ -313,6 +325,8 @@ golang.org/x/text v0.9.0/go.mod h1:e1OnstbJyHTd6l/uOt8jFFHp6TRDWZR/bV3emEE/zU8=
 golang.org/x/text v0.12.0/go.mod h1:TvPlkZtksWOMsz7fbANvkp4WM8x/WCo/om8BMLbz+aE=
 golang.org/x/text v0.16.0 h1:a94ExnEXNtEwYLGJSIUxnWoxoRz/ZcCsV63ROupILh4=
 golang.org/x/text v0.16.0/go.mod h1:GhwF1Be+LQoKShO3cGOHzqOgRrGaYc9AvblQOmPVHnI=
+golang.org/x/time v0.4.0 h1:Z81tqI5ddIoXDPvVQ7/7CC9TnLM7ubaFG2qXYd5BbYY=
+golang.org/x/time v0.4.0/go.mod h1:3BpzKBy/shNhVucY/MWOyx10tF3SFh9QdLuxbVysPQM=
 golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
 golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
 golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc=
@@ -345,6 +359,8 @@ gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
 gopkg.in/yaml.v2 v2.2.4/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
 gopkg.in/yaml.v2 v2.2.5/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
 gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
+gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
+gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
 gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
 gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
 gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

+ 50 - 0
utils/aliyun_oss.go

@@ -0,0 +1,50 @@
+package utils
+
+import (
+	"fmt"
+	"github.com/aliyun/aliyun-oss-go-sdk/oss"
+	"time"
+)
+
+// UploadFile 上传文件
+func (m *AliyunService) UploadFile(fileName, filePath, savePath string) (resourceUrl string, err error) {
+	bucket, errBucketExists := m.client.Bucket(Bucketname)
+	if err != nil {
+		err = fmt.Errorf("BucketExists: %v; err: %v", Bucketname, errBucketExists)
+		FileLog.Error("BucketExists: %v; err: %v", Bucketname, errBucketExists)
+		return
+	}
+	path := fmt.Sprintf("%s%s%s/%s", MinIoUploadDir, time.Now().Format("200601/20060102/"), savePath, fileName)
+	if err != nil {
+		FileLog.Error("MinIo上传文件失败:", err)
+		return
+	}
+	err = bucket.PutObjectFromFile(path, filePath)
+	if err != nil {
+		FileLog.Error("Aliyun上传文件失败:", err)
+		return
+	}
+	resourceUrl = Imghost + path
+	return resourceUrl, err
+}
+
+func (m *AliyunService) Instance() OssClient {
+	if AliyunClient == nil {
+		aliyunOnce.Do(func() {
+			if AccessKeyId == "" {
+				FileLog.Warn("MinIo信息未配置,实例化客户端失败")
+				return
+			}
+			client, err := oss.New(Endpoint, AccessKeyId, AccessKeySecret)
+			if err != nil {
+				FileLog.Error("Aliyun连接失败:", err)
+				return
+			}
+
+			AliyunClient = &AliyunService{
+				client: client,
+			}
+		})
+	}
+	return AliyunClient
+}

+ 51 - 0
utils/config.go

@@ -146,6 +146,34 @@ var (
 	OssType              string
 )
 
+// S3配置
+var (
+	S3Endpoint        string
+	S3BackEndpoint    string
+	S3BucketName      string
+	S3UploadDir       string
+	S3AccessKeyId     string
+	S3AccessKeySecret string
+	S3Host            string
+	S3Region          string
+	S3ForceStyle      string
+	S3EndpointPort    string
+	S3Protocol        string
+	S3DisableSSL      string
+	S3OpenAcl         string
+)
+
+// 阿里云配置
+var (
+	Bucketname       string
+	Endpoint         string
+	Imghost          string
+	UploadDir        string
+	Upload_Audio_Dir string
+	AccessKeyId      string
+	AccessKeySecret  string
+)
+
 func init() {
 	tmpRunMode, err := web.AppConfig.String("run_mode")
 	if err != nil {
@@ -313,6 +341,29 @@ func init() {
 		if OssType == "" {
 			OssType = MINIO
 		}
+		// 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"]
+		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"]
+		S3Protocol = config["s3_protocol"]
+		S3DisableSSL = config["s3_disable_ssl"]
+		S3OpenAcl = config["s3_open_acl"]
+
+		//aliyun oss
+		Endpoint = config["endpoint"]
+		Bucketname = config["bucket_name"]
+		Imghost = config["img_host"]
+		UploadDir = config["upload_dir"]
+		Upload_Audio_Dir = config["upload_audio_dir"]
+		AccessKeyId = config["access_key_id"]
+		AccessKeySecret = config["access_key_secret"]
 	}
 }
 

+ 11 - 2
utils/oss_client.go

@@ -3,6 +3,7 @@ package utils
 import (
 	"context"
 	"fmt"
+	"github.com/aliyun/aliyun-oss-go-sdk/oss"
 	"github.com/minio/minio-go/v7"
 	"github.com/minio/minio-go/v7/pkg/credentials"
 	"sync"
@@ -15,22 +16,30 @@ type OssClient interface {
 }
 
 const (
-	MINIO = "minio"
+	MINIO  = "minio"
+	ALIYUN = "aliyun"
 )
 
 var (
 	ossClientMap = map[string]OssClient{
-		MINIO: &MinioOssService{},
+		MINIO:  &MinioOssService{},
+		ALIYUN: &AliyunService{},
 	}
 )
 
 type MinioOssService struct {
 	client *minio.Client
 }
+type AliyunService struct {
+	client *oss.Client
+}
 
 var (
 	minioOnce   sync.Once
+	aliyunOnce  sync.Once
 	MinioClient *MinioOssService
+
+	AliyunClient *AliyunService
 )
 
 func OssClientHandler(name string) (client OssClient, err error) {