正在显示
6 个修改的文件
包含
159 行增加
和
16 行删除
@@ -4,6 +4,7 @@ go 1.12 | @@ -4,6 +4,7 @@ go 1.12 | ||
4 | 4 | ||
5 | require ( | 5 | require ( |
6 | github.com/aliyun/alibaba-cloud-sdk-go v1.60.348 | 6 | github.com/aliyun/alibaba-cloud-sdk-go v1.60.348 |
7 | + github.com/aliyun/aliyun-oss-go-sdk v2.0.7+incompatible | ||
7 | github.com/astaxie/beego v1.10.0 | 8 | github.com/astaxie/beego v1.10.0 |
8 | github.com/disintegration/imaging v1.6.2 | 9 | github.com/disintegration/imaging v1.6.2 |
9 | github.com/gin-gonic/gin v1.4.0 | 10 | github.com/gin-gonic/gin v1.4.0 |
1 | package aliyun | 1 | package aliyun |
2 | 2 | ||
3 | import ( | 3 | import ( |
4 | + "github.com/aliyun/aliyun-oss-go-sdk/oss" | ||
4 | "github.com/astaxie/beego" | 5 | "github.com/astaxie/beego" |
6 | + "io" | ||
5 | ) | 7 | ) |
6 | 8 | ||
7 | /* | 9 | /* |
@@ -16,11 +18,58 @@ var ( | @@ -16,11 +18,58 @@ var ( | ||
16 | //EndpointCdn string = beego.AppConfig.String("end_point_cdn") | 18 | //EndpointCdn string = beego.AppConfig.String("end_point_cdn") |
17 | ) | 19 | ) |
18 | 20 | ||
19 | -//type OssSts struct{ | ||
20 | -// | ||
21 | -//} | 21 | +type OSSClient struct { |
22 | + client *oss.Client | ||
23 | + UpAddr UploadAddress | ||
24 | + Auth UploadAuth | ||
25 | +} | ||
22 | 26 | ||
23 | -//func NewSTS(){ | ||
24 | -// client,_ :=oos.NewClientWithAccessKey(RegionID,AccessKeyID,AccessKeySecret) | ||
25 | -// client.DoAction() | ||
26 | -//} | 27 | +//新建oss-client对象 |
28 | +func NewStsOSSClient(uploadAddress, uploadAuth string) (o *OSSClient, err error) { | ||
29 | + addr, e := ParseUploadAddress(uploadAddress) | ||
30 | + if e != nil { | ||
31 | + err = e | ||
32 | + return | ||
33 | + } | ||
34 | + auth, e := ParseUploadAuth(uploadAuth) | ||
35 | + if e != nil { | ||
36 | + err = e | ||
37 | + return | ||
38 | + } | ||
39 | + o, err = NewOSSClient(addr.Endpoint, auth.AccessKeyId, auth.AccessKeySecret, auth.SecurityToken) | ||
40 | + if err != nil { | ||
41 | + return | ||
42 | + } | ||
43 | + o.UpAddr = addr | ||
44 | + o.Auth = auth | ||
45 | + return | ||
46 | +} | ||
47 | +func NewOSSClient(ep, key, secret, token string) (o *OSSClient, err error) { | ||
48 | + o = &OSSClient{} | ||
49 | + o.client, err = oss.New(ep, key, secret, oss.SecurityToken(token)) | ||
50 | + if err != nil { | ||
51 | + return | ||
52 | + } | ||
53 | + return | ||
54 | +} | ||
55 | + | ||
56 | +//简单上传 (通过文件地址) | ||
57 | +func (o *OSSClient) PutObjectByUrl(bucketName string, fileName string, url string) error { | ||
58 | + r, err := GetFileFromUrl(url) | ||
59 | + if err != nil { | ||
60 | + return err | ||
61 | + } | ||
62 | + return o.PutObject(bucketName, fileName, r) | ||
63 | +} | ||
64 | +func (o *OSSClient) PutObjectByUrlDefault(url string) error { | ||
65 | + return o.PutObjectByUrl(o.UpAddr.Bucket, o.UpAddr.FileName, url) | ||
66 | +} | ||
67 | + | ||
68 | +//简单上传 | ||
69 | +func (o *OSSClient) PutObject(bucketName, objKey string, r io.Reader, options ...oss.Option) error { | ||
70 | + bucket, err := o.client.Bucket(bucketName) | ||
71 | + if err != nil { | ||
72 | + return err | ||
73 | + } | ||
74 | + return bucket.PutObject(objKey, r, options...) | ||
75 | +} |
1 | package aliyun | 1 | package aliyun |
2 | 2 | ||
3 | import ( | 3 | import ( |
4 | + "bufio" | ||
5 | + "bytes" | ||
4 | "encoding/base64" | 6 | "encoding/base64" |
5 | "encoding/json" | 7 | "encoding/json" |
6 | "fmt" | 8 | "fmt" |
9 | + "io" | ||
10 | + "net/http" | ||
7 | "strings" | 11 | "strings" |
8 | ) | 12 | ) |
9 | 13 | ||
@@ -13,6 +17,14 @@ type UploadAddress struct { | @@ -13,6 +17,14 @@ type UploadAddress struct { | ||
13 | FileName string | 17 | FileName string |
14 | } | 18 | } |
15 | 19 | ||
20 | +type UploadAuth struct { | ||
21 | + SecurityToken string //sts | ||
22 | + AccessKeyId string | ||
23 | + AccessKeySecret string | ||
24 | + Expiration string | ||
25 | + Region string | ||
26 | +} | ||
27 | + | ||
16 | //@cname 绑定域名 | 28 | //@cname 绑定域名 |
17 | func (up UploadAddress) GetFileUrl(cname string) string { | 29 | func (up UploadAddress) GetFileUrl(cname string) string { |
18 | if len(cname) > 0 { | 30 | if len(cname) > 0 { |
@@ -38,3 +50,36 @@ func ParseUploadAddress(uploadAddress string) (up UploadAddress, err error) { | @@ -38,3 +50,36 @@ func ParseUploadAddress(uploadAddress string) (up UploadAddress, err error) { | ||
38 | } | 50 | } |
39 | return | 51 | return |
40 | } | 52 | } |
53 | + | ||
54 | +//解析UploadAddress | ||
55 | +func ParseUploadAuth(uploadAuth string) (up UploadAuth, err error) { | ||
56 | + var jsData []byte | ||
57 | + jsData, err = base64.StdEncoding.DecodeString(uploadAuth) | ||
58 | + if err != nil { | ||
59 | + return | ||
60 | + } | ||
61 | + if err = json.Unmarshal(jsData, &up); err != nil { | ||
62 | + return | ||
63 | + } | ||
64 | + return | ||
65 | +} | ||
66 | + | ||
67 | +//从文件地址下载数据 | ||
68 | +func GetFileFromUrl(url string) (r io.Reader, err error) { | ||
69 | + if !strings.Contains(url, "http") { | ||
70 | + return nil, fmt.Errorf("异常路径:%v", url) | ||
71 | + } | ||
72 | + //下载 | ||
73 | + rsp, err := http.Get(strings.TrimSpace(url)) | ||
74 | + if err != nil { | ||
75 | + return | ||
76 | + } | ||
77 | + var buf *bytes.Buffer = bytes.NewBuffer(nil) | ||
78 | + buf.ReadFrom(rsp.Body) | ||
79 | + r = bufio.NewReader(buf) | ||
80 | + defer rsp.Body.Close() | ||
81 | + if err != nil { | ||
82 | + return | ||
83 | + } | ||
84 | + return | ||
85 | +} |
internal/aliyun/utils_test.go
0 → 100644
1 | +package aliyun | ||
2 | + | ||
3 | +import ( | ||
4 | + "bytes" | ||
5 | + "testing" | ||
6 | +) | ||
7 | + | ||
8 | +func Test_GetFile(t *testing.T) { | ||
9 | + io, err := GetFileFromUrl("http://mmm-opp-dev.fjmaimaimai.com/file/opp/image/20200106/1578297295_RxJscRHCzQmxeseTAEQXTH7A7ZK6z4Fz.jpg") | ||
10 | + if err != nil { | ||
11 | + t.Log(err) | ||
12 | + return | ||
13 | + } | ||
14 | + var buf bytes.Buffer | ||
15 | + n, _ := buf.ReadFrom(io) | ||
16 | + t.Log("read from reader:", n) | ||
17 | +} |
@@ -544,7 +544,7 @@ type Picture struct { | @@ -544,7 +544,7 @@ type Picture struct { | ||
544 | H int `json:"h"` | 544 | H int `json:"h"` |
545 | ImageId string `json:"imageId"` //imageId | 545 | ImageId string `json:"imageId"` //imageId |
546 | PathBak string `json:"path_bak"` | 546 | PathBak string `json:"path_bak"` |
547 | - JobId string `json:"job_id"` | 547 | + //JobId string `json:"job_id"` |
548 | } | 548 | } |
549 | 549 | ||
550 | //视频 | 550 | //视频 |
@@ -2,12 +2,14 @@ package contrab | @@ -2,12 +2,14 @@ package contrab | ||
2 | 2 | ||
3 | import ( | 3 | import ( |
4 | "fmt" | 4 | "fmt" |
5 | + "github.com/aliyun/alibaba-cloud-sdk-go/services/vod" | ||
5 | "github.com/astaxie/beego/orm" | 6 | "github.com/astaxie/beego/orm" |
6 | "gitlab.fjmaimaimai.com/mmm-go/gocomm/pkg/log" | 7 | "gitlab.fjmaimaimai.com/mmm-go/gocomm/pkg/log" |
7 | "opp/internal/aliyun" | 8 | "opp/internal/aliyun" |
8 | "opp/internal/utils" | 9 | "opp/internal/utils" |
9 | "opp/models" | 10 | "opp/models" |
10 | "opp/protocol" | 11 | "opp/protocol" |
12 | + "path/filepath" | ||
11 | ) | 13 | ) |
12 | 14 | ||
13 | const LoopSize = 1 | 15 | const LoopSize = 1 |
@@ -71,18 +73,47 @@ func uploadImages(d *models.ChanceData, img string) (rsp []protocol.Picture) { | @@ -71,18 +73,47 @@ func uploadImages(d *models.ChanceData, img string) (rsp []protocol.Picture) { | ||
71 | printInfo(d, p.Path, Uploaded) | 73 | printInfo(d, p.Path, Uploaded) |
72 | continue | 74 | continue |
73 | } | 75 | } |
74 | - //上传图片 | ||
75 | - callBack := aliyun.NewCallBackHttp(getCallBackUrl()) | ||
76 | - extend := NewChanceDataExtend(d, aliyun.FileImage, p.Path) | ||
77 | - response, err := aliyun.UploadMediaByURL([]string{p.Path}, callBack, extend) | ||
78 | - if err != nil || response == nil || len(response.UploadJobs) == 0 { | ||
79 | - printInfo(d, p.Path, UploadFail) | ||
80 | - continue | 76 | + |
77 | + var ( | ||
78 | + err error | ||
79 | + vodc *vod.Client | ||
80 | + imageRsp *aliyun.CreateUploadImageResponse | ||
81 | + client *aliyun.OSSClient | ||
82 | + ) | ||
83 | + vodc, _ = aliyun.DefaultVodClient() | ||
84 | + list := filepath.SplitList(p.Path) | ||
85 | + if len(list) == 0 { | ||
86 | + err = fmt.Errorf("路径无效:%v", p.Path) | ||
87 | + goto ERR | ||
88 | + } | ||
89 | + if err != nil { | ||
90 | + goto ERR | ||
91 | + } | ||
92 | + imageRsp, err = aliyun.CreateUploadImage(vodc, &aliyun.CreateUploadImageRequest{FileName: list[len(list)-1]}) | ||
93 | + if err != nil { | ||
94 | + goto ERR | ||
81 | } | 95 | } |
96 | + client, err = aliyun.NewStsOSSClient(imageRsp.UploadAddress, imageRsp.UploadAuth) | ||
97 | + if err != nil { | ||
98 | + goto ERR | ||
99 | + } | ||
100 | + err = client.PutObjectByUrlDefault(p.Path) | ||
101 | + if err != nil { | ||
102 | + goto ERR | ||
103 | + } | ||
104 | + | ||
82 | //备份路径 | 105 | //备份路径 |
83 | rsp[i].PathBak = p.Path | 106 | rsp[i].PathBak = p.Path |
84 | - rsp[i].JobId = response.UploadJobs[0].JobId | 107 | + rsp[i].ImageId = imageRsp.ImageId |
108 | + rsp[i].Path = imageRsp.ImageURL | ||
85 | printInfo(d, p.Path, UploadSuccess) | 109 | printInfo(d, p.Path, UploadSuccess) |
110 | + continue | ||
111 | + ERR: | ||
112 | + { | ||
113 | + log.Error("ali vod error:", err) | ||
114 | + printInfo(d, p.Path, UploadFail) | ||
115 | + continue | ||
116 | + } | ||
86 | } | 117 | } |
87 | return | 118 | return |
88 | } | 119 | } |
-
请 注册 或 登录 后发表评论