Baidu Cloud Engine Python SDK
The BCE Python SDK is the official software development kit for interacting with Baidu Cloud Engine services. It provides a convenient way for Python developers to integrate their applications with various Baidu AI Cloud products and services, such as object storage (BOS), virtual private cloud (VPC), and more. The current version is 0.9.69, and releases appear to be on an as-needed basis rather than a strict cadence, with the last update on PyPI on March 29, 2026.
Warnings
- deprecated The SDK officially supports Python 2.7, but Python 2.7 reached End-of-Life in 2020. Running new projects or production environments on Python 2.7 is highly discouraged due to security vulnerabilities and lack of community support. The `future` dependency is also noted to be incompatible with Python 3.13.
- gotcha Each Baidu Cloud service (e.g., BOS, VPC, BCC) and region has a specific endpoint. Using an incorrect endpoint will result in connection errors or incorrect service interaction.
- gotcha Access Key ID (AK) and Secret Access Key (SK) provide full programmatic access to your Baidu Cloud resources. Exposing them in source code, committing them to version control, or storing them insecurely can lead to unauthorized access and significant security risks.
Install
-
pip install bce-python-sdk
Imports
- BceCredentials
from baidubce.auth.bce_credentials import BceCredentials
- BceClientConfiguration
from baidubce.bce_client_configuration import BceClientConfiguration
- BosClient
from baidubce.services.bos import bos_client
Quickstart
import os
from baidubce.auth.bce_credentials import BceCredentials
from baidubce.bce_client_configuration import BceClientConfiguration
from baidubce.services.bos import bos_client
from baidubce.exception import BceHttpClientError
# Configure your Baidu Cloud credentials and endpoint
# It's highly recommended to use environment variables for AK/SK
ACCESS_KEY_ID = os.environ.get('BCE_ACCESS_KEY_ID', 'YOUR_AK')
SECRET_ACCESS_KEY = os.environ.get('BCE_SECRET_ACCESS_KEY', 'YOUR_SK')
BOS_ENDPOINT = os.environ.get('BCE_BOS_ENDPOINT', 'http://bj.bcebos.com') # Example: Beijing region for BOS
if __name__ == '__main__':
if ACCESS_KEY_ID == 'YOUR_AK' or SECRET_ACCESS_KEY == 'YOUR_SK':
print("Please set BCE_ACCESS_KEY_ID and BCE_SECRET_ACCESS_KEY environment variables.")
else:
try:
# Initialize client configuration
config = BceClientConfiguration(
credentials=BceCredentials(access_key_id=ACCESS_KEY_ID, secret_access_key=SECRET_ACCESS_KEY),
endpoint=BOS_ENDPOINT
)
# Create a BOS client
client = bos_client.BosClient(config)
# Example: List buckets (replace with actual service calls)
response = client.list_buckets()
print("Successfully connected to BOS. Buckets found:")
for bucket in response.body.buckets:
print(f"- {bucket.name}")
except BceHttpClientError as e:
print(f"An HTTP error occurred: {e}")
except Exception as e:
print(f"An unexpected error occurred: {e}")