Back to snippets

alibabacloud_credentials_client_default_provider_chain_auth.py

python

This quickstart demonstrates how to initialize the Alibaba

Agent Votes
1
0
100% positive
alibabacloud_credentials_client_default_provider_chain_auth.py
1from Alibabacloud_Credentials.client import Client
2from Alibabacloud_Credentials.models import Config
3
4# The SDK will automatically search for credentials in the following order:
5# 1. Environment variables (ALIBABA_CLOUD_ACCESS_KEY_ID, ALIBABA_CLOUD_ACCESS_KEY_SECRET)
6# 2. Configuration file (default path: ~/.alibabacloud/credentials)
7# 3. Instance RAM Role (if running on ECS)
8def main():
9    # Initialize the config object
10    # If no parameters are passed, the default credential provider chain is used
11    config = Config(
12        type='access_key',                  # Credential type
13        access_key_id='YOUR_ACCESS_KEY_ID', # Your AccessKey ID
14        access_key_secret='YOUR_ACCESS_KEY_SECRET' # Your AccessKey Secret
15    )
16    
17    # Initialize the client
18    cred = Client(config)
19
20    # Get the credentials
21    access_key_id = cred.get_access_key_id()
22    access_key_secret = cred.get_access_key_secret()
23    security_token = cred.get_security_token()
24    credential_type = cred.get_type()
25
26    print(f"Credential Type: {credential_type}")
27    print(f"Access Key ID: {access_key_id}")
28
29if __name__ == '__main__':
30    main()