1. 程式人生 > >IMM服務(智慧媒體管理)通過Notify查詢非同步任務的執行結果

IMM服務(智慧媒體管理)通過Notify查詢非同步任務的執行結果

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。
  • 注意事項
    1. 需要先開通 MNS 服務,新建 topic,queue,並配置好訂閱關係,詳情請參考MNS服務
    2. MNS的region必須和和IMM保持一致。

快速開始

### 初始化客戶端

  1. 程式碼示例

      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操作

  1. 程式碼示例

    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
  2. 輸出結果

    {
    "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手冊文件管理操作

  1. 程式碼示例

    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
  2. 輸出結果

    {
      "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參考

  1. 程式碼示例。

    queue = mns_client.get_queue(MNS_QUEUE_NAME)
    queue.set_encoding(False) #編碼方式設定為False,不對訊息體進行base64編碼
  2. 消費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"
}
  1. 批量消費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"
  }
]
}