diff --git a/.gitignore b/.gitignore index 0d20b648..7e99e367 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1 @@ -*.pyc +*.pyc \ No newline at end of file diff --git a/demo/ci_media.py b/demo/ci_media.py new file mode 100644 index 00000000..2b7a627f --- /dev/null +++ b/demo/ci_media.py @@ -0,0 +1,151 @@ +# -*- coding=utf-8 +from qcloud_cos import CosConfig +from qcloud_cos import CosS3Client +from qcloud_cos.cos_comm import CiDetectType + +import sys +import logging +import os +import time + +# 腾讯云COSV5Python SDK, 目前可以支持Python2.6与Python2.7以及Python3.x + +# https://cloud.tencent.com/document/product/436/48987 + +logging.basicConfig(level=logging.INFO, stream=sys.stdout) + +# 设置用户属性, 包括secret_id, secret_key, region +# appid已在配置中移除,请在参数Bucket中带上appid。Bucket由bucketname-appid组成 +# 这里秘钥是从环境变量取得,如自己测试可改成自己对应的秘钥 +secret_id = os.environ["SECRETID"] # 替换为用户的 SecretId,请登录访问管理控制台进行查看和管理,https://console.cloud.tencent.com/cam/capi +secret_key = os.environ["SECRETKEY"] # 替换为用户的 SecretKey,请登录访问管理控制台进行查看和管理,https://console.cloud.tencent.com/cam/capi +region = 'ap-chongqing' # 替换为用户的 region,已创建桶归属的region可以在控制台查看,https://console.cloud.tencent.com/cos5/bucket +# COS支持的所有region列表参见https://www.qcloud.com/document/product/436/6224 +token = None # 如果使用永久密钥不需要填入token,如果使用临时密钥需要填入,临时密钥生成和使用指引参见https://cloud.tencent.com/document/product/436/14048 + +config = CosConfig(Region=region, SecretId=secret_id, SecretKey=secret_key, Token=token, Scheme='https') # 获取配置对象 +client = CosS3Client(config) + + +bucket_name = 'demo-1253960454' + + +def ci_get_media_queue(): + # 查询媒体队列信息 + response = client.ci_get_media_queue( + Bucket=bucket_name + ) + print(response) + return response + +def ci_create_media_transcode_watermark_jobs(): + # 创建转码任务 + body = { + 'Input':{ + 'Object':'117374C.mp4' + }, + 'QueueId': 'pe943803693bd42d1a3105804ddaee525', + 'Tag': 'Transcode', + 'Operation': { + 'Output':{'Bucket':bucket_name, 'Region':region, 'Object':'117374C_output.mp4'}, + 'TemplateId': 't02db40900dc1c43ad9bdbd8acec6075c5', + # "WatermarkTemplateId": ["", ""], + 'Watermark': [ + { + 'Type':'Text', + 'Pos':'TopRight', + 'LocMode':'Absolute', + 'Dx':'64', + 'Dy': '64', + 'StartTime':'0', + 'EndTime':'1000.5', + 'Text': { + 'Text': '水印内容', + 'FontSize': '90', + 'FontType': 'simfang.ttf', + 'FontColor': '0xFFEEFF', + 'Transparency': '100', + }, + }, + { + 'Type':'Image', + 'Pos':'TopLeft', + 'LocMode':'Absolute', + 'Dx':'100', + 'Dy': '100', + 'StartTime':'0', + 'EndTime':'1000.5', + 'Image': { + 'Url': 'http://'+bucket_name+".cos."+region+".myqcloud.com/1215shuiyin.jpg", + 'Mode': 'Fixed', + 'Width': '128', + 'Height': '128', + 'Transparency': '100', + }, + } + ] + } + } + # dict中数组类型的标签,都需要特殊处理 + lst = [ + '', + '', + '', + '' + ] + response = client.ci_create_media_jobs( + Bucket=bucket_name, + Jobs=body, + Lst=lst, + ContentType='application/xml' + ) + print(response) + return response + +def ci_create_media_transcode_jobs(): + # 创建转码任务 + body = { + 'Input':{ + 'Object':'117374C.mp4' + }, + 'QueueId': 'pe943803693bd42d1a3105804ddaee525', + 'Tag': 'Transcode', + 'Operation': { + 'Output':{'Bucket':bucket_name, 'Region':region, 'Object':'117374C_output.mp4'}, + 'TemplateId': 't02db40900dc1c43ad9bdbd8acec6075c5' + } + } + response = client.ci_create_media_jobs( + Bucket=bucket_name, + Jobs=body, + Lst={}, + ContentType='application/xml' + ) + print(response) + return response + +def ci_list_media_transcode_jobs(): + # 转码任务 + response = client.ci_list_media_jobs( + Bucket=bucket_name, + QueueId='pe943803693bd42d1a3105804ddaee525', + Tag='Transcode', + ContentType='application/xml' + ) + print(response) + return response + +def ci_get_media_transcode_jobs(): + # 转码任务 + response = client.ci_get_media_jobs( + Bucket=bucket_name, + JobIDs='j3feb7ccc28fc11eca50b6f68c211dc6c,jb83bcc5a28fb11ecae48a1f29371c5f8', + ContentType='application/xml' + ) + print(response) + return response + +if __name__ == "__main__": + #ci_get_media_queue() + ci_get_media_transcode_jobs() + #ci_create_media_transcode_jobs() \ No newline at end of file diff --git a/qcloud_cos/cos_client.py b/qcloud_cos/cos_client.py index 8174bfc4..28a8f97d 100644 --- a/qcloud_cos/cos_client.py +++ b/qcloud_cos/cos_client.py @@ -4816,6 +4816,232 @@ def ci_auditing_document_query(self, Bucket, JobID, **kwargs): **kwargs ) + def ci_get_media_queue(self, Bucket, **kwargs): + """查询媒体处理队列接口 https://cloud.tencent.com/document/product/436/54045 + + :param Bucket(string): 存储桶名称. + :param kwargs(dict): 设置请求的headers. + :return(dict): 查询成功返回的结果,dict类型. + + .. code-block:: python + + config = CosConfig(Region=region, SecretId=secret_id, SecretKey=secret_key, Token=token) # 获取配置对象 + client = CosS3Client(config) + # 查询媒体处理队列接口 + response = client.ci_get_media_queue( + Bucket='bucket' + ) + print response + """ + headers = mapped(kwargs) + final_headers = {} + params = {} + for key in headers: + if key.startswith("response"): + params[key] = headers[key] + else: + final_headers[key] = headers[key] + headers = final_headers + + params = format_values(params) + + path = "/queue" + url = self._conf.uri(bucket=Bucket, path=path, endpoint=self._conf._endpoint_ci) + logger.info("get_media_queue result, url=:{url} ,headers=:{headers}, params=:{params}".format( + url=url, + headers=headers, + params=params)) + rt = self.send_request( + method='GET', + url=url, + bucket=Bucket, + auth=CosS3Auth(self._conf, path, params=params), + params=params, + headers=headers) + + data = xml_to_dict(rt.content) + # 单个元素时将dict转为list + format_dict(data, ['QueueList']) + return data + + def ci_create_media_jobs(self, Bucket, Jobs={}, Lst={}, **kwargs): + """ 创建任务接口 https://cloud.tencent.com/document/product/436/54013 + + :param Bucket(string): 存储桶名称. + :param Jobs(dict): 创建任务的配置. + :param Lst(dict): 创建任务dict转xml时去除Key数组. + :param kwargs(dict): 设置请求的headers. + :return(dict): 查询成功返回的结果,dict类型. + + .. code-block:: python + + config = CosConfig(Region=region, SecretId=secret_id, SecretKey=secret_key, Token=token) # 获取配置对象 + client = CosS3Client(config) + # 创建任务接口 + response = client.ci_create_media_jobs( + Bucket='bucket' + Jobs={}, + Lst={} + ) + print response + """ + headers = mapped(kwargs) + final_headers = {} + params = {} + for key in headers: + if key.startswith("response"): + params[key] = headers[key] + else: + final_headers[key] = headers[key] + headers = final_headers + + params = format_values(params) + xml_config = format_xml(data=Jobs, root='Request', lst=Lst) + path = "/jobs" + url = self._conf.uri(bucket=Bucket, path=path, endpoint=self._conf._endpoint_ci) + logger.info("create_media_jobs result, url=:{url} ,headers=:{headers}, params=:{params}, xml_config=:{xml_config}".format( + url=url, + headers=headers, + params=params, + xml_config=xml_config)) + rt = self.send_request( + method='POST', + url=url, + bucket=Bucket, + data=xml_config, + auth=CosS3Auth(self._conf, path, params=params), + params=params, + headers=headers) + + data = xml_to_dict(rt.content) + # 单个元素时将dict转为list + format_dict(data, ['JobsDetail']) + return data + + def ci_get_media_jobs(self, Bucket, JobIDs, **kwargs): + """ 查询任务接口 https://cloud.tencent.com/document/product/436/54010 + + :param Bucket(string): 存储桶名称. + :param JobIDs(string): 任务ID,以,分割多个任务ID. + :param kwargs(dict): 设置请求的headers. + :return(dict): 查询成功返回的结果,dict类型. + + .. code-block:: python + + config = CosConfig(Region=region, SecretId=secret_id, SecretKey=secret_key, Token=token) # 获取配置对象 + client = CosS3Client(config) + # 创建任务接口 + response = client.ci_get_media_jobs( + Bucket='bucket' + JobIDs={} + ) + print response + """ + headers = mapped(kwargs) + final_headers = {} + params = {} + for key in headers: + if key.startswith("response"): + params[key] = headers[key] + else: + final_headers[key] = headers[key] + headers = final_headers + + params = format_values(params) + path = "/jobs/" + JobIDs + url = self._conf.uri(bucket=Bucket, path=path, endpoint=self._conf._endpoint_ci) + logger.info("get_media_jobs result, url=:{url} ,headers=:{headers}, params=:{params}".format( + url=url, + headers=headers, + params=params)) + rt = self.send_request( + method='GET', + url=url, + bucket=Bucket, + auth=CosS3Auth(self._conf, path, params=params), + params=params, + headers=headers) + logger.debug("ci_get_media_jobs result, url=:{url} ,content=:{content}".format( + url=url, + content=rt.content)) + + data = xml_to_dict(rt.content) + # 单个元素时将dict转为list + format_dict(data, ['JobsDetail']) + return data + + def ci_list_media_jobs(self, Bucket, QueueId, Tag, StartCreationTime=None, EndCreationTime=None, OrderByTime='Desc', States='All', Size=10, NextToken='', **kwargs): + """ 查询任务接口 https://cloud.tencent.com/document/product/436/54011 + + :param Bucket(string): 存储桶名称. + :param QueueId(string): 队列ID. + :param Tag(string): 任务类型. + :param StartCreationTime(string): 开始时间. + :param EndCreationTime(string): 结束时间. + :param OrderByTime(string): 排序方式. + :param States(string): 任务状态. + :param Size(string): 任务个数. + :param NextToken(string): 请求的上下文,用于翻页. + :param kwargs(dict): 设置请求的headers. + :return(dict): 查询成功返回的结果,dict类型. + + .. code-block:: python + + config = CosConfig(Region=region, SecretId=secret_id, SecretKey=secret_key, Token=token) # 获取配置对象 + client = CosS3Client(config) + # 创建任务接口 + response = client.ci_get_media_jobs( + Bucket='bucket' + QueueId='', + Tag='Transcode' + ) + print response + """ + headers = mapped(kwargs) + final_headers = {} + params = {} + for key in headers: + if key.startswith("response"): + params[key] = headers[key] + else: + final_headers[key] = headers[key] + headers = final_headers + + params = format_values(params) + path = "/jobs" + url = self._conf.uri(bucket=Bucket, path=path, endpoint=self._conf._endpoint_ci) + url = u"{url}?{QueueId}&{Tag}&{OrderByTime}&{States}&{Size}&{NextToken}".format( + url=to_unicode(url), + QueueId=to_unicode('queueId='+QueueId), + Tag=to_unicode('tag='+Tag), + OrderByTime=to_unicode('orderByTime='+OrderByTime), + States=to_unicode('states='+States), + Size=to_unicode('size='+str(Size)), + NextToken=to_unicode('nextToken='+NextToken) + ) + if StartCreationTime is not None: + url = u"{url}&{StartCreationTime}".format(StartCreationTime=to_unicode('startCreationTime='+StartCreationTime)) + if EndCreationTime is not None: + url = u"{url}&{EndCreationTime}".format(EndCreationTime=to_unicode('endCreationTime='+EndCreationTime)) + logger.info("list_media_jobs result, url=:{url} ,headers=:{headers}, params=:{params}".format( + url=url, + headers=headers, + params=params)) + rt = self.send_request( + method='GET', + url=url, + bucket=Bucket, + auth=CosS3Auth(self._conf, path, params=params), + params=params, + headers=headers) + logger.debug("list_media_jobs result, url=:{url} ,content=:{content}".format( + url=url, + content=rt.content)) + data = xml_to_dict(rt.content) + # 单个元素时将dict转为list + format_dict(data, ['JobsDetail']) + return data + if __name__ == "__main__": pass diff --git a/ut/test.py b/ut/test.py index e31cf937..65b0d56e 100644 --- a/ut/test.py +++ b/ut/test.py @@ -36,6 +36,9 @@ aes_provider = AESProvider() client_for_aes = CosEncryptionClient(conf, aes_provider) +ci_bucket_name = 'ci-qta-gz-1251668577' +ci_region = 'ap-guangzhou' + def _create_test_bucket(test_bucket, create_region=None): try: @@ -1574,6 +1577,137 @@ def _test_qrcode(): print(response, data) +def test_ci_get_media_queue(): + # 查询媒体队列信息 + response = client.ci_get_media_queue( + Bucket=ci_bucket_name + ) + print(response) + assert (response['QueueList']) + + +def test_ci_create_media_transcode_watermark_jobs(): + # 创建转码任务 + response = client.ci_get_media_queue( + Bucket=ci_bucket_name + ) + QueueId = response['QueueList'][0]['QueueId'] + + body = { + 'Input': { + 'Object': '117374C.mp4' + }, + 'QueueId': QueueId, + 'Tag': 'Transcode', + 'Operation': { + 'Output': { + 'Bucket': ci_bucket_name, + 'Region': ci_region, + 'Object': '117374C_output.mp4' + }, + 'TemplateId': 't02db40900dc1c43ad9bdbd8acec6075c5', + # "WatermarkTemplateId": ["", ""], + 'Watermark': [ + { + 'Type': 'Text', + 'Pos': 'TopRight', + 'LocMode': 'Absolute', + 'Dx': '64', + 'Dy': '64', + 'StartTime': '0', + 'EndTime': '1000.5', + 'Text': { + 'Text': '水印内容', + 'FontSize': '90', + 'FontType': 'simfang.ttf', + 'FontColor': '0xFFEEFF', + 'Transparency': '100', + }, + }, + { + 'Type': 'Image', + 'Pos': 'TopLeft', + 'LocMode': 'Absolute', + 'Dx': '100', + 'Dy': '100', + 'StartTime': '0', + 'EndTime': '1000.5', + 'Image': { + 'Url': 'http://'+ci_bucket_name+".cos."+ci_region+".myqcloud.com/1215shuiyin.jpg", + 'Mode': 'Fixed', + 'Width': '128', + 'Height': '128', + 'Transparency': '100', + }, + } + ] + } + } + # dict中数组类型的标签,都需要特殊处理 + lst = [ + '', + '', + '', + '' + ] + response = client.ci_create_media_jobs( + Bucket=ci_bucket_name, + Jobs=body, + Lst=lst, + ContentType='application/xml' + ) + print(response) + assert (response['JobsDetail']) + + +def test_ci_create_media_transcode_jobs(): + # 创建转码任务 + response = client.ci_get_media_queue( + Bucket=ci_bucket_name + ) + QueueId = response['QueueList'][0]['QueueId'] + body = { + 'Input': { + 'Object': '117374C.mp4' + }, + 'QueueId': QueueId, + 'Tag': 'Transcode', + 'Operation': { + 'Output': { + 'Bucket': ci_bucket_name, + 'Region': ci_region, + 'Object': '117374C_output.mp4' + }, + 'TemplateId': 't02db40900dc1c43ad9bdbd8acec6075c5' + } + } + response = client.ci_create_media_jobs( + Bucket=ci_bucket_name, + Jobs=body, + Lst={}, + ContentType='application/xml' + ) + print(response) + assert (response['JobsDetail']) + + +def test_ci_list_media_transcode_jobs(): + # 转码任务 + response = client.ci_get_media_queue( + Bucket=ci_bucket_name + ) + QueueId = response['QueueList'][0]['QueueId'] + response = client.ci_list_media_jobs( + Bucket=ci_bucket_name, + QueueId=QueueId, + Tag='Transcode', + Size=2, + ContentType='application/xml' + ) + print(response) + assert (response['JobsDetail']) + + if __name__ == "__main__": setUp() """ @@ -1606,5 +1740,9 @@ def _test_qrcode(): test_aes_client() test_rsa_client() test_qrcode() + test_ci_get_media_queue() + test_ci_create_media_transcode_watermark_jobs() + test_ci_create_media_transcode_jobs() + test_ci_list_media_transcode_jobs() """ tearDown()