Volcengine SDK for Python
The `volcengine` library is the official Python SDK for interacting with Volcengine cloud services. It provides client interfaces for various services like STS, ECS, IAM, and more. It is actively maintained with frequent releases, often on a daily or weekly basis.
Common errors
-
No module named 'volcengine.ecs.ECSService'
cause The `volcengine` package or a specific service sub-module is not installed, or the import path is incorrect.fixEnsure the `volcengine` package is installed via `pip install volcengine`. Verify the exact import path against the official documentation or examples for the specific service you are trying to use. -
Status code: 401, Error code: SignatureDoesNotMatch
cause The provided Volcengine AccessKey ID, Secret Access Key, or Region is incorrect or does not match the request signature.fixDouble-check the `VOLCENGINE_ACCESSKEYID`, `VOLCENGINE_SECRETACCESSKEY`, and `VOLCENGINE_REGION` environment variables or the values passed to `set_ak()`, `set_sk()`, `set_region()` methods. Ensure they correspond to a valid Volcengine account and region. -
AttributeError: 'StsService' object has no attribute 'get_caller_identity'
cause This usually indicates a typo in the API method name, using an outdated client version, or attempting to call a method that doesn't exist for the service/client.fixVerify the exact method name and its arguments in the official API documentation for your SDK version. Ensure your SDK is up to date with `pip install --upgrade volcengine`.
Warnings
- gotcha Authentication requires correct `VOLCENGINE_ACCESSKEYID`, `VOLCENGINE_SECRETACCESSKEY`, and `VOLCENGINE_REGION` environment variables or explicit client configuration. Misconfiguration is the most common cause of API failures.
- gotcha API request and response objects are typically Protocol Buffer messages, imported from `volcengine.<service_name>.model.<service_name>_pb2`. Failing to use these specific types can lead to `TypeError` or `AttributeError`.
- gotcha Ensure `protobuf` and `grpcio` are correctly installed and compatible. Version mismatches between these core dependencies or with the SDK can lead to cryptic errors during API calls or client initialization.
Install
-
pip install volcengine
Imports
- StsService
from volcengine.sts.StsService import StsService
- GetCallerIdentityRequest
from volcengine.sts.model.sts_pb2 import GetCallerIdentityRequest
- ECSService
from volcengine.ecs.ECSService import ECSService
Quickstart
import os
from volcengine.sts.StsService import StsService
from volcengine.sts.model.sts_pb2 import GetCallerIdentityRequest
# Initialize STS service client with credentials from environment variables
sts_service = StsService()
sts_service.set_ak(os.environ.get("VOLCENGINE_ACCESSKEYID", ""))
sts_service.set_sk(os.environ.get("VOLCENGINE_SECRETACCESSKEY", ""))
sts_service.set_region(os.environ.get("VOLCENGINE_REGION", "cn-beijing")) # Default to cn-beijing if not set
# Create a request object
req = GetCallerIdentityRequest()
try:
# Call the API
resp = sts_service.get_caller_identity(req)
# Print the response
print("Successfully called GetCallerIdentity:")
print(f" Account ID: {resp.AccountId}")
print(f" User ID: {resp.UserId}")
print(f" User Name: {resp.UserName}")
except Exception as e:
print(f"Error calling GetCallerIdentity: {e}")