正在显示
6 个修改的文件
包含
81 行增加
和
25 行删除
| @@ -12,6 +12,9 @@ cname ="https://media.fjmaimaimai.com/" | @@ -12,6 +12,9 @@ cname ="https://media.fjmaimaimai.com/" | ||
| 12 | OssEndPoint ="oss-cn-shenzhen.aliyuncs.com" | 12 | OssEndPoint ="oss-cn-shenzhen.aliyuncs.com" |
| 13 | BuckName ="timeless-world" | 13 | BuckName ="timeless-world" |
| 14 | 14 | ||
| 15 | +OssEndPointHZ ="oss-cn-hangzhou.aliyuncs.com" | ||
| 16 | +BuckNameHZ ="byte-bank" | ||
| 17 | + | ||
| 15 | #数据库相关 | 18 | #数据库相关 |
| 16 | MYSQL_USER = "${MYSQL_USER||root1}" | 19 | MYSQL_USER = "${MYSQL_USER||root1}" |
| 17 | MYSQL_PASSWORD = "${MYSQL_PASSWORD||sutianxia2018}" | 20 | MYSQL_PASSWORD = "${MYSQL_PASSWORD||sutianxia2018}" |
| @@ -21,7 +21,7 @@ type BuckObject struct { | @@ -21,7 +21,7 @@ type BuckObject struct { | ||
| 21 | FileName string `json:"fileName"` | 21 | FileName string `json:"fileName"` |
| 22 | } | 22 | } |
| 23 | 23 | ||
| 24 | -//创建视频上传凭证 | 24 | +// 创建视频上传凭证 |
| 25 | func CreateOssUpload(header *protocol.RequestHeader, MultipartForm *multipart.Form) (rsp interface{}, err error) { | 25 | func CreateOssUpload(header *protocol.RequestHeader, MultipartForm *multipart.Form) (rsp interface{}, err error) { |
| 26 | var objects []aliyun.Object | 26 | var objects []aliyun.Object |
| 27 | var listPath []BuckObject | 27 | var listPath []BuckObject |
| @@ -67,9 +67,15 @@ func CreateOssUpload(header *protocol.RequestHeader, MultipartForm *multipart.Fo | @@ -67,9 +67,15 @@ func CreateOssUpload(header *protocol.RequestHeader, MultipartForm *multipart.Fo | ||
| 67 | return | 67 | return |
| 68 | } | 68 | } |
| 69 | 69 | ||
| 70 | -//创建视频上传凭证 | ||
| 71 | -func CreateStsAuth(header *protocol.RequestHeader, files []string) (rsp interface{}, err error) { | ||
| 72 | - access, e := aliyun.DefaultSts() | 70 | +// 创建视频上传凭证 |
| 71 | +func CreateStsAuth(header *protocol.RequestHeader, files []string, regionId string) (rsp interface{}, err error) { | ||
| 72 | + var buckName = constant.BuckName | ||
| 73 | + var ossEndPoint = constant.OssEndPoint | ||
| 74 | + if regionId == aliyun.OssReginHZ { | ||
| 75 | + buckName = constant.BuckNameHZ | ||
| 76 | + ossEndPoint = constant.OssEndPointHZ | ||
| 77 | + } | ||
| 78 | + access, e := aliyun.Sts(regionId, constant.AccessKeyID, constant.AccessKeySecret, buckName, ossEndPoint) | ||
| 73 | if e != nil { | 79 | if e != nil { |
| 74 | err = e | 80 | err = e |
| 75 | log.Error(e.Error()) | 81 | log.Error(e.Error()) |
| @@ -79,8 +85,8 @@ func CreateStsAuth(header *protocol.RequestHeader, files []string) (rsp interfac | @@ -79,8 +85,8 @@ func CreateStsAuth(header *protocol.RequestHeader, files []string) (rsp interfac | ||
| 79 | fileBase := filepath.Base(v) | 85 | fileBase := filepath.Base(v) |
| 80 | key := utils.GetFileName(header.AppProject, aliyun.FileObject, fileBase) | 86 | key := utils.GetFileName(header.AppProject, aliyun.FileObject, fileBase) |
| 81 | listPath = append(listPath, BuckObject{ | 87 | 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), | 88 | + DefaultHost: fmt.Sprintf("https://%v.%v", buckName, ossEndPoint), |
| 89 | + Path: fmt.Sprintf("https://%v.%v/%v", buckName, ossEndPoint, key), | ||
| 84 | Key: key, | 90 | Key: key, |
| 85 | FileName: fileBase, | 91 | FileName: fileBase, |
| 86 | }) | 92 | }) |
| @@ -5,6 +5,9 @@ var ( | @@ -5,6 +5,9 @@ var ( | ||
| 5 | BuckName = "mmm-vod-dev-public" | 5 | BuckName = "mmm-vod-dev-public" |
| 6 | //OssAccessKeyID string = "LTAI4GGkZMdDoxRpehefbQhV" | 6 | //OssAccessKeyID string = "LTAI4GGkZMdDoxRpehefbQhV" |
| 7 | //OssAccessKeySecret string = "tytXo0RwxhGa3luyfmBB6PKe8M6Uu4" | 7 | //OssAccessKeySecret string = "tytXo0RwxhGa3luyfmBB6PKe8M6Uu4" |
| 8 | + | ||
| 9 | + OssEndPointHZ = "oss-cn-hangzhou.aliyuncs.com" | ||
| 10 | + BuckNameHZ = "byte-bank" | ||
| 8 | ) | 11 | ) |
| 9 | 12 | ||
| 10 | func init() { | 13 | func init() { |
| @@ -19,7 +19,7 @@ import ( | @@ -19,7 +19,7 @@ import ( | ||
| 19 | */ | 19 | */ |
| 20 | 20 | ||
| 21 | /* | 21 | /* |
| 22 | - 1.前端sts上传需要设置bucket跨域,bucket->权限管理->跨域设置 | 22 | +1.前端sts上传需要设置bucket跨域,bucket->权限管理->跨域设置 |
| 23 | */ | 23 | */ |
| 24 | func DefaultSts() (interface{}, error) { | 24 | func DefaultSts() (interface{}, error) { |
| 25 | //构建一个阿里云客户端, 用于发起请求。 | 25 | //构建一个阿里云客户端, 用于发起请求。 |
| @@ -51,3 +51,39 @@ func DefaultSts() (interface{}, error) { | @@ -51,3 +51,39 @@ func DefaultSts() (interface{}, error) { | ||
| 51 | "endpoint": constant.OssEndPoint, | 51 | "endpoint": constant.OssEndPoint, |
| 52 | }, nil | 52 | }, nil |
| 53 | } | 53 | } |
| 54 | + | ||
| 55 | +func Sts(regionId, accessKeyID, accessKeySecret, buckName, ossEndPoint string) (interface{}, error) { | ||
| 56 | + //构建一个阿里云客户端, 用于发起请求。 | ||
| 57 | + //构建阿里云客户端时,需要设置AccessKey ID和AccessKey Secret。 | ||
| 58 | + client, err := sts.NewClientWithAccessKey(regionId, accessKeyID, accessKeySecret) | ||
| 59 | + if err != nil { | ||
| 60 | + return nil, err | ||
| 61 | + } | ||
| 62 | + | ||
| 63 | + //构建请求对象。 | ||
| 64 | + request := sts.CreateAssumeRoleRequest() | ||
| 65 | + request.Scheme = "https" | ||
| 66 | + request.RoleArn = constant.RoleArn | ||
| 67 | + // 会话名称 | ||
| 68 | + request.RoleSessionName = "role-oss-sts-session" | ||
| 69 | + | ||
| 70 | + //发起请求,并得到响应。 | ||
| 71 | + response, e := client.AssumeRole(request) | ||
| 72 | + if e != nil { | ||
| 73 | + err = e | ||
| 74 | + log.Error(e.Error()) | ||
| 75 | + } | ||
| 76 | + return map[string]interface{}{ | ||
| 77 | + "accessKeySecret": response.Credentials.AccessKeySecret, | ||
| 78 | + "expiration": response.Credentials.Expiration, | ||
| 79 | + "accessKeyId": response.Credentials.AccessKeyId, | ||
| 80 | + "securityToken": response.Credentials.SecurityToken, | ||
| 81 | + "bucket": buckName, | ||
| 82 | + "endpoint": ossEndPoint, | ||
| 83 | + }, nil | ||
| 84 | +} | ||
| 85 | + | ||
| 86 | +var ( | ||
| 87 | + OssReginSZ = "cn-shenzhen" | ||
| 88 | + OssReginHZ = "cn-hangzhou" | ||
| 89 | +) |
| @@ -14,7 +14,7 @@ type VodController struct { | @@ -14,7 +14,7 @@ type VodController struct { | ||
| 14 | controllers.BaseController | 14 | controllers.BaseController |
| 15 | } | 15 | } |
| 16 | 16 | ||
| 17 | -//创建视频上传凭证 CreateUploadVideo | 17 | +// 创建视频上传凭证 CreateUploadVideo |
| 18 | // @router /createUploadVideo [post] | 18 | // @router /createUploadVideo [post] |
| 19 | func (this *VodController) CreateUploadVideo() { | 19 | func (this *VodController) CreateUploadVideo() { |
| 20 | var msg *domain.ResponseMessage | 20 | var msg *domain.ResponseMessage |
| @@ -38,7 +38,7 @@ func (this *VodController) CreateUploadVideo() { | @@ -38,7 +38,7 @@ func (this *VodController) CreateUploadVideo() { | ||
| 38 | msg = domain.NewReturnResponse(vod.CreateUploadVideo(header, request)) | 38 | msg = domain.NewReturnResponse(vod.CreateUploadVideo(header, request)) |
| 39 | } | 39 | } |
| 40 | 40 | ||
| 41 | -//创建视频上传凭证(多个) CreateUploadVideos | 41 | +// 创建视频上传凭证(多个) CreateUploadVideos |
| 42 | // @router /createUploadVideos [post] | 42 | // @router /createUploadVideos [post] |
| 43 | func (this *VodController) CreateUploadVideos() { | 43 | func (this *VodController) CreateUploadVideos() { |
| 44 | var msg *domain.ResponseMessage | 44 | var msg *domain.ResponseMessage |
| @@ -75,7 +75,7 @@ func (this *VodController) CreateUploadVideos() { | @@ -75,7 +75,7 @@ func (this *VodController) CreateUploadVideos() { | ||
| 75 | msg = domain.NewReturnResponse(response, err) | 75 | msg = domain.NewReturnResponse(response, err) |
| 76 | } | 76 | } |
| 77 | 77 | ||
| 78 | -//刷新视频上传凭证 RefreshUploadVideo | 78 | +// 刷新视频上传凭证 RefreshUploadVideo |
| 79 | // @router /refreshUploadVideo [post] | 79 | // @router /refreshUploadVideo [post] |
| 80 | func (this *VodController) RefreshUploadVideo() { | 80 | func (this *VodController) RefreshUploadVideo() { |
| 81 | var msg *domain.ResponseMessage | 81 | var msg *domain.ResponseMessage |
| @@ -96,7 +96,7 @@ func (this *VodController) RefreshUploadVideo() { | @@ -96,7 +96,7 @@ func (this *VodController) RefreshUploadVideo() { | ||
| 96 | msg = domain.NewReturnResponse(vod.RefreshUploadVideo(header, request)) | 96 | msg = domain.NewReturnResponse(vod.RefreshUploadVideo(header, request)) |
| 97 | } | 97 | } |
| 98 | 98 | ||
| 99 | -//创建图片上传凭证 CreateUploadImage | 99 | +// 创建图片上传凭证 CreateUploadImage |
| 100 | // @router /createUploadImage [post] | 100 | // @router /createUploadImage [post] |
| 101 | func (this *VodController) CreateUploadImage() { | 101 | func (this *VodController) CreateUploadImage() { |
| 102 | var msg *domain.ResponseMessage | 102 | var msg *domain.ResponseMessage |
| @@ -118,7 +118,7 @@ func (this *VodController) CreateUploadImage() { | @@ -118,7 +118,7 @@ func (this *VodController) CreateUploadImage() { | ||
| 118 | msg = domain.NewReturnResponse(vod.CreateUploadImage(header, request)) | 118 | msg = domain.NewReturnResponse(vod.CreateUploadImage(header, request)) |
| 119 | } | 119 | } |
| 120 | 120 | ||
| 121 | -//创建图片上传凭证 CreateUploadImages | 121 | +// 创建图片上传凭证 CreateUploadImages |
| 122 | // @router /createUploadImages [post] | 122 | // @router /createUploadImages [post] |
| 123 | func (this *VodController) CreateUploadImages() { | 123 | func (this *VodController) CreateUploadImages() { |
| 124 | var msg *domain.ResponseMessage | 124 | var msg *domain.ResponseMessage |
| @@ -157,7 +157,7 @@ func (this *VodController) CreateUploadImages() { | @@ -157,7 +157,7 @@ func (this *VodController) CreateUploadImages() { | ||
| 157 | msg = domain.NewReturnResponse(response, err) | 157 | msg = domain.NewReturnResponse(response, err) |
| 158 | } | 158 | } |
| 159 | 159 | ||
| 160 | -//获取视频播放地址 GetPlayInfo | 160 | +// 获取视频播放地址 GetPlayInfo |
| 161 | // @router /getPlayInfo [post] | 161 | // @router /getPlayInfo [post] |
| 162 | func (this *VodController) GetPlayInfo() { | 162 | func (this *VodController) GetPlayInfo() { |
| 163 | var msg *domain.ResponseMessage | 163 | var msg *domain.ResponseMessage |
| @@ -178,7 +178,7 @@ func (this *VodController) GetPlayInfo() { | @@ -178,7 +178,7 @@ func (this *VodController) GetPlayInfo() { | ||
| 178 | msg = domain.NewReturnResponse(vod.GetPlayInfo(header, request)) | 178 | msg = domain.NewReturnResponse(vod.GetPlayInfo(header, request)) |
| 179 | } | 179 | } |
| 180 | 180 | ||
| 181 | -//获取视频播放凭证 GetVideoPlayAuth | 181 | +// 获取视频播放凭证 GetVideoPlayAuth |
| 182 | // @router /getVideoPlayAuth [post] | 182 | // @router /getVideoPlayAuth [post] |
| 183 | func (this *VodController) GetVideoPlayAuth() { | 183 | func (this *VodController) GetVideoPlayAuth() { |
| 184 | var msg *domain.ResponseMessage | 184 | var msg *domain.ResponseMessage |
| @@ -199,7 +199,7 @@ func (this *VodController) GetVideoPlayAuth() { | @@ -199,7 +199,7 @@ func (this *VodController) GetVideoPlayAuth() { | ||
| 199 | msg = domain.NewReturnResponse(vod.GetVideoPlayAuth(header, request)) | 199 | msg = domain.NewReturnResponse(vod.GetVideoPlayAuth(header, request)) |
| 200 | } | 200 | } |
| 201 | 201 | ||
| 202 | -//归档对象 PutObject | 202 | +// 归档对象 PutObject |
| 203 | // @router /putObject [post] | 203 | // @router /putObject [post] |
| 204 | func (this *VodController) PutObject() { | 204 | func (this *VodController) PutObject() { |
| 205 | var msg *domain.ResponseMessage | 205 | var msg *domain.ResponseMessage |
| @@ -225,5 +225,5 @@ func (this *VodController) CreateStsAuth() { | @@ -225,5 +225,5 @@ func (this *VodController) CreateStsAuth() { | ||
| 225 | return | 225 | return |
| 226 | } | 226 | } |
| 227 | header := controllers.GetRequestHeader(this.Ctx) | 227 | header := controllers.GetRequestHeader(this.Ctx) |
| 228 | - msg = domain.NewReturnResponse(oss.CreateStsAuth(header, request)) | 228 | + msg = domain.NewReturnResponse(oss.CreateStsAuth(header, request, aliyun.OssReginSZ)) |
| 229 | } | 229 | } |
| @@ -14,7 +14,7 @@ type VodController struct { | @@ -14,7 +14,7 @@ type VodController struct { | ||
| 14 | controllers.BaseController | 14 | controllers.BaseController |
| 15 | } | 15 | } |
| 16 | 16 | ||
| 17 | -//创建视频上传凭证 CreateUploadVideo | 17 | +// 创建视频上传凭证 CreateUploadVideo |
| 18 | // @router /createUploadVideo [post] | 18 | // @router /createUploadVideo [post] |
| 19 | func (this *VodController) CreateUploadVideo() { | 19 | func (this *VodController) CreateUploadVideo() { |
| 20 | var msg *domain.ResponseMessage | 20 | var msg *domain.ResponseMessage |
| @@ -38,7 +38,7 @@ func (this *VodController) CreateUploadVideo() { | @@ -38,7 +38,7 @@ func (this *VodController) CreateUploadVideo() { | ||
| 38 | msg = domain.NewReturnResponse(vod.CreateUploadVideo(header, request)) | 38 | msg = domain.NewReturnResponse(vod.CreateUploadVideo(header, request)) |
| 39 | } | 39 | } |
| 40 | 40 | ||
| 41 | -//创建视频上传凭证(多个) CreateUploadVideos | 41 | +// 创建视频上传凭证(多个) CreateUploadVideos |
| 42 | // @router /createUploadVideos [post] | 42 | // @router /createUploadVideos [post] |
| 43 | func (this *VodController) CreateUploadVideos() { | 43 | func (this *VodController) CreateUploadVideos() { |
| 44 | var msg *domain.ResponseMessage | 44 | var msg *domain.ResponseMessage |
| @@ -75,7 +75,7 @@ func (this *VodController) CreateUploadVideos() { | @@ -75,7 +75,7 @@ func (this *VodController) CreateUploadVideos() { | ||
| 75 | msg = domain.NewReturnResponse(response, err) | 75 | msg = domain.NewReturnResponse(response, err) |
| 76 | } | 76 | } |
| 77 | 77 | ||
| 78 | -//刷新视频上传凭证 RefreshUploadVideo | 78 | +// 刷新视频上传凭证 RefreshUploadVideo |
| 79 | // @router /refreshUploadVideo [post] | 79 | // @router /refreshUploadVideo [post] |
| 80 | func (this *VodController) RefreshUploadVideo() { | 80 | func (this *VodController) RefreshUploadVideo() { |
| 81 | var msg *domain.ResponseMessage | 81 | var msg *domain.ResponseMessage |
| @@ -96,7 +96,7 @@ func (this *VodController) RefreshUploadVideo() { | @@ -96,7 +96,7 @@ func (this *VodController) RefreshUploadVideo() { | ||
| 96 | msg = domain.NewReturnResponse(vod.RefreshUploadVideo(header, request)) | 96 | msg = domain.NewReturnResponse(vod.RefreshUploadVideo(header, request)) |
| 97 | } | 97 | } |
| 98 | 98 | ||
| 99 | -//创建图片上传凭证 CreateUploadImage | 99 | +// 创建图片上传凭证 CreateUploadImage |
| 100 | // @router /createUploadImage [post] | 100 | // @router /createUploadImage [post] |
| 101 | func (this *VodController) CreateUploadImage() { | 101 | func (this *VodController) CreateUploadImage() { |
| 102 | var msg *domain.ResponseMessage | 102 | var msg *domain.ResponseMessage |
| @@ -118,7 +118,7 @@ func (this *VodController) CreateUploadImage() { | @@ -118,7 +118,7 @@ func (this *VodController) CreateUploadImage() { | ||
| 118 | msg = domain.NewReturnResponse(vod.CreateUploadImage(header, request)) | 118 | msg = domain.NewReturnResponse(vod.CreateUploadImage(header, request)) |
| 119 | } | 119 | } |
| 120 | 120 | ||
| 121 | -//创建图片上传凭证 CreateUploadImages | 121 | +// 创建图片上传凭证 CreateUploadImages |
| 122 | // @router /createUploadImages [post] | 122 | // @router /createUploadImages [post] |
| 123 | func (this *VodController) CreateUploadImages() { | 123 | func (this *VodController) CreateUploadImages() { |
| 124 | var msg *domain.ResponseMessage | 124 | var msg *domain.ResponseMessage |
| @@ -157,7 +157,7 @@ func (this *VodController) CreateUploadImages() { | @@ -157,7 +157,7 @@ func (this *VodController) CreateUploadImages() { | ||
| 157 | msg = domain.NewReturnResponse(response, err) | 157 | msg = domain.NewReturnResponse(response, err) |
| 158 | } | 158 | } |
| 159 | 159 | ||
| 160 | -//获取视频播放地址 GetPlayInfo | 160 | +// 获取视频播放地址 GetPlayInfo |
| 161 | // @router /getPlayInfo [post] | 161 | // @router /getPlayInfo [post] |
| 162 | func (this *VodController) GetPlayInfo() { | 162 | func (this *VodController) GetPlayInfo() { |
| 163 | var msg *domain.ResponseMessage | 163 | var msg *domain.ResponseMessage |
| @@ -178,7 +178,7 @@ func (this *VodController) GetPlayInfo() { | @@ -178,7 +178,7 @@ func (this *VodController) GetPlayInfo() { | ||
| 178 | msg = domain.NewReturnResponse(vod.GetPlayInfo(header, request)) | 178 | msg = domain.NewReturnResponse(vod.GetPlayInfo(header, request)) |
| 179 | } | 179 | } |
| 180 | 180 | ||
| 181 | -//获取视频播放凭证 GetVideoPlayAuth | 181 | +// 获取视频播放凭证 GetVideoPlayAuth |
| 182 | // @router /getVideoPlayAuth [post] | 182 | // @router /getVideoPlayAuth [post] |
| 183 | func (this *VodController) GetVideoPlayAuth() { | 183 | func (this *VodController) GetVideoPlayAuth() { |
| 184 | var msg *domain.ResponseMessage | 184 | var msg *domain.ResponseMessage |
| @@ -199,7 +199,7 @@ func (this *VodController) GetVideoPlayAuth() { | @@ -199,7 +199,7 @@ func (this *VodController) GetVideoPlayAuth() { | ||
| 199 | msg = domain.NewReturnResponse(vod.GetVideoPlayAuth(header, request)) | 199 | msg = domain.NewReturnResponse(vod.GetVideoPlayAuth(header, request)) |
| 200 | } | 200 | } |
| 201 | 201 | ||
| 202 | -//归档对象 PutObject | 202 | +// 归档对象 PutObject |
| 203 | // @router /putObject [post] | 203 | // @router /putObject [post] |
| 204 | func (this *VodController) PutObject() { | 204 | func (this *VodController) PutObject() { |
| 205 | var msg *domain.ResponseMessage | 205 | var msg *domain.ResponseMessage |
| @@ -220,12 +220,20 @@ func (this *VodController) CreateStsAuth() { | @@ -220,12 +220,20 @@ func (this *VodController) CreateStsAuth() { | ||
| 220 | }() | 220 | }() |
| 221 | var request = struct { | 221 | var request = struct { |
| 222 | FileNames []string `json:"files"` | 222 | FileNames []string `json:"files"` |
| 223 | + RegionId string `json:"regionId"` // cn-shenzhen cn-hangzhou 默认深圳:cn-shenzhen | ||
| 224 | + ProjectId string `json:"projectId"` // 项目标识 byte-bank | ||
| 223 | }{} | 225 | }{} |
| 226 | + if len(request.RegionId) == 0 { | ||
| 227 | + request.RegionId = aliyun.OssReginSZ | ||
| 228 | + } | ||
| 224 | if err := json.Unmarshal(this.ByteBody, &request); err != nil { | 229 | if err := json.Unmarshal(this.ByteBody, &request); err != nil { |
| 225 | log.Error(err) | 230 | log.Error(err) |
| 226 | msg = domain.BadRequestParam(1) | 231 | msg = domain.BadRequestParam(1) |
| 227 | return | 232 | return |
| 228 | } | 233 | } |
| 229 | header := controllers.GetRequestHeader(this.Ctx) | 234 | header := controllers.GetRequestHeader(this.Ctx) |
| 230 | - msg = domain.NewReturnResponse(oss.CreateStsAuth(header, request.FileNames)) | 235 | + if len(header.AppProject) == 0 && request.ProjectId != "" { |
| 236 | + header.AppProject = request.ProjectId | ||
| 237 | + } | ||
| 238 | + msg = domain.NewReturnResponse(oss.CreateStsAuth(header, request.FileNames, request.RegionId)) | ||
| 231 | } | 239 | } |
-
请 注册 或 登录 后发表评论