作者 yangfu

修改 阿里云媒体迁移

... ... @@ -4,6 +4,7 @@ go 1.12
require (
github.com/aliyun/alibaba-cloud-sdk-go v1.60.348
github.com/aliyun/aliyun-oss-go-sdk v2.0.7+incompatible
github.com/astaxie/beego v1.10.0
github.com/disintegration/imaging v1.6.2
github.com/gin-gonic/gin v1.4.0
... ...
package aliyun
import (
"github.com/aliyun/aliyun-oss-go-sdk/oss"
"github.com/astaxie/beego"
"io"
)
/*
... ... @@ -16,11 +18,58 @@ var (
//EndpointCdn string = beego.AppConfig.String("end_point_cdn")
)
//type OssSts struct{
//
//}
type OSSClient struct {
client *oss.Client
UpAddr UploadAddress
Auth UploadAuth
}
//func NewSTS(){
// client,_ :=oos.NewClientWithAccessKey(RegionID,AccessKeyID,AccessKeySecret)
// client.DoAction()
//}
//新建oss-client对象
func NewStsOSSClient(uploadAddress, uploadAuth string) (o *OSSClient, err error) {
addr, e := ParseUploadAddress(uploadAddress)
if e != nil {
err = e
return
}
auth, e := ParseUploadAuth(uploadAuth)
if e != nil {
err = e
return
}
o, err = NewOSSClient(addr.Endpoint, auth.AccessKeyId, auth.AccessKeySecret, auth.SecurityToken)
if err != nil {
return
}
o.UpAddr = addr
o.Auth = auth
return
}
func NewOSSClient(ep, key, secret, token string) (o *OSSClient, err error) {
o = &OSSClient{}
o.client, err = oss.New(ep, key, secret, oss.SecurityToken(token))
if err != nil {
return
}
return
}
//简单上传 (通过文件地址)
func (o *OSSClient) PutObjectByUrl(bucketName string, fileName string, url string) error {
r, err := GetFileFromUrl(url)
if err != nil {
return err
}
return o.PutObject(bucketName, fileName, r)
}
func (o *OSSClient) PutObjectByUrlDefault(url string) error {
return o.PutObjectByUrl(o.UpAddr.Bucket, o.UpAddr.FileName, url)
}
//简单上传
func (o *OSSClient) PutObject(bucketName, objKey string, r io.Reader, options ...oss.Option) error {
bucket, err := o.client.Bucket(bucketName)
if err != nil {
return err
}
return bucket.PutObject(objKey, r, options...)
}
... ...
package aliyun
import (
"bufio"
"bytes"
"encoding/base64"
"encoding/json"
"fmt"
"io"
"net/http"
"strings"
)
... ... @@ -13,6 +17,14 @@ type UploadAddress struct {
FileName string
}
type UploadAuth struct {
SecurityToken string //sts
AccessKeyId string
AccessKeySecret string
Expiration string
Region string
}
//@cname 绑定域名
func (up UploadAddress) GetFileUrl(cname string) string {
if len(cname) > 0 {
... ... @@ -38,3 +50,36 @@ func ParseUploadAddress(uploadAddress string) (up UploadAddress, err error) {
}
return
}
//解析UploadAddress
func ParseUploadAuth(uploadAuth string) (up UploadAuth, err error) {
var jsData []byte
jsData, err = base64.StdEncoding.DecodeString(uploadAuth)
if err != nil {
return
}
if err = json.Unmarshal(jsData, &up); err != nil {
return
}
return
}
//从文件地址下载数据
func GetFileFromUrl(url string) (r io.Reader, err error) {
if !strings.Contains(url, "http") {
return nil, fmt.Errorf("异常路径:%v", url)
}
//下载
rsp, err := http.Get(strings.TrimSpace(url))
if err != nil {
return
}
var buf *bytes.Buffer = bytes.NewBuffer(nil)
buf.ReadFrom(rsp.Body)
r = bufio.NewReader(buf)
defer rsp.Body.Close()
if err != nil {
return
}
return
}
... ...
package aliyun
import (
"bytes"
"testing"
)
func Test_GetFile(t *testing.T) {
io, err := GetFileFromUrl("http://mmm-opp-dev.fjmaimaimai.com/file/opp/image/20200106/1578297295_RxJscRHCzQmxeseTAEQXTH7A7ZK6z4Fz.jpg")
if err != nil {
t.Log(err)
return
}
var buf bytes.Buffer
n, _ := buf.ReadFrom(io)
t.Log("read from reader:", n)
}
... ...
... ... @@ -544,7 +544,7 @@ type Picture struct {
H int `json:"h"`
ImageId string `json:"imageId"` //imageId
PathBak string `json:"path_bak"`
JobId string `json:"job_id"`
//JobId string `json:"job_id"`
}
//视频
... ...
... ... @@ -2,12 +2,14 @@ package contrab
import (
"fmt"
"github.com/aliyun/alibaba-cloud-sdk-go/services/vod"
"github.com/astaxie/beego/orm"
"gitlab.fjmaimaimai.com/mmm-go/gocomm/pkg/log"
"opp/internal/aliyun"
"opp/internal/utils"
"opp/models"
"opp/protocol"
"path/filepath"
)
const LoopSize = 1
... ... @@ -71,18 +73,47 @@ func uploadImages(d *models.ChanceData, img string) (rsp []protocol.Picture) {
printInfo(d, p.Path, Uploaded)
continue
}
//上传图片
callBack := aliyun.NewCallBackHttp(getCallBackUrl())
extend := NewChanceDataExtend(d, aliyun.FileImage, p.Path)
response, err := aliyun.UploadMediaByURL([]string{p.Path}, callBack, extend)
if err != nil || response == nil || len(response.UploadJobs) == 0 {
printInfo(d, p.Path, UploadFail)
continue
var (
err error
vodc *vod.Client
imageRsp *aliyun.CreateUploadImageResponse
client *aliyun.OSSClient
)
vodc, _ = aliyun.DefaultVodClient()
list := filepath.SplitList(p.Path)
if len(list) == 0 {
err = fmt.Errorf("路径无效:%v", p.Path)
goto ERR
}
if err != nil {
goto ERR
}
imageRsp, err = aliyun.CreateUploadImage(vodc, &aliyun.CreateUploadImageRequest{FileName: list[len(list)-1]})
if err != nil {
goto ERR
}
client, err = aliyun.NewStsOSSClient(imageRsp.UploadAddress, imageRsp.UploadAuth)
if err != nil {
goto ERR
}
err = client.PutObjectByUrlDefault(p.Path)
if err != nil {
goto ERR
}
//备份路径
rsp[i].PathBak = p.Path
rsp[i].JobId = response.UploadJobs[0].JobId
rsp[i].ImageId = imageRsp.ImageId
rsp[i].Path = imageRsp.ImageURL
printInfo(d, p.Path, UploadSuccess)
continue
ERR:
{
log.Error("ali vod error:", err)
printInfo(d, p.Path, UploadFail)
continue
}
}
return
}
... ...