Package: tencent-cloud-sdk
Tencent Cloud SDK for PythonA simple, efficient and easy to use Tencent Cloud SDK implemented in Python.By integrating and using the Tencent Cloud SDK for Python, you can achieve fast and secure access to the Tencent Cloud API in an object-oriented programming manner, without having to pay attention to implementation details.Features:Tencent Cloud SDK for Python supports full asynchronous programming.It is important to note that you cannot useasyncio.runto call asynchronous entry functions. See theasynchronous programming example codefor details.NavigationHomepageDocumentationRelease NotesLicenseInstallationWith the Python package manager, you can quickly install the Tencent Cloud SDK for Python.pipinstalltencent-cloud-sdkIf you want to integrate only the products you need, you can take a subcontracted installation of the Tencent Cloud SDK for Python. First we need to install the base package:pipinstalltencent-cloud-sdk-auth
pipinstalltencent-cloud-sdk-coreThen install the packages for the required products, such as Serverless Cloud Function:pipinstalltencent-cloud-sdk-serverless-functionsTips:Please note that your uninstallation method should be the same as the installation method, otherwise some packages may not be removed.Getting StartedWith the Tencent Cloud SDK for Python, you can use Tencent Cloud products with Python's object-oriented programming.Currently Tencent Cloud SDK for Python supports programmatic use in synchronous and asynchronous ways. Asynchronous way can write applications with better performance. Below we will show you how to use the Tencent Cloud SDK for Python in multiple consecutive code snippets using synchronous programming.Serverless Cloud FunctionFor example, Invoke has created a Serverless Cloud Function:Import PackageFirst, we need to import the packages we use:fromtencent.cloud.coreimporterrorsfromtencent.cloud.authimportcredentialsfromtencent.cloud.serverlessimportfunctionsAccess CredentialsNext, we need to instantiate an access credential so that Tencent Cloud can determine that you have the right to operate.access_credentials=credentials.Credentials(secret_id='AKIDF3sMOAU1pOgkmrKHchX6TZQ1Mo1C5qa7',secret_key='b4JL8fwxkIgsKMXGi39yYt0ECxZw4wZf')Thesecret_idandsecret_keygiven in the sample code above are demo credentials. You need to go to theTencent Cloud Consoleto obtain credentials for your account.Environmental CredentialsThe best practice is to use environmental credentials. By default, the SDK will automatically search for environment variables namedTENCENTCLOUD_SECRETIDandTENCENTCLOUD_SECRETKEY. If you need to customize the name of an environment variable, you must explicitly initialize thecredentials.EnvironmentalCredentialsclass.File CredentialsAlternatively, you can use file credentials. By default, the SDK will automatically search for a credentials file namedcredentials.jsonin the.,./.tencentand~/.tencentdirectories. The credentials file contains 2 attributes,secretIdandsecretKey. If you need to customize the path to the credentials file, you must explicitly initialize thecredentials.FileCredentialsclass.By default, you do not need to explicitly initialize any credentials, and the relevant parameters should always be set toNoneor ignored.Remember, please prioritize the use of environmental credentials or file credentials over hard-coded credentials. For more details, please seecredentials.EnvironmentalCredentialsandcredentials.FileCredentials.Product ClientThen, we need to instantiate a product client for Serverless Cloud Function:function_client:functions.Client=functions.Client(access_credentials=access_credentials)The parametercredentials_contextis optional. If you use the Tencent Cloud SDK for Python in a Serverless Cloud Function container, this parameter can be ignored or set toNone.Invoke Cloud FunctionFinally, we try to Invoke a Cloud Function and get the return value. Suppose we have a Cloud Functionhelloin the namespacedefaultof the data centerap-shanghai:return_value:str=function_client.easy_invoke(region_id='ap-shanghai',# Unique identifier of the data centernamespace_name='default',# Name of the namespacefunction_name='hello'# Name of the Cloud Function)The local variablereturn_valuenow has the actual return value of the Cloud Function.The methodeasy_invokewill attempt to infer the return value in the given Cloud Function result and return the return value as a Python native data type.Tips:If an error occurs for a given Cloud Function runtime, anInvokeErrorexception is thrown. The above exceptions are defined in thetencent.cloud.serverless.functions.errorsmodule.Close product clientWhen the instantiated client is no longer in use, we should explicitly close it:function_client.close()See theexamples source codefor the complete demo code.Other Tencent Cloud ProductsFor Tencent Cloud products that do not yet provide a product client in the Tencent Cloud SDK, a universal client can be used.Import PackagesAs with the Serverless Cloud Function product, we need to import the packages we use:fromtencent.cloud.coreimporterrorsfromtencent.cloud.coreimportclientfromtencent.cloud.authimportcredentialsAccess CredentialsAs with the Serverless Cloud Function product, we need to instantiate an access credential. For this paragraph, please refer to the Serverless Cloud Function product getting started demo.Product ClientSince the Tencent Cloud product we need to use does not provide a product client, we need to instantiate a universal client.Below we take Cloud Virtual Machine (CVM) products as an example:cvm_client=client.UniversalClient(product_id='cvm',# Unique identifier of the productaccess_credentials=access_credentials# Access credentials)Use Tencent Cloud APIFinally, we try to retrieve Zone information operated by a given data center campus, which will use the Tencent Cloud API calledDescribeZones:action_result:dict=cvm_client.action(region_id='ap-shanghai',# Unique identifier of the data centeraction_id='DescribeZones',# Unique identifier of the Tencent Cloud APIaction_parameters=None,action_version='2017-03-12'# Version name of the Tencent Cloud API)Tips:If a given Tencent Cloud API encounters an error, anActionErrorexception is thrown; if the given Tencent Cloud API response is not as expected, anActionResultErrorexception is thrown. The above exceptions are defined in thetencent.cloud.core.errorsmodule.Print unique identifiers for all zones:forzone_infoinaction_result['ZoneSet']:print(zone_info['Zone'])For more ways to use Tencent Cloud SDK for Python, see our online documentation. Thank you!LicenseThe Tencent Cloud SDK for Python is open source using the MIT license, which means that your use is subject to the license, pleaseviewlicense details.It is worth noting that the Tencent Cloud SDK for Python is using a number of open source dependency packages that are located within a package calledtencent-cloud-sdk-common. Our use of these dependency packages is governed by the open source license issued with them, and details of the dependency packages can be found atCommon Component Package.OtherIf you encounter any problems during use, you are welcome to navigate to theIssuespage to submit and we will be happy to assist you with the problem.