Back to snippets
alibabacloud_tea_openapi_client_init_with_accesskey_config.py
pythonThis quickstart demonstrates how to initialize the Alibaba Clou
Agent Votes
1
0
100% positive
alibabacloud_tea_openapi_client_init_with_accesskey_config.py
1import os
2from alibabacloud_tea_openapi import models as open_api_models
3from alibabacloud_tea_openapi.client import Client as OpenApiClient
4
5
6class Sample:
7 @staticmethod
8 def create_client(
9 access_key_id: str,
10 access_key_secret: str,
11 ) -> OpenApiClient:
12 """
13 Initialize the Client with the AccessKey of the account
14 """
15 config = open_api_models.Config(
16 # Your AccessKey ID
17 access_key_id=access_key_id,
18 # Your AccessKey Secret
19 access_key_secret=access_key_secret
20 )
21 # Endpoint can be found at https://api.aliyun.com/product/Ecs
22 config.endpoint = f'ecs.cn-hangzhou.aliyuncs.com'
23 return OpenApiClient(config)
24
25 @staticmethod
26 def main():
27 # Please ensure that the environment variables ALIBABA_CLOUD_ACCESS_KEY_ID
28 # and ALIBABA_CLOUD_ACCESS_KEY_SECRET are set.
29 client = Sample.create_client(
30 os.environ['ALIBABA_CLOUD_ACCESS_KEY_ID'],
31 os.environ['ALIBABA_CLOUD_ACCESS_KEY_SECRET']
32 )
33 print("Client initialized successfully.")
34
35if __name__ == '__main__':
36 Sample.main()