【阿里云】阿里云产品go sdk封装调用demo

2 篇文章 0 订阅

文章目录

代码

产品sdk封装函数:

package apsara

import (
	"github.com/aliyun/alibaba-cloud-sdk-go/services/ecs"
	"github.com/aliyun/alibaba-cloud-sdk-go/services/nas"
	"github.com/aliyun/alibaba-cloud-sdk-go/services/rds"
	"github.com/aliyun/alibaba-cloud-sdk-go/services/slb"
	"github.com/aliyun/alibaba-cloud-sdk-go/services/vpc"
)

type Client struct {
	Vpc *vpc.Client
	Ecs *ecs.Client
	Slb *slb.Client
	Rds *rds.Client
	Nas *nas.Client
}

func NewClient() (*Client, error) {

	zone := "zone"
	ak := "ak"
	sk := "sk"

	vpcClient, err := vpc.NewClientWithAccessKey(zone, ak, sk)
	if err != nil {
		return nil, err
	}
	vpcClient.Domain = ""
	vpcClient.SetHTTPSInsecure(true)

	ecsClient, err := ecs.NewClientWithAccessKey(zone, ak, sk)
	if err != nil {
		return nil, err
	}
	ecsClient.Domain = ""
	ecsClient.SetHTTPSInsecure(true)

	slbClient, err := slb.NewClientWithAccessKey(zone, ak, sk)
	if err != nil {
		return nil, err
	}
	slbClient.Domain = ""
	slbClient.SetHTTPSInsecure(true)

	rdsClient, err := rds.NewClientWithAccessKey(zone, ak, sk)
	if err != nil {
		return nil, err
	}
	rdsClient.Domain = ""
	rdsClient.SetHTTPSInsecure(true)

	nasClient, err := nas.NewClientWithAccessKey(zone, ak, sk)
	if err != nil {
		return nil, err
	}
	nasClient.Domain = ""
	nasClient.SetHTTPSInsecure(true)

	c := &Client{
		Vpc: vpcClient,
		Ecs: ecsClient,
		Slb: slbClient,
		Rds: rdsClient,
		Nas: nasClient,
	}

	return c, nil
}

main函数,以调DescribeLoadBalancersRequest接口为例:

func main() {
	client, err := apsara.NewClient()
	if err != nil {
		fmt.Println("err:", err)
	}
	
	// slb
	request := slb.CreateDescribeLoadBalancersRequest()
	request.Scheme = "HTTPS"
	request.PageNumber = requests.NewInteger(1)
	request.PageSize = requests.NewInteger(50)
	headers := map[string]string{
		"x-acs-resourcegroupid": "5",    // 指定资源集
	}
	request.Headers = headers
	response, err := client.Slb.DescribeLoadBalancers(request)
	if err != nil {
		fmt.Println("err1:", err)
	}
	fmt.Println("res:---", response)
}

Reference

阿里云官方文档:https://help.aliyun.com/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

微雨停了

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值