From b4b55809c444fb9db1798ec2d54dc14e51557e81 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E5=85=89=E6=98=A5?= Date: Tue, 17 May 2022 21:00:23 +0800 Subject: [PATCH] =?UTF-8?q?-=20=E5=A2=9E=E5=8A=A0=E8=85=BE=E8=AE=AF?= =?UTF-8?q?=E4=BA=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- aliyun.go | 17 +++-------------- connom.go | 8 ++++++++ go.mod | 9 ++++++++- go.sum | 25 +++++++++++++++++++++++++ tencent.go | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 94 insertions(+), 15 deletions(-) create mode 100644 connom.go diff --git a/aliyun.go b/aliyun.go index cd599a5..cb38d5a 100644 --- a/aliyun.go +++ b/aliyun.go @@ -16,25 +16,14 @@ type AliYun struct { } // NewAliYun 初始化 -func NewAliYun(endpoint string, accessKeyId string, accessKeySecret string) *AliYun { +// https://help.aliyun.com/document_detail/32144.html +func NewAliYun(endpoint, accessKeyId, accessKeySecret, bucketName string) *AliYun { app := &AliYun{Endpoint: endpoint, AccessKeyId: accessKeyId, AccessKeySecret: accessKeySecret} app.client, app.error = oss.New(endpoint, accessKeyId, accessKeySecret) + app.bucket, app.error = app.client.Bucket(bucketName) return app } -// Bucket 存储空间 -func (c *AliYun) Bucket(name string) *AliYun { - c.bucket, c.error = c.client.Bucket(name) - return c -} - -// FileInfo 上传文件的信息 -type FileInfo struct { - Path string // 文件路径 - Name string // 文件名称 - Url string // 文件地址 -} - // PutObject 上传文件流 // @param file 文件流 // @param filePath 文件路径 diff --git a/connom.go b/connom.go new file mode 100644 index 0000000..4533f58 --- /dev/null +++ b/connom.go @@ -0,0 +1,8 @@ +package gostorage + +// FileInfo 上传文件的信息 +type FileInfo struct { + Path string // 文件路径 + Name string // 文件名称 + Url string // 文件地址 +} diff --git a/go.mod b/go.mod index 6006b5c..0342d78 100644 --- a/go.mod +++ b/go.mod @@ -2,10 +2,17 @@ module go.dtapp.net/gostorage go 1.18 -require github.com/aliyun/aliyun-oss-go-sdk v2.2.3+incompatible +require ( + github.com/aliyun/aliyun-oss-go-sdk v2.2.3+incompatible + github.com/tencentyun/cos-go-sdk-v5 v0.7.34 +) require ( github.com/baiyubin/aliyun-sts-go-sdk v0.0.0-20180326062324-cfa1a18b161f // indirect + github.com/clbanning/mxj v1.8.4 // indirect + github.com/google/go-querystring v1.1.0 // indirect + github.com/mitchellh/mapstructure v1.5.0 // indirect + github.com/mozillazg/go-httpheader v0.3.1 // indirect github.com/satori/go.uuid v1.2.0 // indirect golang.org/x/time v0.0.0-20220411224347-583f2d630306 // indirect gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c // indirect diff --git a/go.sum b/go.sum index bd8fbaa..be94f7d 100644 --- a/go.sum +++ b/go.sum @@ -1,15 +1,40 @@ +github.com/QcloudApi/qcloud_sign_golang v0.0.0-20141224014652-e4130a326409/go.mod h1:1pk82RBxDY/JZnPQrtqHlUFfCctgdorsd9M06fMynOM= github.com/aliyun/aliyun-oss-go-sdk v2.2.3+incompatible h1:KlwIELiuuvj7uMeEXrgXecPE3+xz/gGsDSnhL6Eztq0= github.com/aliyun/aliyun-oss-go-sdk v2.2.3+incompatible/go.mod h1:T/Aws4fEfogEE9v+HPhhw+CntffsBHJ8nXQCwKr0/g8= github.com/baiyubin/aliyun-sts-go-sdk v0.0.0-20180326062324-cfa1a18b161f h1:ZNv7On9kyUzm7fvRZumSyy/IUiSC7AzL0I1jKKtwooA= github.com/baiyubin/aliyun-sts-go-sdk v0.0.0-20180326062324-cfa1a18b161f/go.mod h1:AuiFmCCPBSrqvVMvuqFuk0qogytodnVFVSN5CeJB8Gc= +github.com/clbanning/mxj v1.8.4 h1:HuhwZtbyvyOw+3Z1AowPkU87JkJUSv751ELWaiTpj8I= +github.com/clbanning/mxj v1.8.4/go.mod h1:BVjHeAH+rl9rs6f+QIpeRl0tfu10SXn1pUSa5PVGJng= +github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/google/go-cmp v0.5.2 h1:X2ev0eStA3AbceY54o37/0PQ/UWqKEiiO2dKL5OPaFM= +github.com/google/go-cmp v0.5.2/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= +github.com/google/go-querystring v1.0.0/go.mod h1:odCYkC5MyYFN7vkCjXpyrEuKhc/BUO6wN/zVPAxq5ck= +github.com/google/go-querystring v1.1.0 h1:AnCroh3fv4ZBgVIf1Iwtovgjaw/GiKJo8M8yD/fhyJ8= +github.com/google/go-querystring v1.1.0/go.mod h1:Kcdr2DB4koayq7X8pmAG4sNG59So17icRSOU623lUBU= +github.com/google/uuid v1.1.1 h1:Gkbcsh/GbpXz7lPftLA3P6TYMwjCLYm83jiFQZF/3gY= +github.com/google/uuid v1.1.1/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= github.com/kr/pretty v0.2.1 h1:Fmg33tUaq4/8ym9TJN1x7sLJnHVwhP33CNkpYV/7rwI= github.com/kr/pretty v0.2.1/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI= github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE= github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= +github.com/mitchellh/mapstructure v1.4.3/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo= +github.com/mitchellh/mapstructure v1.5.0 h1:jeMsZIYE/09sWLaz43PL7Gy6RuMjD2eJVyuac5Z2hdY= +github.com/mitchellh/mapstructure v1.5.0/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo= +github.com/mozillazg/go-httpheader v0.2.1/go.mod h1:jJ8xECTlalr6ValeXYdOF8fFUISeBAdw6E61aqQma60= +github.com/mozillazg/go-httpheader v0.3.1 h1:IRP+HFrMX2SlwY9riuio7raffXUpzAosHtZu25BSJok= +github.com/mozillazg/go-httpheader v0.3.1/go.mod h1:PuT8h0pw6efvp8ZeUec1Rs7dwjK08bt6gKSReGMqtdA= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/satori/go.uuid v1.2.0 h1:0uYX9dsZ2yD7q2RtLRtPSdGDWzjeM3TbMJP9utgA0ww= github.com/satori/go.uuid v1.2.0/go.mod h1:dA0hQrYB0VpLJoorglMZABFdXlWrHn1NEOzdhQKdks0= +github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= +github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common v1.0.194/go.mod h1:7sCQWVkxcsR38nffDW057DRGk8mUjK1Ing/EFOK8s8Y= +github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/kms v1.0.194/go.mod h1:yrBKWhChnDqNz1xuXdSbWXG56XawEq0G5j1lg4VwBD4= +github.com/tencentyun/cos-go-sdk-v5 v0.7.34 h1:xm+Pg+6m486y4eugRI7/E4WasbVmpY1hp9QBSRErgp8= +github.com/tencentyun/cos-go-sdk-v5 v0.7.34/go.mod h1:4dCEtLHGh8QPxHEkgq+nFaky7yZxQuYwgSJM87icDaw= golang.org/x/time v0.0.0-20220411224347-583f2d630306 h1:+gHMid33q6pen7kv9xvT+JRinntgeXO2AeZVd0AWD3w= golang.org/x/time v0.0.0-20220411224347-583f2d630306/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ= +golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk= gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q= diff --git a/tencent.go b/tencent.go index a613a45..e2037f9 100644 --- a/tencent.go +++ b/tencent.go @@ -1 +1,51 @@ package gostorage + +import ( + "context" + "fmt" + "github.com/tencentyun/cos-go-sdk-v5" + "io" + "net/http" + "net/url" +) + +type Tencent struct { + BucketName string // 存储桶名称 + Regions string // 所属地域 + SecretID string // 用户的 SecretId + SecretKey string // 用户的 SecretKey + client *cos.Client // 驱动 +} + +// NewTencent 初始化 +// https://cloud.tencent.com/document/product/436/31215 +func NewTencent(bucketName, regions, secretID, secretKey string) *Tencent { + app := &Tencent{BucketName: bucketName, Regions: regions, SecretID: secretID, SecretKey: secretKey} + u, _ := url.Parse(fmt.Sprintf("https://%s.cos.%s.myqcloud.com", bucketName, regions)) + su, _ := url.Parse(fmt.Sprintf("https://cos.%s.myqcloud.com", regions)) + b := &cos.BaseURL{BucketURL: u, ServiceURL: su} + // 1.永久密钥 + app.client = cos.NewClient(b, &http.Client{ + Transport: &cos.AuthorizationTransport{ + SecretID: secretID, + SecretKey: secretKey, + }, + }) + return app +} + +// PutObject 上传文件流 +// @param file 文件流 +// @param filePath 文件路径 +// @param fileName 文件名称 +func (c *Tencent) PutObject(file io.Reader, filePath, fileName string) (resp FileInfo, err error) { + objectKey := filePath + if fileName != "" { + objectKey = filePath + "/" + fileName + } + _, err = c.client.Object.Put(context.Background(), objectKey, file, nil) + resp.Path = filePath + resp.Name = fileName + resp.Url = objectKey + return +}