IMM服务(智能媒体管理)通过Notify查询异步任务的执行结果
在使用智能媒体管理的过程中,我们可能碰到一些大的文档的转换,当我们需要知道结果的时候,一直轮询 GetOfficeConversionTask接口去获取任务状态其实是浪费时间的。其实IMM服务提供任务结果异步反向通知的功能,今天我们就使用MNS服务去获取异步任务执行结果。
准备工作
-
安装 MNS 和 IMM 的SDK
pip install aliyun-python-sdk-core # 安装阿里云 SDK 核心库 pip install aliyun-python-sdk-imm # 安装管理 IMM 的库 pip install aliyun-mns #阿里云的MNS服务的SDK
访问阿里云官网,获取AccessKeyId、AccessKeySecret、AccountId。
-
注意事项
- 需要先开通 MNS 服务,新建 topic,queue,并配置好订阅关系,详情请参考MNS服务。
- MNS的region必须和和IMM保持一致。
快速开始
### 初始化客户端
-
代码示例
IMM_REGION = 'cn-beijing' #服务区域Region,根据需要修改 ACCESS_KEY_ID = 'xxxxxxx' #您的AccessKey ACCESS_KEY_SECRET = 'xxxxxxx' #您的AccessSecret MNS_ENDPOINT = 'http://account.mns.cn-beijing.aliyuncs.com/' #Account是阿里云注册用户的帐号的ID MNS_TOPIC_NAME = 'xxxxx' #您mns服务配置的Topic MNS_QUEUE_NAME = 'xxxxx' #您mns服务配置的Queue mns_client = Account(MNS_ENDPOINT, ACCESS_KEY_ID, ACCESS_KEY_SECRET) #MNS客户端 imm_client = AcsClient(ACCESS_KEY_ID, ACCESS_KEY_SECRET, IMM_REGION) #IMM客户端
创建Project
本示例目的是展示基本使用,详情参考IMM服务API手册Project操作。
-
代码示例
from aliyunsdkimm.request.v20170906 import PutProjectRequest project = "GetResult-ByNotify-Demo" #设置Project的名字 project_type = "DocStarter" #文档标准型 project_cu = 1 createReq = PutProjectRequest.PutProjectRequest() createReq.set_Project(project) createReq.set_Type(project_type) createReq.set_Type(project_cu) response = imm_client.do_action_with_exception(createReq) print response
-
输出结果
{ "CU": 1, "Type": "DocStarter", "CreateTime": "2018-11-15T02:40:35Z", "RequestId": "3FE3F20C-2076-41B2-8DBB-73E90CF19C8C", "ModifyTime": "2018-11-15T02:40:35Z", "Project": "GetResult-ByNotify-Demo", "BillingType": "ByUsage", "Endpoint": "imm.cn-beijing.aliyuncs.com", "ServiceRole": "" }
创建文档转化任务
本示例目的是展示基本使用,详情参考IMM服务API手册文档管理操作。
-
代码示例
project = "GetResult-ByNotify-Demo" # 演示用project已创建,您需要自己创建 async_srcUri = "oss://co-user-cn-beijing/zqh/input/WORD/5页Word.docx" # 您文档资源的位置 async_tgtUri = "oss://co-user-cn-beijing/zqh/output/PDF/" # 您要输出的位置 tgt_type = "png" # 转化类型 createReq= CreateOfficeConversionTaskRequest.CreateOfficeConversionTaskRequest() createReq.set_Project(project) createReq.set_SrcUri(async_srcUri) createReq.set_TgtUri(async_tgtUri) createReq.set_TgtType(tgt_type) createReq.set_NotifyEndpoint(MNS_ENDPOINT) createReq.set_NotifyTopicName(MNS_TOPIC_NAME) response = imm_client.do_action_with_exception(createReq) print response
-
输出结果
{ "Status": "Running", "CreateTime": "2018-11-15T02:42:31.024Z", "RequestId": "9A046CDE-B4BC-47A0-B235-7D904260190D", "Percent": 0, "TaskId": "7c711cb4-279c-4773-a46d-8e39d3595691", "TgtLoc": "oss://co-user-cn-beijing/zqh/output/PDF/" }
调用MNS服务获取异步任务的执行结果。
本示例目的是展示基本使用,详情参考MNS服务API参考。
-
代码示例。
queue = mns_client.get_queue(MNS_QUEUE_NAME) queue.set_encoding(False) #编码方式设置为False,不对消息体进行base64编码
-
消费Message,查询任务执行结果。
msg = queue.receive_message() print msg.message_body
//输出结果
{
"Message": {
"events": [
{
"eventName": "FormatConvertJob:JobFinished", //任务结果
"eventSource": "acs:imm",
"eventTime": "2018-11-14T09:51:20.342Z", //完成时间
"eventVersion": "1.0",
"imm": { //任务具体信息
"code": "NoError",
"pageCount": "5",
"percent": "100",
"taskId": "723f6489-2e91-4475-93a2-41bbedf76d1f"
}
}
]
},
"MessageId": "1AF8A292DE807634-1-16711A1E057-20000076E",
"MessageMD5": "FF48C799CEF8997625D00B364D7471D1",
"PublishTime": "1542189080663",
"Subscriber": "xxxxxxx",
"SubscriptionName": "xxxxxxxx",
"TopicName": "xxxxxxxx",
"TopicOwner": "xxxxxxxxx"
}
-
批量消费Message,查询任务执行结果。(需要创建多个文档转化任务,才能看到多条Message。)
messages = queue.batch_receive_message(batch_size) #batch_size为本次请求最多获取的消息条数 for msg in messages: print msg.message_body
//输出结果
[
{
"Message": {
"events ": [
{
"eventName ": "FormatConvertJob:JobFinished ",
"eventSource ": "acs:imm ",
"eventTime ": "2018-11-14T09:51:20.342Z ",
"eventVersion ": "1.0 ",
"imm ": {
"code ": "NoError ",
"pageCount ": "5 ",
"percent ": "100 ",
"taskId ": "723f6489-2e91-4475-93a2-41bbedf76d1f "
}
}
]
},
"MessageId": "1AF8A292DE807634-1-16711A1E057-20000076E",
"MessageMD5": "FF48C799CEF8997625D00B364D7471D1",
"PublishTime": "1542189080663",
"Subscriber": "xxxxxxx",
"SubscriptionName": "xxxxxxxx",
"TopicName": "xxxxxxxx",
"TopicOwner": "xxxxxxxxx"
},
{
"Message": {
"events ": [
{
"eventName ": "FormatConvertJob:JobFinished ",
"eventSource ": "acs:imm ",
"eventTime ": "2018-11-14T09:51:47.941Z ",
"eventVersion ": "1.0 ",
"imm ": {
"code ": "NoError ",
"pageCount ": "5 ",
"percent ": "100 ",
"taskId ": "c54b5836-5270-4beb-a5e5-ceb7627e5c98 "
}
}
]
},
"MessageId": "1AF8A292DE807634-1-16711A24C0A-200000770",
"MessageMD5": "9D7EAD233258273ADC9C7FB496FE9316",
"PublishTime": "1542189108234",
"Subscriber": "xxxxxxx",
"SubscriptionName": "xxxxxxxx",
"TopicName": "xxxxxxxx",
"TopicOwner": "xxxxxxxxx"
},
{
"Message": {
"events ": [
{
"eventName ": "FormatConvertJob:JobFinished ",
"eventSource ": "acs:imm ",
"eventTime ": "2018-11-14T09:52:09.584Z ",
"eventVersion ": "1.0 ",
"imm ": {
"code ": "NoError ",
"pageCount ": "5 ",
"percent ": "100 ",
"taskId ": "2aaeaeb0-7082-4330-8489-42dcd1b877c6 "
}
}
]
},
"MessageId": "1AF8A292DE807634-1-16711A2A023-200000771",
"MessageMD5": "93B11711E9F2836CC4E65838DD6DD81D",
"PublishTime": "1542189129763",
"Subscriber": "xxxxxxx",
"SubscriptionName": "xxxxxxxx",
"TopicName": "xxxxxxxx",
"TopicOwner": "xxxxxxxxx"
}
]
}