作者 yangfu

修改:1.推送类型

增加:1.oss文件上传 2.sts授权
@@ -9,6 +9,9 @@ AccessKeyID ="LTAI4FhiZ3UktC6N1u3H5GFC" @@ -9,6 +9,9 @@ AccessKeyID ="LTAI4FhiZ3UktC6N1u3H5GFC"
9 AccessKeySecret ="UyspWwdni55CYQ02hUCint4qY2jNYO" 9 AccessKeySecret ="UyspWwdni55CYQ02hUCint4qY2jNYO"
10 cname ="https://media.goexample.live/" 10 cname ="https://media.goexample.live/"
11 11
  12 +OssEndPoint ="oss-cn-shanghai.aliyuncs.com"
  13 +BuckName ="mmm-vod-dev-public"
  14 +
12 15
13 #友盟推送 16 #友盟推送
14 UMENG_API_HOST = "http://msg.umeng.com" 17 UMENG_API_HOST = "http://msg.umeng.com"
@@ -5,6 +5,7 @@ go 1.12 @@ -5,6 +5,7 @@ go 1.12
5 require ( 5 require (
6 github.com/ajg/form v1.5.1 // indirect 6 github.com/ajg/form v1.5.1 // indirect
7 github.com/aliyun/alibaba-cloud-sdk-go v1.60.348 7 github.com/aliyun/alibaba-cloud-sdk-go v1.60.348
  8 + github.com/aliyun/aliyun-oss-go-sdk v2.1.4+incompatible
8 github.com/aliyun/aliyun-sts-go-sdk v0.0.0-20171106034748-98d3903a2309 9 github.com/aliyun/aliyun-sts-go-sdk v0.0.0-20171106034748-98d3903a2309
9 github.com/astaxie/beego v1.10.0 10 github.com/astaxie/beego v1.10.0
10 github.com/fasthttp-contrib/websocket v0.0.0-20160511215533-1f3b11f56072 // indirect 11 github.com/fasthttp-contrib/websocket v0.0.0-20160511215533-1f3b11f56072 // indirect
  1 +package oss
  2 +
  3 +import (
  4 + "bytes"
  5 + "fmt"
  6 + "github.com/aliyun/aliyun-oss-go-sdk/oss"
  7 + "io/ioutil"
  8 + "mime/multipart"
  9 + "openapi/pkg/constant"
  10 + protocol "openapi/pkg/domain"
  11 + "openapi/pkg/infrastructure/aliyun"
  12 + "openapi/pkg/infrastructure/log"
  13 + "openapi/pkg/infrastructure/utils"
  14 + "path/filepath"
  15 +)
  16 +
  17 +type BuckObject struct {
  18 + DefaultHost string `json:"host"`
  19 + Key string `json:"key"`
  20 + Path string `json:"path"`
  21 + FileName string `json:"fileName"`
  22 +}
  23 +
  24 +//创建视频上传凭证
  25 +func CreateOssUpload(header *protocol.RequestHeader, MultipartForm *multipart.Form) (rsp interface{}, err error) {
  26 + var objects []aliyun.Object
  27 + var listPath []BuckObject
  28 + var bucket *oss.Bucket
  29 + bucket, err = aliyun.DefaultBucket()
  30 + if err != nil {
  31 + log.Error(err.Error())
  32 + return
  33 + }
  34 + for k, _ := range MultipartForm.File {
  35 + v := MultipartForm.File[k]
  36 + for i := range v {
  37 + if v[i].Size > 1024*1024*50 {
  38 + continue
  39 + }
  40 + f, _ := v[i].Open()
  41 + data, _ := ioutil.ReadAll(f)
  42 + f.Close()
  43 + reader := bytes.NewBuffer(data)
  44 + fileBase := filepath.Base(v[i].Filename)
  45 + key := utils.GetFileName(header.AppProject, aliyun.FileObject, fileBase)
  46 + objects = append(objects, aliyun.Object{
  47 + Key: key,
  48 + Value: reader,
  49 + })
  50 + listPath = append(listPath, BuckObject{
  51 + DefaultHost: fmt.Sprintf("https://%v.%v", constant.BuckName, constant.OssEndPoint),
  52 + Path: fmt.Sprintf("https://%v.%v/%v", constant.BuckName, constant.OssEndPoint, key),
  53 + Key: key,
  54 + FileName: fileBase,
  55 + })
  56 + }
  57 + }
  58 + if len(objects) == 0 {
  59 + return
  60 + }
  61 + err = aliyun.CreateObjects(bucket, objects...)
  62 + if err != nil {
  63 + log.Error(err.Error())
  64 + return
  65 + }
  66 + rsp = listPath
  67 + return
  68 +}
  69 +
  70 +//创建视频上传凭证
  71 +func CreateStsAuth(header *protocol.RequestHeader, files []string) (rsp interface{}, err error) {
  72 + access, e := aliyun.DefaultSts()
  73 + if e != nil {
  74 + err = e
  75 + log.Error(e.Error())
  76 + }
  77 + var listPath []BuckObject
  78 + for _, v := range files {
  79 + fileBase := filepath.Base(v)
  80 + key := utils.GetFileName(header.AppProject, aliyun.FileObject, fileBase)
  81 + listPath = append(listPath, BuckObject{
  82 + DefaultHost: fmt.Sprintf("https://%v.%v", constant.BuckName, constant.OssEndPoint),
  83 + Path: fmt.Sprintf("https://%v.%v/%v", constant.BuckName, constant.OssEndPoint, key),
  84 + Key: key,
  85 + FileName: fileBase,
  86 + })
  87 + }
  88 + return map[string]interface{}{"access": access, "files": listPath}, nil
  89 +}
  1 +package constant
  2 +
  3 +var (
  4 + OssEndPoint = "oss-cn-shanghai.aliyuncs.com"
  5 + BuckName = "mmm-vod-dev-public"
  6 + //OssAccessKeyID string = "LTAI4GGkZMdDoxRpehefbQhV"
  7 + //OssAccessKeySecret string = "tytXo0RwxhGa3luyfmBB6PKe8M6Uu4"
  8 +)
  9 +
  10 +func init() {
  11 + OssEndPoint = config.StringDefault("OssEndPoint", OssEndPoint)
  12 + BuckName = config.StringDefault("BuckName", BuckName)
  13 + //OssAccessKeyID = config.StringDefault("AccessKeyID", OssAccessKeyID)
  14 + //OssAccessKeySecret = config.StringDefault("OssAccessKeyID", OssAccessKeySecret)
  15 +}
@@ -10,6 +10,7 @@ const ( @@ -10,6 +10,7 @@ const (
10 FileImage = "image" 10 FileImage = "image"
11 FileVoice = "voice" 11 FileVoice = "voice"
12 FileVideo = "video" 12 FileVideo = "video"
  13 + FileObject = "object"
13 ) 14 )
14 15
15 const ( 16 const (
1 package aliyun 1 package aliyun
2 2
  3 +import (
  4 + "github.com/aliyun/aliyun-oss-go-sdk/oss"
  5 + "io"
  6 + "openapi/pkg/constant"
  7 +)
  8 +
3 /* 9 /*
4 AK AccessKey 完全访问权限 10 AK AccessKey 完全访问权限
5 STS (Security Token Service) 临时访问控制 11 STS (Security Token Service) 临时访问控制
@@ -12,3 +18,46 @@ type ossConfig struct { @@ -12,3 +18,46 @@ type ossConfig struct {
12 BuckName string 18 BuckName string
13 CallbackUrl string 19 CallbackUrl string
14 } 20 }
  21 +
  22 +func DefaultBucket() (*oss.Bucket, error) {
  23 + client, err := oss.New(constant.OssEndPoint, constant.AccessKeyID, constant.AccessKeySecret)
  24 + if err != nil {
  25 + return nil, err
  26 + }
  27 +
  28 + // Get bucket
  29 + bucket, err := client.Bucket(constant.BuckName)
  30 + if err != nil {
  31 + return nil, err
  32 + }
  33 +
  34 + return bucket, nil
  35 +}
  36 +
  37 +type Object struct {
  38 + Key string
  39 + Value io.Reader
  40 +}
  41 +
  42 +// CreateObjects creates some objects
  43 +func CreateObjects(bucket *oss.Bucket, objects ...Object) error {
  44 + for _, object := range objects {
  45 + err := bucket.PutObject(object.Key, object.Value)
  46 + if err != nil {
  47 + return err
  48 + }
  49 + }
  50 + return nil
  51 +}
  52 +
  53 +// DeleteObjects deletes some objects.
  54 +// object = key
  55 +func DeleteObjects(bucket *oss.Bucket, objects ...string) error {
  56 + for _, object := range objects {
  57 + err := bucket.DeleteObject(object)
  58 + if err != nil {
  59 + return err
  60 + }
  61 + }
  62 + return nil
  63 +}
  1 +package aliyun
  2 +
  3 +import (
  4 + "fmt"
  5 + "github.com/aliyun/alibaba-cloud-sdk-go/services/sts"
  6 + "openapi/pkg/constant"
  7 + "openapi/pkg/infrastructure/log"
  8 +)
  9 +
  10 +func DefaultSts() (interface{}, error) {
  11 + //构建一个阿里云客户端, 用于发起请求。
  12 + //构建阿里云客户端时,需要设置AccessKey ID和AccessKey Secret。
  13 + client, err := sts.NewClientWithAccessKey(constant.RegionID, constant.AccessKeyID, constant.AccessKeySecret)
  14 + if err != nil {
  15 + return nil, err
  16 + }
  17 +
  18 + //构建请求对象。
  19 + request := sts.CreateAssumeRoleRequest()
  20 + request.Scheme = "https"
  21 +
  22 + //设置参数。 指定角色的ARN。格式:acs:ram::$accountID:role/$roleName/$RoleSessionName 。 mmm-go@1373671070046453.onaliyun.com
  23 + // 配置用户 - ram角色权限-oss授权
  24 + // $accountID 用户的阿里云账号
  25 + // $RoleArn 点到ram角色 具体的权限详情 格式 格式:acs:ram::$accountID:role/$roleName/$RoleSessionName
  26 + request.RoleArn = fmt.Sprintf("acs:ram::1373671070046453:role/role-oss-sts")
  27 + // 会话名称
  28 + request.RoleSessionName = "role-oss-sts-session"
  29 +
  30 + //发起请求,并得到响应。
  31 + response, e := client.AssumeRole(request)
  32 + if e != nil {
  33 + err = e
  34 + log.Error(e.Error())
  35 + }
  36 + return map[string]interface{}{
  37 + "accessKeySecret": response.Credentials.AccessKeySecret,
  38 + "expiration": response.Credentials.Expiration,
  39 + "accessKeyId": response.Credentials.AccessKeyId,
  40 + "securityToken": response.Credentials.SecurityToken,
  41 + }, nil
  42 +}
@@ -165,7 +165,7 @@ func NewAps(options *push.Options) (v *Aps) { @@ -165,7 +165,7 @@ func NewAps(options *push.Options) (v *Aps) {
165 v = &Aps{ 165 v = &Aps{
166 Alert: NewAlert(options), 166 Alert: NewAlert(options),
167 AutoBadge: "+1", 167 AutoBadge: "+1",
168 - ContentAvailable: 1, 168 + ContentAvailable: 0,
169 } 169 }
170 return 170 return
171 } 171 }
@@ -3,6 +3,7 @@ package v1 @@ -3,6 +3,7 @@ package v1
3 import ( 3 import (
4 "encoding/json" 4 "encoding/json"
5 "gitlab.fjmaimaimai.com/mmm-go/gocomm/pkg/log" 5 "gitlab.fjmaimaimai.com/mmm-go/gocomm/pkg/log"
  6 + oss "openapi/pkg/application/oss/service"
6 vod "openapi/pkg/application/vod/service" 7 vod "openapi/pkg/application/vod/service"
7 "openapi/pkg/domain" 8 "openapi/pkg/domain"
8 "openapi/pkg/infrastructure/aliyun" 9 "openapi/pkg/infrastructure/aliyun"
@@ -197,3 +198,32 @@ func (this *VodController) GetVideoPlayAuth() { @@ -197,3 +198,32 @@ func (this *VodController) GetVideoPlayAuth() {
197 header := controllers.GetRequestHeader(this.Ctx) 198 header := controllers.GetRequestHeader(this.Ctx)
198 msg = domain.NewReturnResponse(vod.GetVideoPlayAuth(header, request)) 199 msg = domain.NewReturnResponse(vod.GetVideoPlayAuth(header, request))
199 } 200 }
  201 +
  202 +//归档对象 PutObject
  203 +// @router /putObject [post]
  204 +func (this *VodController) PutObject() {
  205 + var msg *domain.ResponseMessage
  206 + defer func() {
  207 + this.Resp(msg)
  208 + }()
  209 +
  210 + header := controllers.GetRequestHeader(this.Ctx)
  211 + msg = domain.NewReturnResponse(oss.CreateOssUpload(header, this.Ctx.Request.MultipartForm))
  212 +}
  213 +
  214 +// STS授权 CreateStsAuth
  215 +// @router /createStsAuth [post]
  216 +func (this *VodController) CreateStsAuth() {
  217 + var msg *domain.ResponseMessage
  218 + defer func() {
  219 + this.Resp(msg)
  220 + }()
  221 + var request []string
  222 + if err := json.Unmarshal(this.ByteBody, &request); err != nil {
  223 + log.Error(err)
  224 + msg = domain.BadRequestParam(1)
  225 + return
  226 + }
  227 + header := controllers.GetRequestHeader(this.Ctx)
  228 + msg = domain.NewReturnResponse(oss.CreateStsAuth(header, request))
  229 +}
@@ -86,5 +86,18 @@ func init() { @@ -86,5 +86,18 @@ func init() {
86 AllowHTTPMethods: []string{"post"}, 86 AllowHTTPMethods: []string{"post"},
87 MethodParams: param.Make(), 87 MethodParams: param.Make(),
88 Params: nil}) 88 Params: nil})
89 - 89 + beego.GlobalControllerRouter["openapi/pkg/port/beego/controllers/v1:VodController"] = append(beego.GlobalControllerRouter["openapi/pkg/port/beego/controllers/v1:VodController"],
  90 + beego.ControllerComments{
  91 + Method: "PutObject",
  92 + Router: `/putObject`,
  93 + AllowHTTPMethods: []string{"post"},
  94 + MethodParams: param.Make(),
  95 + Params: nil})
  96 + beego.GlobalControllerRouter["openapi/pkg/port/beego/controllers/v1:VodController"] = append(beego.GlobalControllerRouter["openapi/pkg/port/beego/controllers/v1:VodController"],
  97 + beego.ControllerComments{
  98 + Method: "CreateStsAuth",
  99 + Router: `/createStsAuth`,
  100 + AllowHTTPMethods: []string{"post"},
  101 + MethodParams: param.Make(),
  102 + Params: nil})
90 } 103 }