All Products
Search
Document Center

Alibaba Cloud SDK:Instantiate a client and configure a credential

Last Updated:Aug 17, 2022

Use AccessKey pairs

from aliyunsdkcore.client import AcsClient
from aliyunsdkecs.request.v20140526.AcceptInquiredSystemEventRequest import AcceptInquiredSystemEventRequest

# Instantiate a client.
client = AcsClient(
    '<access_key_id>', # The AccessKey ID of the Resource Access Management (RAM) user.
    '<access_secret>', # The AccessKey ID of the RAM user.
    '<region_id>' # The region ID.
)

# Create an API request and configure parameters.
request = AcceptInquiredSystemEventRequest()
request.set_accept_format('json')
# Send the request and handle the response or exception.
response = client.do_action_with_exception(request)

print(str(response, encoding='utf-8'))

Use the default credential provider chain

  1. Use credentials from environment variables

    The credential provider chain searches for credentials in environment variables. If you define the environment variables ALIYUN_ACCESS_KEY_ID and ALIYUN_ACCESS_KEY_SECRET and the environment variables are not empty, the credential provider chain uses the environment variables to create a default credential.

    from aliyunsdkcore.client import AcsClient
    from aliyunsdkecs.request.v20140526.AcceptInquiredSystemEventRequest import AcceptInquiredSystemEventRequest
    
    client = AcsClient(
        region_id='<region_id>'
    )
    
    request = AcceptInquiredSystemEventRequest()
    request.set_accept_format('json')
    response = client.do_action_with_exception(request)
    
    print(str(response, encoding='utf-8'))

  2. Use STS credentials

    To ensure the security of your business, you can apply for temporary security credentials (TSC) from Security Token Service (STS) to create a temporary client.

    from aliyunsdkcore.client import AcsClient
    from aliyunsdkcore.auth.credentials import StsTokenCredential
    from aliyunsdkecs.request.v20140526.AcceptInquiredSystemEventRequest import AcceptInquiredSystemEventRequest
    
    cred = StsTokenCredential(
        sts_access_key_id = '<sts_access_key_id>',
        sts_access_key_secret = '<sts_access_key_secret>',
        sts_token = '<sts_token>'
    )
    
    client = AcsClient(
        region_id='<region_id>',
        credential=cred
    )
    
    request = AcceptInquiredSystemEventRequest()
    request.set_accept_format('json')
    response = client.do_action_with_exception(request)
    
    print(str(response, encoding='utf-8'))

  3. Use RamRoleArn credentials

    You can assign a RAM role to a client. Then, the client can automatically apply for and maintain STS tokens before the client initiates an API request. This way, the client becomes an STS client that has a validity period. You can also apply for an STS token and create an STS client.

    from aliyunsdkcore.client import AcsClient
    from aliyunsdkcore.auth.credentials import RamRoleArnCredential
    from aliyunsdkecs.request.v20140526.AcceptInquiredSystemEventRequest import AcceptInquiredSystemEventRequest
    
    cred = RamRoleArnCredential(
        sts_access_key_id='<sts_access_key_id>',
        sts_access_key_secret='<sts_access_key_secret>',
        role_arn='<role_arn>',
        session_role_name='<session_role_name>'
    )
    
    client = AcsClient(
        region_id='<region_id>',
        credential=cred
    )
    
    request = AcceptInquiredSystemEventRequest()
    request.set_accept_format('json')
    response = client.do_action_with_exception(request)
    
    print(str(response, encoding='utf-8'))

  4. Use EcsRamRole credentials

    You can assign a RAM role to a client. Then, the client applies for an STS token from http://100.100.100.200/latest/meta-data/ram/security-credentials/.

    from aliyunsdkcore.client import AcsClient
    from aliyunsdkcore.auth.credentials import EcsRamRoleCredential
    from aliyunsdkecs.request.v20140526.AcceptInquiredSystemEventRequest import AcceptInquiredSystemEventRequest
    
    cred = EcsRamRoleCredential(
        role_name='<role name>'
    )
    
    client = AcsClient(
        region_id='<region_id>',
        credential=cred
    )
    
    request = AcceptInquiredSystemEventRequest()
    request.set_accept_format('json')
    response = client.do_action_with_exception(request)
    
    print(str(response, encoding='utf-8'))