Ejemplo n.º 1
0
    def init_credentials_provider(self):
        from alibabacloud.credentials import AccessKeyCredentials

        credentials = AccessKeyCredentials(self.access_key_id,
                                           self.access_key_secret)
        from alibabacloud.credentials.provider import StaticCredentialsProvider
        credentials_provider = StaticCredentialsProvider(credentials)
        return credentials_provider
        def my_credentials_provider():
            from alibabacloud.credentials import AccessKeyCredentials

            credentials = AccessKeyCredentials(self.access_key_id,
                                               'BadAccessKeySecret')
            from alibabacloud.credentials.provider import StaticCredentialsProvider
            credentials_provider = StaticCredentialsProvider(credentials)
            return credentials_provider
        def init_credentials_provider():
            from alibabacloud.credentials import AccessKeyCredentials

            credentials = AccessKeyCredentials(
                access_key_id=self.access_key_id,
                access_key_secret="BadAccessKeySecret")
            from alibabacloud.credentials.provider import StaticCredentialsProvider
            credentials_provider = StaticCredentialsProvider(credentials)
            return credentials_provider
Ejemplo n.º 4
0
    def init_sub_client_config(self):
        self._create_default_ram_user()
        # self._attach_default_policy()
        self._create_access_key()
        sub_client_config = ClientConfig(region_id=self.region_id,
                                         connection_timeout=120)

        credentials = AccessKeyCredentials(self.ram_user_access_key_id,
                                           self.ram_user_access_key_secret, )
        from alibabacloud.credentials.provider import StaticCredentialsProvider
        sub_credentials_provider = StaticCredentialsProvider(credentials)

        return sub_client_config, sub_credentials_provider
def get_client(service_name,
               api_version=None,
               region_id=None,
               endpoint=None,
               access_key_id=None,
               access_key_secret=None,
               credentials_provider=None,
               retry_policy=None,
               endpoint_resolver=None,
               config=None):
    """
    获取 `Alibaba Cloud Python SDK` 某个产品某个Version的client

    :param service_name: 产品的product_code,比如Ecs
    :type service_name: str

    :param api_version: 产品的version,格式:2018-06-09
    :type api_version: str

    :param region_id: 参照 `可用区 <https://help.aliyun.com/document_detail/40654.html>`_
    :type region_id: str

    :param endpoint: 自定义的endpoint,不经过endpoint的解析流程
    :type endpoint: str

    :param access_key_id: 秘钥AccessKeyID
    :type access_key_id: str

    :param access_key_secret: 秘钥AccessKeySecret
    :type access_key_secret: str

    :param credentials_provider: 自定义的credentials_provider,如果用户自定义
        credentials_provider,使用用户自定义的
    :type credentials_provider: 一个具备provide接口的对象

    :param retry_policy: 用户自定义的重试策略
    :type retry_policy:

    :param endpoint_resolver: 用户自定义的endpoint_resolver,如果用户自定义
        endpoint_resolver,使用用户自定义的
    :type endpoint_resolver: 一个具备resolve接口的对象

    :param config: 如果用户自定义ClientConfig,使用用户自定义的,否则初始化一个ClientConfig。
        如果用户在 `get_client()` 指定region_id ,则会覆盖config当中的region_id
    :type config: ClientConfig

    :return: client
    :rtype: AlibabaCloudClient

    """
    module_name, client_name = _prepare_module(
        service_name, api_version)  # ecs_20180909, EcsClient
    if region_id is not None and config:
        config.region_id = region_id
    if endpoint is not None and config:
        config.endpoint = endpoint
    client_config = config if config else ClientConfig(region_id=region_id,
                                                       endpoint=endpoint)

    try:
        client_module = __import__(
            '.'.join(['alibabacloud', 'clients', module_name]), globals(),
            locals(), ['clients', module_name], 0)

    except ImportError:
        raise NoModuleException(name='.'.join(module_name))

    if credentials_provider is not None:
        custom_credentials_provider = credentials_provider

    elif access_key_id and access_key_secret:
        custom_credentials_provider = StaticCredentialsProvider(
            AccessKeyCredentials(access_key_id, access_key_secret))
    else:
        custom_credentials_provider = DefaultChainedCredentialsProvider(
            client_config)

    return getattr(client_module, client_name)(
        client_config,
        retry_policy=retry_policy,
        credentials_provider=custom_credentials_provider,
        endpoint_resolver=endpoint_resolver)