Alibaba Cloud Tea Util for Python
The `alibabacloud-tea-util` library is a core utility module for the Alibaba Cloud Python SDK. It provides common functionalities such as model handling, various helper methods for data manipulation and validation, and definitions for runtime options. The library is actively maintained, supports Python 3.6 and later, and serves as a foundational component for many other Alibaba Cloud SDKs.
Warnings
- gotcha Never hardcode AccessKey ID or AccessKey Secret directly in your code. Always use environment variables (`ALIBABA_CLOUD_ACCESS_KEY_ID`, `ALIBABA_CLOUD_ACCESS_KEY_SECRET`) or more secure credential management solutions like RAM roles or STS tokens to prevent credential leakage.
- gotcha The `alibabacloud-tea-util` library requires Python 3.6 or higher. Ensure your environment meets this minimum requirement to avoid compatibility issues.
- gotcha While `tea-util` provides core utilities, it's typically used as a dependency for service-specific SDKs (e.g., `alibabacloud-airec`) or the generic `alibabacloud-tea-openapi` package. For full functionality, you'll usually need to install and integrate with these other packages.
- gotcha When handling API responses or network operations, be prepared to catch `TeaException` for business-related errors or `TeaUnretryableException` for network/retry exhaustion. Direct exception handling is crucial for robust applications.
Install
-
pip install alibabacloud-tea-util
Imports
- Client
from alibabacloud_tea_util.client import Client as UtilClient
- models
from alibabacloud_tea_util import models as util_models
Quickstart
import os
from alibabacloud_tea_util.client import Client as UtilClient
from alibabacloud_tea_util import models as util_models
# NOTE: In a real application, AccessKey ID and Secret should be
# managed securely, e.g., via environment variables or STS tokens.
# NEVER hardcode credentials in your source code.
# export ALIBABA_CLOUD_ACCESS_KEY_ID="your_access_key_id"
# export ALIBABA_CLOUD_ACCESS_KEY_SECRET="your_access_key_secret"
# Example of using a utility method and a common model
def main():
try:
# Instantiate a common model provided by tea-util
runtime_options = util_models.RuntimeOptions(
connect_timeout=10000, # milliseconds
read_timeout=15000 # milliseconds
)
# You would typically pass these options to a service client,
# but here we'll just print its string representation
print(f"Created RuntimeOptions: {UtilClient.to_jsonstring(runtime_options)}")
# Example of a simple utility function - getting a current timestamp
timestamp = UtilClient.get_timestamp()
print(f"Current timestamp (GMT): {timestamp}")
except Exception as e:
print(f"An error occurred: {e}")
if __name__ == '__main__':
main()