Baidu Cloud Engine Python SDK

0.9.69 · active · verified Sat Apr 11

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

Install

Imports

Quickstart

This quickstart demonstrates how to initialize a client for Baidu Object Storage (BOS) using your Access Key (AK), Secret Key (SK), and a service endpoint. It shows the basic steps to configure the client and make a sample API call (listing buckets). Remember to replace 'YOUR_AK' and 'YOUR_SK' with your actual credentials, ideally from environment variables, and choose the correct endpoint for your service and region.

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}")

view raw JSON →