このトピックでは、ApsaraVideo Live が提供する Python サーバー SDK の使用方法について説明し、関連するサンプルコードを提供します。プロダクションスタジオを照会する API を例に、ApsaraVideo Live API の呼び出し方法を解説します。
前提条件
Python 2.7 以降がインストールされていること。
Python サーバー SDK がダウンロードされていること。詳細については、「SDK ダウンロード」をご参照ください。
操作手順
SDK のインストール
Alibaba Cloud Core SDK のインストール
sudo pip install aliyun-python-sdk-core
ApsaraVideo Live SDK のインストール
sudo pip install aliyun-python-sdk-live
SDK の更新
Alibaba Cloud Core SDK の更新
sudo pip install aliyun-python-sdk-core --upgrade
ApsaraVideo Live SDK の更新
sudo pip install aliyun-python-sdk-live --upgrade
Python サーバー 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): # 文字列 return self.get_query_params().get('StartTime') def set_StartTime(self, StartTime): # 文字列 self.add_query_param('StartTime', StartTime) def get_PageNum(self): # 整数 return self.get_query_params().get('PageNum') def set_PageNum(self, PageNum): # 整数 self.add_query_param('PageNum', PageNum) def get_CasterName(self): # 文字列 return self.get_query_params().get('CasterName') def set_CasterName(self, CasterName): # 文字列 self.add_query_param('CasterName', CasterName) def get_PageSize(self): # 整数 return self.get_query_params().get('PageSize') def set_PageSize(self, PageSize): # 整数 self.add_query_param('PageSize', PageSize) def get_NormType(self): # 文字列 return self.get_query_params().get('NormType') def set_NormType(self, NormType): # 文字列 self.add_query_param('NormType', NormType) def get_CasterId(self): # 文字列 return self.get_query_params().get('CasterId') def set_CasterId(self, CasterId): # 文字列 self.add_query_param('CasterId', CasterId) def get_EndTime(self): # 文字列 return self.get_query_params().get('EndTime') def set_EndTime(self, EndTime): # 文字列 self.add_query_param('EndTime', EndTime) def get_OwnerId(self): # 長整数 return self.get_query_params().get('OwnerId') def set_OwnerId(self, OwnerId): # 長整数 self.add_query_param('OwnerId', OwnerId) def get_OrderByModifyAsc(self): # 文字列 return self.get_query_params().get('OrderByModifyAsc') def set_OrderByModifyAsc(self, OrderByModifyAsc): # 文字列 self.add_query_param('OrderByModifyAsc', OrderByModifyAsc) def get_ChargeType(self): # 整数 return self.get_query_params().get('ChargeType') def set_ChargeType(self, ChargeType): # 整数 self.add_query_param('ChargeType', ChargeType) def get_Status(self): # 整数 return self.get_query_params().get('Status') def set_Status(self, Status): # 整数 self.add_query_param('Status', Status)ファイル内の DescribeCastersRequest クラスには、対応する
get_Xメソッドとset_Xメソッドが複数セットあります。これらは、それぞれ API リクエストのパラメーターを取得および指定するために使用されます。config.iniという名前の設定ファイルを作成し、confディレクトリに配置します。設定ファイルに AccessKey ID と AccessKey Secret を含めます。例:[default] access_key_id = YOUR_ACCESS_KEY_ID access_key_secret = YOUR_ACCESS_KEY_SECRETYOUR_ACCESS_KEY_ID と YOUR_ACCESS_KEY_SECRET を、実際の AccessKey ID と AccessKey Secret に置き換えます。
次に、以下の Python コードを使用して設定ファイルを読み取り、SDK を呼び出すことができます。
次のサンプルコードは、SDK の v20161101 の DescribeCasters API を例としています。
#!/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') # Alibaba Cloud アカウントの AccessKey ペアは、すべての API オペレーションに対するアクセス権限を持っています。RAM ユーザーの AccessKey ペアを使用して API オペレーションを呼び出すか、日常の O&M を実行することを推奨します。 # AccessKey ペア (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') # クライアントを初期化します。 # Alibaba Cloud アカウントの AccessKey ID と AccessKey Secret を使用します。これらは Alibaba Cloud 管理コンソールで取得できます。 # この例では、リージョンを cn-hangzhou に設定します。 credentials = AccessKeyCredential(access_key_id, access_key_secret) # STS トークンを使用 # 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:01Z") # リクエストを送信します。 response = client.do_action_with_exception(request) # Python 2 の場合: print(response) # 結果を取得します。 print(str(response, encoding='utf-8'))
次のステップ
関連する SDK をアンインストールするには、次のコマンドを実行します。
sudo pip uninstall aliyun-python-sdk-core
sudo pip uninstall aliyun-python-sdk-live