阿里云视频点播(视频上传、图片上传凭证)
删除图片
import json import traceback from aliyunsdkcore.client import AcsClient from aliyunsdkvod.request.v20170321 import DeleteImageRequest def init_vod_client(accessKeyId, accessKeySecret): regionId = 'cn-shanghai' # 点播服务接入区域 connectTimeout = 3 # 连接超时,单位为秒 return AcsClient(accessKeyId, accessKeySecret, regionId, auto_retry=True, max_retry_time=3, timeout=connectTimeout) from aliyunsdkvod.request.v20170321 import DeleteImageRequest def delete_image(clt): request = DeleteImageRequest.DeleteImageRequest() #根据ImageURL删除图片文件 request.set_DeleteImageType('http://video.dayingjiaol.com/image/default/9C380571FB79484F9E1E27DFA7D91FD6-6-2.jpg') request.set_ImageURLs('http://sample.aliyun.com/cover.jpg') #根据ImageId删除图片文件 #request.set_DeleteImageType('ImageId') #request.set_ImageIds('ImageId1,ImageId2') #根据VideoId删除指定ImageType的图片文件 #request.set_DeleteImageType('VideoId') #request.set_VideoId('VideoId') #request.set_ImageType('SpriteSnapshot') request.set_accept_format('JSON') response = json.loads(clt.do_action_with_exception(request)) return response try: clt = init_vod_client('accessKeyId', 'accessKeySecret') delInfo = delete_image(clt) print(json.dumps(delInfo, ensure_ascii=False, indent=4)) except Exception as e: print(e) print(traceback.format_exc())
删除视频
import json import traceback from aliyunsdkcore.client import AcsClient accessKeyId = 'LTAIT0qo' accessSecret = '8LOt6GqWd' from aliyunsdkvod.request.v20170321 import DeleteImageRequest def init_vod_client(accessKeyId, accessKeySecret): regionId = 'cn-shanghai' # 点播服务接入区域 connectTimeout = 3 # 连接超时,单位为秒 return AcsClient(accessKeyId, accessKeySecret, regionId, auto_retry=True, max_retry_time=3, timeout=connectTimeout) from aliyunsdkvod.request.v20170321 import DeleteVideoRequest def delete_videos(clt): request = DeleteVideoRequest.DeleteVideoRequest() videoIds = ['videoId1', 'videoId2'] request.set_VideoIds(','.join(videoIds)) # 支持批量删除视频,多个用逗号分隔 request.set_accept_format('JSON') response = json.loads(clt.do_action_with_exception(request)) return response try: clt = init_vod_client(accessKeyId, accessSecret) delInfo = delete_videos(clt) print(json.dumps(delInfo, ensure_ascii=False, indent=4)) except Exception as e: print(e) print(traceback.format_exc())
刷新视频上传凭证
import json from aliyunsdkvod.request.v20170321 import RefreshUploadVideoRequest import json import traceback from aliyunsdkcore.client import AcsClient def init_vod_client(accessKeyId, accessKeySecret): regionId = 'cn-shanghai' # 点播服务接入区域 connectTimeout = 3 # 连接超时,单位为秒 return AcsClient(accessKeyId, accessKeySecret, regionId, auto_retry=True, max_retry_time=3, timeout=connectTimeout) def refresh_upload_video(clt, videoId): request = RefreshUploadVideoRequest.RefreshUploadVideoRequest() request.set_VideoId(videoId) request.set_accept_format('JSON') return json.loads(clt.do_action_with_exception(request)) try: clt = init_vod_client('<AccessKeyId>', '<AccessKeySecret>') uploadInfo = refresh_upload_video(clt, 'VideoId') print(json.dumps(uploadInfo, ensure_ascii=False, indent=4)) except Exception as e: print(e) print(traceback.format_exc())
获取图片上传地址和凭证
# -*- coding: UTF-8 -*- from aliyunsdkvod.request.v20170321 import CreateUploadImageRequest import json import traceback from aliyunsdkcore.client import AcsClient accessKeyId = 'LTAIT0' accessSecret = '8LOt6GqW' def init_vod_client(accessKeyId, accessKeySecret): regionId = 'cn-shanghai' # 点播服务接入区域 connectTimeout = 3 # 连接超时,单位为秒 return AcsClient(accessKeyId, accessKeySecret, regionId, auto_retry=True, max_retry_time=3, timeout=connectTimeout) def create_upload_image(clt, imageType, imageExt): request = CreateUploadImageRequest.CreateUploadImageRequest() request.set_ImageType(imageType) request.set_ImageExt(imageExt) request.set_accept_format('JSON') return json.loads(clt.do_action_with_exception(request)) try: clt = init_vod_client(accessKeyId, accessSecret) imageInfo = create_upload_image(clt, 'cover', 'jpg') print(json.dumps(imageInfo, ensure_ascii=False, indent=4)) except Exception as e: print(e) print(traceback.format_exc())
获取视频上传地址和凭证
import json from aliyunsdkvod.request.v20170321 import CreateUploadVideoRequest import json import traceback from aliyunsdkcore.client import AcsClient def init_vod_client(accessKeyId, accessKeySecret): regionId = 'cn-shanghai' # 点播服务接入区域 connectTimeout = 3 # 连接超时,单位为秒 return AcsClient(accessKeyId, accessKeySecret, regionId, auto_retry=True, max_retry_time=3, timeout=connectTimeout) def create_upload_video(clt): request = CreateUploadVideoRequest.CreateUploadVideoRequest() request.set_Title('Video Title') request.set_FileName('/opt/video/sample/video_file.mp4') request.set_Description('Video Description') request.set_CoverURL('http://img.alicdn.com/tps/TB1qnJ1PVXXXXXCXXXXXXXXXXXX-700-700.png') request.set_Tags('tag1,tag2') request.set_CateId(0) request.set_accept_format('JSON') response = json.loads(clt.do_action_with_exception(request)) return response try: clt = init_vod_client('<AccessKeyId>', '<AccessKeySecret>') uploadInfo = create_upload_video(clt) print(uploadInfo['UploadAuth']) print(json.dumps(uploadInfo, ensure_ascii=False, indent=4)) except Exception as e: print(e) print(traceback.format_exc())
视频点播初始化
# -*- coding: UTF-8 -*- import json import traceback from aliyunsdkcore.client import AcsClient from aliyunsdkvod.request.v20170321 import GetPlayInfoRequest # 初始化播放软件 accessKeyId = 'LTAIT0' accessSecret = '8LOt6GqWdYgl10L' videoId = 'e44d0b3625d445c599' dict = {} def init_vod_client(accessKeyId, accessKeySecret): ''' 初始化客户端 :param accessKeyId: :param accessKeySecret: :return: ''' regionId = 'cn-shanghai' # 点播服务接入区域 connectTimeout = 3 # 连接超时,单位为秒 ac = AcsClient(accessKeyId, accessKeySecret, regionId, auto_retry=True, max_retry_time=3, timeout=connectTimeout) # print(ac) return ac def get_play_info(clt, videoId): ''' 获取播放地址 :param clt: :param videoId: :return: ''' request = GetPlayInfoRequest.GetPlayInfoRequest() request.set_accept_format('JSON') request.set_VideoId(videoId) request.set_AuthTimeout(3600 * 5) response = json.loads(clt.do_action_with_exception(request)) return response try: clt = init_vod_client(accessKeyId, accessSecret) playInfo = get_play_info(clt, videoId) r1 = playInfo # print(r1) dict['playInfo'] = r1 except Exception as e: print(e) print(traceback.format_exc()) from aliyunsdkvod.request.v20170321 import GetVideoPlayAuthRequest def get_video_playauth(clt, videoId): request = GetVideoPlayAuthRequest.GetVideoPlayAuthRequest() request.set_accept_format('JSON') request.set_VideoId(videoId) request.set_AuthInfoTimeout(3600) response = json.loads(clt.do_action_with_exception(request)) return response try: clt = init_vod_client(accessKeyId, accessSecret) playAuth = get_video_playauth(clt, videoId) r2 = playAuth # print(type(r2)) dict['playAuth'] = r2 dict= json.dumps(dict, ensure_ascii=False, indent=4) print(dict) except Exception as e: print(e) print(traceback.format_exc())
阿里云STS认证
#!/usr/bin/env python #coding=utf-8 from aliyunsdkcore import client from aliyunsdkcore.profile import region_provider from aliyunsdksts.request.v20150401 import AssumeRoleRequest # 构建一个阿里云 Client, 用于发起请求 # 构建阿里云 Client 时需要设置 access key ID 和 access key secret REGIONID = 'cn-shanghai' ENDPOINT = 'sts.cn-shanghai.aliyuncs.com' # 配置要访问的 STS endpoint region_provider.add_endpoint('Sts', REGIONID, ENDPOINT) # 初始化 Client clt = client.AcsClient('LTAIkGqU','nroZiqsuQK6g', REGIONID) # 构建 AssumeRole 请求 request = AssumeRoleRequest.AssumeRoleRequest() # 指定角色 ARN request.set_RoleArn('acs:ram::1868391547387230:role/frindssts') # 设置会话名称,审计服务使用此名称区分调用者 request.set_RoleSessionName('img-up') # 发起请求,并得到 response response = clt.do_action_with_exception(request) print (response)