← 返回
未分类 Key 中文

Alibabacloud Sdk Client Initialization For Python

Initialize and manage Alibaba Cloud SDK clients in Python. Covers singleton pattern, thread safety, endpoint vs region configuration, VPC endpoints, async mo...
在 Python 中初始化和管理阿里云 SDK 客户端,包括单例模式、线程安全、endpoint 与 region 配置、VPC 端点、异步模式等要点。
yndu13
未分类 clawhub v0.0.1-beta 1 版本 100000 Key: 需要
★ 0
Stars
📥 318
下载
💾 0
安装
1
版本
#latest

概述

Client Initialization Best Practices (Python)

Core Rules

  • Client is thread-safe — safe to share across threads without additional locking.
  • Use singleton pattern — do NOT create new client instances per request. Frequent client creation wastes resources.
  • Prefer explicit endpoint over region-based endpoint resolution.

Recommended Client Creation

import os
from threading import Lock
from alibabacloud_tea_openapi.models import Config
from alibabacloud_ecs20140526.client import Client as EcsClient

_client = None
_lock = Lock()

def get_ecs_client() -> EcsClient:
    global _client
    if _client is None:
        with _lock:
            if _client is None:
                config = Config(
                    access_key_id=os.environ['ALIBABA_CLOUD_ACCESS_KEY_ID'],
                    access_key_secret=os.environ['ALIBABA_CLOUD_ACCESS_KEY_SECRET'],
                    endpoint='ecs.cn-hangzhou.aliyuncs.com',
                )
                _client = EcsClient(config)
    return _client

Endpoint Configuration

Priority: explicit endpoint > region-based resolution via region_id.

# Preferred: explicit endpoint
config = Config(endpoint='ecs.cn-hangzhou.aliyuncs.com')

# Alternative: SDK resolves endpoint from region
config = Config(region_id='cn-hangzhou')

VPC Endpoints

Use VPC endpoints when running inside Alibaba Cloud VPC:

config = Config(endpoint='ecs-vpc.cn-hangzhou.aliyuncs.com')

File Upload APIs (Advance)

Set both region_id and endpoint to the same region. Optionally set open_platform_endpoint and endpoint_type for VPC:

config = Config(
    region_id='cn-shanghai',
    endpoint='objectdet.cn-shanghai.aliyuncs.com',
    open_platform_endpoint='openplatform-vpc.cn-shanghai.aliyuncs.com',
    endpoint_type='internal',
)

SDK Components

ComponentInstall Command
---------------------------
Core SDKpip install alibabacloud-tea-openapi
Product SDKpip install alibabacloud_ecs20140526 (example)

Async Mode

Python SDK supports async calls via _async method suffix:

import asyncio
from alibabacloud_ecs20140526.client import Client
from alibabacloud_ecs20140526.models import DescribeImagesRequest
from alibabacloud_tea_openapi.models import Config

async def main():
    config = Config(
        access_key_id=os.environ.get('ALIBABA_CLOUD_ACCESS_KEY_ID'),
        access_key_secret=os.environ.get('ALIBABA_CLOUD_ACCESS_KEY_SECRET'),
        endpoint='ecs-cn-hangzhou.aliyuncs.com',
    )
    client = Client(config)
    request = DescribeImagesRequest(region_id='cn-hangzhou')
    response = await client.describe_images_async(request)
    return response

asyncio.run(main())

版本历史

共 1 个版本

  • v0.0.1-beta 当前
    2026-05-07 13:21 安全 安全

安全检测

腾讯云安全 (Keen)

安全,无风险
查看报告

腾讯云安全 (Sanbu)

安全,无风险
查看报告

🔗 相关推荐

dev-programming

Github

steipete
使用 `gh` CLI 与 GitHub 交互,通过 `gh issue`、`gh pr`、`gh run` 和 `gh api` 管理议题、PR、CI 运行及高级查询。
★ 681 📥 329,552
dev-programming

CodeConductor.ai

larsonreever
AI驱动平台,提供快速全栈开发、智能体、工作流自动化及低代码AI集成的可扩展产品创建。
★ 76 📥 182,493
dev-programming

YouTube

byungkyu
使用托管OAuth集成YouTube Data API,支持搜索视频、管理播放列表、获取频道数据及评论互动,适用于用户需要时使用此技能。
★ 142 📥 41,904