Volcengine SDK for Python
The Volcengine SDK for Python provides comprehensive access to Volcengine cloud services, including compute, storage, networking, and AI. It is currently at version 5.0.23 and receives frequent minor updates adding new services and API versions.
Warnings
- gotcha Missing or incorrect authentication credentials (Access Key and Secret Key) are the most common cause of API request failures. Ensure 'VOLCENGINE_AK' and 'VOLCENGINE_SK' environment variables are correctly set or provided to the client.
- gotcha Volcengine services are region-specific. Making requests to a service endpoint in a region where the service is not deployed, or using the wrong region for your resources, will result in errors.
- gotcha The SDK offers a generic `volcengine.client.Client` and numerous service-specific clients (e.g., `volcengine.iam.IAMService`). The generic client requires manual construction of `ServiceInfo` and `APIInfo` for each call, while service-specific clients provide type-hinted methods for ease of use.
Install
-
pip install volcengine-python-sdk
Imports
- Client
from volcengine.client import Client
- IAMService
from volcengine.iam.IAMService import IAMService
- StsService
from volcengine.sts.StsService import StsService
Quickstart
import os
from volcengine.iam.IAMService import IAMService
# Set your Volcengine Access Key (AK) and Secret Key (SK) as environment variables
# or replace with actual credentials. NEVER hardcode production credentials.
VOLCENGINE_AK = os.environ.get("VOLCENGINE_AK", "YOUR_AK")
VOLCENGINE_SK = os.environ.get("VOLCENGINE_SK", "YOUR_SK")
if VOLCENGINE_AK == "YOUR_AK" or VOLCENGINE_SK == "YOUR_SK":
print("Please set VOLCENGINE_AK and VOLCENGINE_SK environment variables or replace placeholders.")
exit(1)
try:
# Initialize the IAM service client
iam_service = IAMService()
iam_service.set_ak(VOLCENGINE_AK)
iam_service.set_sk(VOLCENGINE_SK)
# Default region is cn-beijing. You can set it explicitly if needed:
# iam_service.set_region("cn-beijing")
# Example: List up to 10 access keys for the current account
print("Attempting to list access keys...")
resp = iam_service.list_access_keys({"Limit": 10})
if resp.get("ResponseMetadata", {}).get("Error"):
error_message = resp["ResponseMetadata"]["Error"]["Message"]
print(f"Error listing access keys: {error_message}")
else:
access_keys = resp.get("Result", {}).get("AccessKeys", [])
print(f"Successfully listed {len(access_keys)} access keys.")
for key in access_keys:
print(f" Access Key Id: {key.get('AccessKeyId')}, Status: {key.get('Status')}")
except Exception as e:
print(f"An error occurred: {e}")