本文介绍Python SDK初始化Client的流程。

系统变量读取

阿里云SDK支持通过定义ALIBABA_CLOUD_ACCESS_KEY_IDALIBABA_CLOUD_ACCESS_KEY_SECRET环境变量来创建默认的访问凭证。调用接口时,程序直接访问凭证,读取您的访问密钥(即AccessKey)并自动完成鉴权。详细配置方式,请参见在Linux、macOS和Windows系统配置环境变量

初始化

  1. 初始化配置对象 alibabacloud_tea_openapi.Config。

    import os
    from alibabacloud_tea_openapi import models as open_api_models
    
    config = open_api_models.Config(
        // 必填,请确保代码运行环境设置了环境变量 ALIBABA_CLOUD_ACCESS_KEY_ID。
        access_key_id=os.environ['ALIBABA_CLOUD_ACCESS_KEY_ID'],
        // 必填,请确保代码运行环境设置了环境变量 ALIBABA_CLOUD_ACCESS_KEY_SECRET。
        access_key_secret=os.environ['ALIBABA_CLOUD_ACCESS_KEY_SECRET']
    )
    # 访问的域名
    config.endpoint = 'mts.cn-hangzhou.aliyuncs.com'
  2. 实例化一个客户端,从 alibabacloud_mts20140618.Client 类生成对象 client 。

    from alibabacloud_mts20140618.client import Client as Client
    from alibabacloud_mts20140618 import models as models
    
    client = Client(config)

完整代码

import os
import sys

from typing import List

from alibabacloud_mts20140618.client import Client as Mts20140618Client
from alibabacloud_tea_openapi import models as open_api_models
from alibabacloud_mts20140618 import models as mts_20140618_models
from alibabacloud_tea_util import models as util_models
from alibabacloud_tea_util.client import Client as UtilClient


class Sample:
    def __init__(self):
        pass

    @staticmethod
    def create_client() -> Mts20140618Client:
        """
        使用AK&SK初始化账号Client
        @return: Client
        @throws Exception
        """
        config = open_api_models.Config(
            # 必填,请确保代码运行环境设置了环境变量 ALIBABA_CLOUD_ACCESS_KEY_ID。,
            access_key_id=os.environ['ALIBABA_CLOUD_ACCESS_KEY_ID'],
            # 必填,请确保代码运行环境设置了环境变量 ALIBABA_CLOUD_ACCESS_KEY_SECRET。,
            access_key_secret=os.environ['ALIBABA_CLOUD_ACCESS_KEY_SECRET']
        )

        config.endpoint = f'mts.cn-hangzhou.aliyuncs.com'
        return Mts20140618Client(config)

    @staticmethod
    def main(
        args: List[str],
    ) -> None:
        client = Sample.create_client()
        activate_media_workflow_request = mts_20140618_models.ActivateMediaWorkflowRequest(
            media_workflow_id='your_value'
        )
        try:
            # 复制代码运行请自行打印 API 的返回值
            client.activate_media_workflow_with_options(activate_media_workflow_request, util_models.RuntimeOptions())
        except Exception as error:
            # 此处仅做打印展示,请谨慎对待异常处理,在工程项目中切勿直接忽略异常。
            # 错误 message
            print(error.message)
            # 诊断地址
            print(error.data.get("Recommend"))
            UtilClient.assert_as_string(error.message)

    @staticmethod
    async def main_async(
        args: List[str],
    ) -> None:
        client = Sample.create_client()
        activate_media_workflow_request = mts_20140618_models.ActivateMediaWorkflowRequest(
            media_workflow_id='your_value'
        )
        try:
            # 复制代码运行请自行打印 API 的返回值
            await client.activate_media_workflow_with_options_async(activate_media_workflow_request, util_models.RuntimeOptions())
        except Exception as error:
            # 此处仅做打印展示,请谨慎对待异常处理,在工程项目中切勿直接忽略异常。
            # 错误 message
            print(error.message)
            # 诊断地址
            print(error.data.get("Recommend"))
            UtilClient.assert_as_string(error.message)


if __name__ == '__main__':
    Sample.main(sys.argv[1:])