← 返回
未分类 Key 中文

Alibabacloud Sdk Client Initialization For Golang

Initialize and manage Alibaba Cloud SDK clients in Go. Covers sync.Once singleton, goroutine safety, endpoint vs region configuration, VPC endpoints, and deb...
初始化并管理阿里云SDK Go 客户端,涵盖 sync.Once 单例、goroutine 安全、endpoint 与 region 配置、VPC 端点及调试。
sdk-team sdk-team 来源
未分类 clawhub v0.0.1-beta 1 版本 99703.3 Key: 需要
★ 0
Stars
📥 336
下载
💾 0
安装
1
版本
#latest

概述

Client Initialization Best Practices (Go)

Core Rules

  • Client is goroutine-safe — safe to share across goroutines without additional synchronization.
  • 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"
    "sync"

    openapi "github.com/alibabacloud-go/darabonba-openapi/v2/client"
    ecs "github.com/alibabacloud-go/ecs-20140526/v3/client"
    "github.com/alibabacloud-go/tea/tea"
)

var (
    ecsClient     *ecs.Client
    ecsClientOnce sync.Once
)

func GetEcsClient() *ecs.Client {
    ecsClientOnce.Do(func() {
        config := &openapi.Config{
            AccessKeyId:     tea.String(os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_ID")),
            AccessKeySecret: tea.String(os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET")),
            Endpoint:        tea.String("ecs.cn-hangzhou.aliyuncs.com"),
        }
        var err error
        ecsClient, err = ecs.NewClient(config)
        if err != nil {
            panic(err)
        }
    })
    return ecsClient
}

Endpoint Configuration

Priority: explicit Endpoint > region-based resolution via RegionId.

// Preferred: explicit endpoint
config.Endpoint = tea.String("ecs.cn-hangzhou.aliyuncs.com")

// Alternative: SDK resolves endpoint from region
config.RegionId = tea.String("cn-hangzhou")

VPC Endpoints

Use VPC endpoints when running inside Alibaba Cloud VPC:

config.Endpoint = tea.String("ecs-vpc.cn-hangzhou.aliyuncs.com")

File Upload APIs (Advance)

Set both RegionId and Endpoint to the same region for file upload APIs:

config.RegionId = tea.String("cn-shanghai")
config.Endpoint = tea.String("objectdet.cn-shanghai.aliyuncs.com")
// VPC file upload authorization:
client.OpenPlatformEndpoint = tea.String("openplatform-vpc.cn-shanghai.aliyuncs.com")
client.EndpointType = tea.String("internal")

SDK Components

ComponentDescription
------------------------
Core SDK (darabonba-openapi)Generic calls, no product dependency
Product SDK (e.g., ecs-20140526)Typed client, models, convenience methods

Debugging

Enable debug mode via environment variable to log all requests:

export DEBUG=tea

版本历史

共 1 个版本

  • v0.0.1-beta 当前
    2026-05-07 09:35 安全 安全

安全检测

腾讯云安全 (Keen)

安全,无风险
查看报告

腾讯云安全 (Sanbu)

安全,无风险
查看报告

🔗 相关推荐

Alibabacloud Find Skills

sdk-team
用于搜索、发现、浏览或查找阿里云(Alibaba Cloud)代理技能。触发词包括“查找X技能”“搜索阿里云…”等。
★ 0 📥 1,011

Alibabacloud Rds Copilot

sdk-team
阿里云RDS Copilot智能运维助手技能。用于RDS相关智能问答、SQL优化、实例运维和故障排查。
★ 1 📥 794

Alibabacloud Pds Intelligent Workspace

sdk-team
阿里云 PDS(智能云盘/网盘)文件操作技能。支持:文件搜索、文件上传、文件下载、文档/音视频分析、打包下载、图像编辑(缩放、裁剪、旋转、分割、移除、水印等)、以图搜图、挂载网盘、文件分享链接管理。 当用户提到 PDS、网盘、云盘、个人空间
★ 0 📥 593