因为需要买这个,手抢累,手撸了段代码。
这程序本身意义不大,不过可以做为阿里SDK的范本,比如我还需要写一个流量到了自动关机的脚本。
package main
import (
"fmt"
"os"
"time"
openapi "github.com/alibabacloud-go/darabonba-openapi/v2/client"
swas_open20200601 "github.com/alibabacloud-go/swas-open-20200601/client"
"github.com/alibabacloud-go/tea/tea"
)
func CreateClient(Key, Secret string) (_result *swas_open20200601.Client, _err error) {
config := &openapi.Config{
AccessKeyId: tea.String(Key),
AccessKeySecret: tea.String(Secret),
}
config.Endpoint = tea.String("swas.cn-hongkong.aliyuncs.com")
_result = &swas_open20200601.Client{}
_result, _err = swas_open20200601.NewClient(config)
return _result, _err
}
func RealBuy(Key, Secret string) error {
client, err := CreateClient(Key, Secret)
if err != nil {
return err
}
request := &swas_open20200601.CreateInstancesRequest{
RegionId: tea.String("cn-hongkong"), //香港
ImageId: tea.String("8b798eb927684a08b26bb95da94f5812"), //轻量24
PlanId: tea.String("swas.s2.c2m1s40b30t1.un"), //debian 11
Period: tea.Int32(1),
}
_, err = client.CreateInstances(request)
if err != nil {
return err
}
return nil
}
func main() {
location, err := time.LoadLocation("Asia/Shanghai")
if err != nil {
fmt.Println("Error loading location:", err)
return
}
args := os.Args
if len(args) < 3 {
fmt.Printf("Usage: %s <AccessKeyId> <AccessKeySecret>\n", args[0])
return
}
fmt.Println("Waiting for 00:00:00 to start...")
now := time.Now().In(location)
tomorrowMidnight := now.Truncate(24 * time.Hour).Add(24 * time.Hour)
time.Sleep(time.Until(tomorrowMidnight))
for i := 0; i < 10; i++ {
err := RealBuy(args[1], args[2])
if err != nil {
fmt.Printf("%d - Error: %s\n", i, err)
} else {
fmt.Printf("%d - Success! Please check it in your aliyun console.\n", i)
break
}
}
fmt.Println("Done")
}