本文介紹ApsaraVideo for Live服務端Python SDK的使用方法和範例程式碼。以查詢導播台列表介面為例,協助您快速掌握ApsaraVideo for Live介面的使用方法。
前提條件
已安裝Python 2.7或以上版本。
已下載服務端Python SDK,詳細資料,請參見SDK下載。
操作步驟
安裝SDK。
安裝阿里雲核心SDK。
sudo pip install aliyun-python-sdk-core
安裝阿里雲ApsaraVideo for LiveSDK。
sudo pip install aliyun-python-sdk-live
更新SDK。
更新阿里雲核心SDK。
sudo pip install aliyun-python-sdk-core --upgrade
更新阿里雲ApsaraVideo for LiveSDK。
sudo pip install aliyun-python-sdk-live --upgrade
使用SDK。
檔案說明。本文以SDK中的
DescribeCastersRequest.py
檔案為例介紹。# Licensed to the Apache Software Foundation (ASF) under one # or more contributor license agreements. See the NOTICE file # distributed with this work for additional information # regarding copyright ownership. The ASF licenses this file # to you under the Apache License, Version 2.0 (the # "License"); you may not use this file except in compliance # with the License. You may obtain a copy of the License at # # # http://www.apache.org/licenses/LICENSE-2.0 # # # Unless required by applicable law or agreed to in writing, # software distributed under the License is distributed on an # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY # KIND, either express or implied. See the License for the # specific language governing permissions and limitations # under the License. from aliyunsdkcore.request import RpcRequest from aliyunsdklive.endpoint import endpoint_data class DescribeCastersRequest(RpcRequest): def __init__(self): RpcRequest.__init__(self, 'live', '2016-11-01', 'DescribeCasters','live') self.set_method('POST') if hasattr(self, "endpoint_map"): setattr(self, "endpoint_map", endpoint_data.getEndpointMap()) if hasattr(self, "endpoint_regional"): setattr(self, "endpoint_regional", endpoint_data.getEndpointRegional()) def get_StartTime(self): # String return self.get_query_params().get('StartTime') def set_StartTime(self, StartTime): # String self.add_query_param('StartTime', StartTime) def get_PageNum(self): # Integer return self.get_query_params().get('PageNum') def set_PageNum(self, PageNum): # Integer self.add_query_param('PageNum', PageNum) def get_CasterName(self): # String return self.get_query_params().get('CasterName') def set_CasterName(self, CasterName): # String self.add_query_param('CasterName', CasterName) def get_PageSize(self): # Integer return self.get_query_params().get('PageSize') def set_PageSize(self, PageSize): # Integer self.add_query_param('PageSize', PageSize) def get_NormType(self): # String return self.get_query_params().get('NormType') def set_NormType(self, NormType): # String self.add_query_param('NormType', NormType) def get_CasterId(self): # String return self.get_query_params().get('CasterId') def set_CasterId(self, CasterId): # String self.add_query_param('CasterId', CasterId) def get_EndTime(self): # String return self.get_query_params().get('EndTime') def set_EndTime(self, EndTime): # String self.add_query_param('EndTime', EndTime) def get_OwnerId(self): # Long return self.get_query_params().get('OwnerId') def set_OwnerId(self, OwnerId): # Long self.add_query_param('OwnerId', OwnerId) def get_OrderByModifyAsc(self): # String return self.get_query_params().get('OrderByModifyAsc') def set_OrderByModifyAsc(self, OrderByModifyAsc): # String self.add_query_param('OrderByModifyAsc', OrderByModifyAsc) def get_ChargeType(self): # Integer return self.get_query_params().get('ChargeType') def set_ChargeType(self, ChargeType): # Integer self.add_query_param('ChargeType', ChargeType) def get_Status(self): # Integer return self.get_query_params().get('Status') def set_Status(self, Status): # Integer self.add_query_param('Status', Status)
該檔案中對於DescribeCastersRequest這個類,有若干組一一對應的
get_X
和set_X
方法,分別用於擷取和設定該API請求的參數。建立一個名為 config.ini 的設定檔放在建議目錄conf中,其中包含 AK/SK 資訊。設定檔的內容如下:
[default] access_key_id = YOUR_ACCESS_KEY_ID access_key_secret = YOUR_ACCESS_KEY_SECRET
請將 YOUR_ACCESS_KEY_ID 和 YOUR_ACCESS_KEY_SECRET 替換為您的實際 AK/SK 值。
然後,可以使用以下 Python 代碼讀取設定檔並調用阿里雲 SDK。
此處以v20161101版本為例,調用DescribeCasters介面:
#!/usr/bin/env python #coding=utf-8 from configparser import ConfigParser from aliyunsdkcore.client import AcsClient from aliyunsdkcore.acs_exception.exceptions import ClientException from aliyunsdkcore.acs_exception.exceptions import ServerException from aliyunsdkcore.auth.credentials import AccessKeyCredential from aliyunsdkcore.auth.credentials import StsTokenCredential from aliyunsdklive.request.v20161101.DescribeCastersRequest import DescribeCastersRequest config = ConfigParser() config.read('conf/config.ini') # 阿里雲帳號AccessKey擁有所有API的存取權限,建議您使用RAM使用者進行API訪問或日常營運。 # 強烈建議不要把AccessKey ID和AccessKey Secret儲存到工程代碼裡,否則可能導致AccessKey泄露,威脅您帳號下所有資源的安全。 # 本樣本通過從設定檔中讀取AccessKey,來實現API訪問的身分識別驗證。 access_key_id = config.get('default', 'access_key_id') access_key_secret = config.get('default', 'access_key_secret') #初始化client #AK和Secrect需要自己提供(可以在阿里雲控制台中找到) #以訪問cn-hangzhou region為例 credentials = AccessKeyCredential(access_key_id, access_key_secret) # use STS Token # credentials = StsTokenCredential('<your-access-key-id>', '<your-access-key-secret>', '<your-sts-token>') client = AcsClient(region_id='cn-hangzhou', credential=credentials) #構造需發起的API請求調用 request = DescribeCastersRequest() request.set_accept_format('json') request.set_CasterId("testCase") request.set_CasterName("testName") request.set_StartTime("2020-01-01T12:00:01") #發起請求 response = client.do_action_with_exception(request) # python2: print(response) #請求結果 print(str(response, encoding='utf-8'))
後續步驟
如果您需要移除SDK,請執行以下命令:
sudo pip uninstall aliyun-python-sdk-core
sudo pip uninstall aliyun-python-sdk-live