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
}
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")
Use VPC endpoints when running inside Alibaba Cloud VPC:
config.Endpoint = tea.String("ecs-vpc.cn-hangzhou.aliyuncs.com")
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")
| Component | Description |
|---|---|
| ----------- | ------------- |
Core SDK (darabonba-openapi) | Generic calls, no product dependency |
Product SDK (e.g., ecs-20140526) | Typed client, models, convenience methods |
Enable debug mode via environment variable to log all requests:
export DEBUG=tea
共 1 个版本