10.4新建分支
parent
d1440fb706
commit
5bb0737c82
|
@ -115,6 +115,11 @@
|
|||
<artifactId>tea-util</artifactId>
|
||||
<version>0.2.21</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.aliyun</groupId>
|
||||
<artifactId>darabonba-env</artifactId>
|
||||
<version>0.1.1</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
|
|
|
@ -0,0 +1,17 @@
|
|||
package com.muyu.cloud.vehicle.gateway.aliyun.domain;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
@Data
|
||||
@AllArgsConstructor
|
||||
@NoArgsConstructor
|
||||
public class Instance {
|
||||
|
||||
private String instanceId;
|
||||
|
||||
private String ipAddress;
|
||||
|
||||
private String status;
|
||||
}
|
|
@ -1,23 +1,36 @@
|
|||
// This file is auto-generated, don't edit it. Thanks.
|
||||
package com.muyu.cloud.vehicle.gateway.test.example;
|
||||
package com.muyu.cloud.vehicle.gateway.aliyun.ecs;
|
||||
|
||||
import com.aliyun.ecs20140526.Client;
|
||||
import com.aliyun.ecs20140526.models.DeleteInstancesRequest;
|
||||
import com.aliyun.ecs20140526.models.DescribeInstancesRequest;
|
||||
import com.aliyun.ecs20140526.models.DescribeInstancesResponse;
|
||||
import com.aliyun.ecs20140526.models.DescribeInstancesResponseBody;
|
||||
import com.aliyun.tea.*;
|
||||
import com.aliyun.teaopenapi.models.Config;
|
||||
import com.aliyun.teautil.Common;
|
||||
import com.aliyun.teautil.models.RuntimeOptions;
|
||||
import lombok.extern.log4j.Log4j2;
|
||||
import org.springframework.beans.factory.DisposableBean;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.context.ApplicationListener;
|
||||
import org.springframework.context.event.ContextClosedEvent;
|
||||
import org.springframework.data.redis.core.StringRedisTemplate;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 删除实例
|
||||
*/
|
||||
@Log4j2
|
||||
@Component
|
||||
public class DelInstance implements DisposableBean{
|
||||
public class DelInstance implements ApplicationListener<ContextClosedEvent> {
|
||||
|
||||
@Autowired
|
||||
private StringRedisTemplate redisTemplate;
|
||||
|
||||
|
||||
/**
|
||||
* <b>description</b> :
|
||||
|
@ -26,46 +39,48 @@ public class DelInstance implements DisposableBean{
|
|||
*
|
||||
* @throws Exception
|
||||
*/
|
||||
public static com.aliyun.ecs20140526.Client createClient() throws Exception {
|
||||
public static Client createClient() throws Exception {
|
||||
// 工程代码泄露可能会导致 AccessKey 泄露,并威胁账号下所有资源的安全性。以下代码示例仅供参考。
|
||||
// 建议使用更安全的 STS 方式,更多鉴权访问方式请参见:https://help.aliyun.com/document_detail/378657.html。
|
||||
com.aliyun.teaopenapi.models.Config config = new com.aliyun.teaopenapi.models.Config()
|
||||
Config config = new Config()
|
||||
// 必填,请确保代码运行环境设置了环境变量 ALIBABA_CLOUD_ACCESS_KEY_ID。
|
||||
.setAccessKeyId("LTAI5t7Fnx2QLTYLSu9357wP")
|
||||
// 必填,请确保代码运行环境设置了环境变量 ALIBABA_CLOUD_ACCESS_KEY_SECRET。
|
||||
.setAccessKeySecret("3LOnydNZ25ytsTGczuSygElx0HJ6nN");
|
||||
// Endpoint 请参考 https://api.aliyun.com/product/Ecs
|
||||
config.endpoint = "ecs.cn-shanghai.aliyuncs.com";
|
||||
return new com.aliyun.ecs20140526.Client(config);
|
||||
return new Client(config);
|
||||
}
|
||||
|
||||
|
||||
|
||||
public static void delInstance() throws Exception{
|
||||
public void delInstance() throws Exception{
|
||||
//创建ECS客户端对象,用于后续调用ECS相关API
|
||||
com.aliyun.ecs20140526.Client client = DelInstance.createClient();
|
||||
Client client = DelInstance.createClient();
|
||||
|
||||
DescribeInstancesRequest describeInstancesRequest = new DescribeInstancesRequest()
|
||||
.setRegionId("cn-shanghai");
|
||||
List<String> list = redisTemplate.opsForList().range("instanceIds",0,-1);
|
||||
|
||||
//创建运行时选择对象,用于配置运行时的选项参数
|
||||
RuntimeOptions runtimeOptions = new RuntimeOptions();
|
||||
// DescribeInstancesRequest describeInstancesRequest = new DescribeInstancesRequest()
|
||||
// .setRegionId("cn-shanghai");
|
||||
|
||||
//获取实例列表
|
||||
DescribeInstancesResponse describeInstancesResponse = client.describeInstancesWithOptions(describeInstancesRequest, runtimeOptions);
|
||||
|
||||
//提取实例ID集合
|
||||
ArrayList<String> list = new ArrayList<>();
|
||||
DescribeInstancesResponseBody body = describeInstancesResponse.getBody();
|
||||
|
||||
for (DescribeInstancesResponseBody.DescribeInstancesResponseBodyInstancesInstance instance : body.getInstances().getInstance()) {
|
||||
list.add(instance.getInstanceId());
|
||||
}
|
||||
|
||||
System.out.println("Instance IDs"+list);
|
||||
// //创建运行时选择对象,用于配置运行时的选项参数
|
||||
// RuntimeOptions runtimeOptions = new RuntimeOptions();
|
||||
//
|
||||
// //获取实例列表
|
||||
// DescribeInstancesResponse describeInstancesResponse = client.describeInstancesWithOptions(describeInstancesRequest, runtimeOptions);
|
||||
//
|
||||
// //提取实例ID集合
|
||||
// ArrayList<String> list = new ArrayList<>();
|
||||
// DescribeInstancesResponseBody body = describeInstancesResponse.getBody();
|
||||
//
|
||||
// for (DescribeInstancesResponseBody.DescribeInstancesResponseBodyInstancesInstance instance : body.getInstances().getInstance()) {
|
||||
// list.add(instance.getInstanceId());
|
||||
// }
|
||||
//
|
||||
// System.out.println("Instance IDs"+list);
|
||||
|
||||
// 创建删除实例请求对象,并设置请求参数
|
||||
com.aliyun.ecs20140526.models.DeleteInstancesRequest deleteInstancesRequest = new com.aliyun.ecs20140526.models.DeleteInstancesRequest()
|
||||
DeleteInstancesRequest deleteInstancesRequest = new DeleteInstancesRequest()
|
||||
// 设置地域ID,指定要删除的实例所属的地域ID。
|
||||
.setRegionId("cn-shanghai")
|
||||
//设置DryRun为True,用于验证请求是否可以成功,但不实际执行删除操作
|
||||
|
@ -77,32 +92,44 @@ public class DelInstance implements DisposableBean{
|
|||
// 设置实例ID列表,使用参数名称 instanceId,参数类型为数组。
|
||||
.setInstanceId(list);
|
||||
// 创建运行时选项对象,用于配置运行时的选项参数
|
||||
com.aliyun.teautil.models.RuntimeOptions runtime = new com.aliyun.teautil.models.RuntimeOptions();
|
||||
RuntimeOptions runtime = new RuntimeOptions();
|
||||
try{
|
||||
//复制代码运行请自行打印 API 的返回值
|
||||
client.deleteInstancesWithOptions(deleteInstancesRequest,runtime);
|
||||
} catch (TeaException error) {
|
||||
// 此处仅做打印展示,请谨慎对待异常处理,在工程项目中切勿直接忽略异常。
|
||||
// 错误 message
|
||||
System.out.println(error.getMessage());
|
||||
log.error(error.getMessage());
|
||||
// 诊断地址
|
||||
System.out.println(error.getData().get("Recommend"));
|
||||
com.aliyun.teautil.Common.assertAsString(error.message);
|
||||
log.error(error.getData().get("Recommend"));
|
||||
Common.assertAsString(error.message);
|
||||
} catch (Exception _error){
|
||||
TeaException error = new TeaException(_error.getMessage(), _error);
|
||||
// 此处仅做打印展示,请谨慎对待异常处理,在工程项目中切勿直接忽略异常。
|
||||
// 错误 message
|
||||
System.out.println(error.getMessage());
|
||||
log.error(error.getMessage());
|
||||
// 诊断地址
|
||||
System.out.println(error.getData().get("Recommend"));
|
||||
com.aliyun.teautil.Common.assertAsString(error.message);
|
||||
log.error(error.getData().get("Recommend"));
|
||||
Common.assertAsString(error.message);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// @Override
|
||||
// public void destroy() throws Exception {
|
||||
// log.info("===============>开始执行删除实例方法");
|
||||
// delInstance();
|
||||
// }
|
||||
|
||||
@Override
|
||||
public void destroy() throws Exception {
|
||||
log.info("===============>开始执行删除实例方法");
|
||||
public void onApplicationEvent(ContextClosedEvent event) {
|
||||
try{
|
||||
log.info("=======>删除实例");
|
||||
delInstance();
|
||||
redisTemplate.delete("instanceIds");
|
||||
redisTemplate.delete("instanceList");
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,59 @@
|
|||
package com.muyu.cloud.vehicle.gateway.aliyun.ecs;
|
||||
|
||||
import com.aliyun.ecs20140526.Client;
|
||||
import com.aliyun.ecs20140526.models.DescribeInstancesRequest;
|
||||
import com.aliyun.ecs20140526.models.DescribeInstancesResponse;
|
||||
import com.aliyun.teaopenapi.models.Config;
|
||||
import com.aliyun.darabonba.env.EnvClient;
|
||||
import com.aliyun.teautil.Common;
|
||||
import com.aliyun.teautil.models.RuntimeOptions;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 查询一台或多台实例的详细信息
|
||||
*/
|
||||
public class QueryInstance {
|
||||
|
||||
/**
|
||||
* <b>description</b> :
|
||||
* <p>使用AK&SK初始化账号Client</p>
|
||||
* @return Client
|
||||
*
|
||||
* @throws Exception
|
||||
*/
|
||||
public static Client createClient(String alibabaCloudAccessKeyId, String alibabaCloudAccessKeySecret) throws Exception{
|
||||
Config config = new Config()
|
||||
.setAccessKeyId("LTAI5t7Fnx2QLTYLSu9357wP")
|
||||
.setAccessKeySecret("3LOnydNZ25ytsTGczuSygElx0HJ6nN");
|
||||
config.endpoint = "ecs-cn-hangzhou.aliyuncs.com";
|
||||
return new Client(config);
|
||||
}
|
||||
|
||||
public static Client createClientWithSTS() throws Exception {
|
||||
Config config = new Config()
|
||||
.setAccessKeyId("LTAI5t7Fnx2QLTYLSu9357wP")
|
||||
.setAccessKeySecret("3LOnydNZ25ytsTGczuSygElx0HJ6nN")
|
||||
.setSecurityToken("token")
|
||||
.setType("sts");
|
||||
config.endpoint = "ecs-cn-hangzhou.aliyuncs.com";
|
||||
return new Client(config);
|
||||
}
|
||||
|
||||
public static void main(String[] args_) throws Exception{
|
||||
List<String> list = Arrays.asList(args_);
|
||||
|
||||
Client client = QueryInstance.createClient(EnvClient.getEnv("LTAI5t7Fnx2QLTYLSu9357wP"), EnvClient.getEnv("3LOnydNZ25ytsTGczuSygElx0HJ6nN"));
|
||||
DescribeInstancesRequest describeInstancesRequest = new DescribeInstancesRequest()
|
||||
.setRegionId("cn-shanghai")
|
||||
.setInternetChargeType("PayByTraffic")
|
||||
.setInstanceChargeType("PostPaid");
|
||||
RuntimeOptions runtime = new RuntimeOptions();
|
||||
DescribeInstancesResponse resp = client.describeInstancesWithOptions(describeInstancesRequest, runtime);
|
||||
com.aliyun.teaconsole.Client.log(Common.toJSONString(resp));
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
|
@ -1,12 +1,25 @@
|
|||
package com.muyu.cloud.vehicle.gateway.test.example;
|
||||
package com.muyu.cloud.vehicle.gateway.aliyun.ecs;
|
||||
|
||||
|
||||
import com.aliyun.ecs20140526.Client;
|
||||
import com.aliyun.ecs20140526.models.*;
|
||||
import com.aliyun.tea.*;
|
||||
import com.aliyun.teaopenapi.models.Config;
|
||||
import com.aliyun.teautil.models.RuntimeOptions;
|
||||
import com.muyu.cloud.vehicle.gateway.aliyun.domain.Instance;
|
||||
import lombok.extern.log4j.Log4j2;
|
||||
import org.apache.logging.log4j.CloseableThreadContext;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.ApplicationArguments;
|
||||
import org.springframework.boot.ApplicationRunner;
|
||||
import org.springframework.data.redis.core.ListOperations;
|
||||
import org.springframework.data.redis.core.StringRedisTemplate;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 批量创建ECS实例
|
||||
*/
|
||||
|
@ -14,6 +27,11 @@ import org.springframework.stereotype.Component;
|
|||
@Component
|
||||
public class Sample implements ApplicationRunner {
|
||||
|
||||
@Autowired
|
||||
private StringRedisTemplate redisTemplate;
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* <b>description</b> :
|
||||
* <p>使用AK&SK初始化账号Client</p>
|
||||
|
@ -21,44 +39,51 @@ public class Sample implements ApplicationRunner {
|
|||
*
|
||||
* @throws Exception
|
||||
*/
|
||||
public static com.aliyun.ecs20140526.Client createClient() throws Exception {
|
||||
public static Client createClient() throws Exception {
|
||||
// 工程代码泄露可能会导致 AccessKey 泄露,并威胁账号下所有资源的安全性。以下代码示例仅供参考。
|
||||
com.aliyun.teaopenapi.models.Config config = new com.aliyun.teaopenapi.models.Config()
|
||||
Config config = new Config()
|
||||
// 必填,请确保代码运行环境设置了环境变量 ALIBABA_CLOUD_ACCESS_KEY_ID。
|
||||
.setAccessKeyId("LTAI5t7Fnx2QLTYLSu9357wP")
|
||||
// 必填,请确保代码运行环境设置了环境变量 ALIBABA_CLOUD_ACCESS_KEY_SECRET。
|
||||
.setAccessKeySecret("3LOnydNZ25ytsTGczuSygElx0HJ6nN");
|
||||
// Endpoint 请参考 https://api.aliyun.com/product/Ecs
|
||||
config.endpoint = "ecs-cn-hangzhou.aliyuncs.com";
|
||||
return new com.aliyun.ecs20140526.Client(config);
|
||||
return new Client(config);
|
||||
}
|
||||
|
||||
public static void creatInstance() throws Exception {
|
||||
public void creatInstance() throws Exception {
|
||||
|
||||
// 将命令行参数转换为字符串列表
|
||||
java.util.List<String> args = java.util.Arrays.asList();
|
||||
List<String> args = Arrays.asList();
|
||||
// 创建阿里云ECS客户端
|
||||
com.aliyun.ecs20140526.Client client = Sample.createClient();
|
||||
Client client = Sample.createClient();
|
||||
// 配置系统盘参数
|
||||
com.aliyun.ecs20140526.models.RunInstancesRequest.RunInstancesRequestSystemDisk systemDisk = new com.aliyun.ecs20140526.models.RunInstancesRequest.RunInstancesRequestSystemDisk()
|
||||
RunInstancesRequest.RunInstancesRequestSystemDisk systemDisk = new RunInstancesRequest.RunInstancesRequestSystemDisk()
|
||||
.setSize("20")// 设置系统盘大小为40GB
|
||||
.setCategory("cloud_essd");// 设置系统盘类型为cloud_essd
|
||||
// 创建创建实例请求对象并设置参数
|
||||
com.aliyun.ecs20140526.models.RunInstancesRequest runInstancesRequest = new com.aliyun.ecs20140526.models.RunInstancesRequest()
|
||||
RunInstancesRequest runInstancesRequest = new RunInstancesRequest()
|
||||
.setRegionId("cn-shanghai") // 设置地域ID
|
||||
.setImageId("m-uf66taa8r57hvow8cxlj")// 设置镜像ID
|
||||
.setImageId("m-uf63dnbv4od71jlezdne")// 设置镜像ID
|
||||
.setInstanceType("ecs.e-c1m1.large")// 设置实例类型
|
||||
.setSecurityGroupId("sg-uf6hyictocodexptlgiv")// 设置安全组ID
|
||||
.setVSwitchId("vsw-uf6ags5luz17qd6ckn2tb")// 设置虚拟交换机ID
|
||||
.setInstanceName("launch-advisor-20240928")// 设置实例名称
|
||||
.setInstanceName("launch-advisor-20240929")// 设置实例名称
|
||||
.setInstanceChargeType("PostPaid")// 设置实例付费类型为后付费按量付费
|
||||
.setSystemDisk(systemDisk)// 设置系统盘配置
|
||||
.setHostName("root")// 设置主机名
|
||||
.setPassword("@ywt021900")// 设置实例密码
|
||||
.setAmount(2);// 设置创建实例的数量
|
||||
com.aliyun.teautil.models.RuntimeOptions runtime = new com.aliyun.teautil.models.RuntimeOptions();
|
||||
.setAmount(1)// 设置创建实例的数量
|
||||
.setInternetChargeType("PayByTraffic")// 设置互联网计费类型为按流量计费
|
||||
.setInternetMaxBandwidthOut(1);// 设置互联网最大出带宽为1 Mbps
|
||||
RuntimeOptions runtime = new RuntimeOptions();
|
||||
try {
|
||||
// 复制代码运行请自行打印 API 的返回值
|
||||
client.runInstancesWithOptions(runInstancesRequest, runtime);
|
||||
RunInstancesResponse runInstancesResponse = client.runInstancesWithOptions(runInstancesRequest, runtime);
|
||||
RunInstancesResponseBody body = runInstancesResponse.getBody();
|
||||
ArrayList<String> list = new ArrayList<>(body.getInstanceIdSets().getInstanceIdSet());
|
||||
log.info("实例ID:{}", list);
|
||||
|
||||
} catch (TeaException error) {
|
||||
// 此处仅做打印展示,请谨慎对待异常处理,在工程项目中切勿直接忽略异常。
|
||||
// 错误 message
|
||||
|
@ -77,6 +102,13 @@ public class Sample implements ApplicationRunner {
|
|||
}
|
||||
}
|
||||
|
||||
// private static DescribeInstancesResponse queryInstanceDetails(Client client) {
|
||||
//
|
||||
// //创建查询实例请求对象并设置参数
|
||||
// DescribeInstancesRequest describeInstancesRequest = new DescribeInstancesRequest();
|
||||
// return null;
|
||||
// }
|
||||
|
||||
@Override
|
||||
public void run(ApplicationArguments args) throws Exception {
|
||||
log.info("===============>开始批量创建实例");
|
|
@ -0,0 +1,56 @@
|
|||
package com.muyu.cloud.vehicle.gateway.aliyun.ecs;//// This file is auto-generated, don't edit it. Thanks.
|
||||
//package com.muyu.cloud.vehicle.gateway.aliyun.ecs;
|
||||
//
|
||||
//import com.aliyun.tea.*;
|
||||
//
|
||||
///**
|
||||
// * 启动一台实例
|
||||
// */
|
||||
//public class StartInstance {
|
||||
//
|
||||
// /**
|
||||
// * <b>description</b> :
|
||||
// * <p>使用AK&SK初始化账号Client</p>
|
||||
// * @return Client
|
||||
// *
|
||||
// * @throws Exception
|
||||
// */
|
||||
// public static com.aliyun.ecs20140526.Client createClient() throws Exception {
|
||||
// // 工程代码泄露可能会导致 AccessKey 泄露,并威胁账号下所有资源的安全性。以下代码示例仅供参考。
|
||||
// // 建议使用更安全的 STS 方式,更多鉴权访问方式请参见:https://help.aliyun.com/document_detail/378657.html。
|
||||
// com.aliyun.teaopenapi.models.Config config = new com.aliyun.teaopenapi.models.Config()
|
||||
// // 必填,请确保代码运行环境设置了环境变量 ALIBABA_CLOUD_ACCESS_KEY_ID。
|
||||
// .setAccessKeyId("LTAI5t7Fnx2QLTYLSu9357wP")
|
||||
// // 必填,请确保代码运行环境设置了环境变量 ALIBABA_CLOUD_ACCESS_KEY_SECRET。
|
||||
// .setAccessKeySecret("3LOnydNZ25ytsTGczuSygElx0HJ6nN");
|
||||
// // Endpoint 请参考 https://api.aliyun.com/product/Ecs
|
||||
// config.endpoint = "ecs.cn-shanghai.aliyuncs.com";
|
||||
// return new com.aliyun.ecs20140526.Client(config);
|
||||
// }
|
||||
//
|
||||
// public static void main(String[] args_) throws Exception {
|
||||
// java.util.List<String> args = java.util.Arrays.asList(args_);
|
||||
// com.aliyun.ecs20140526.Client client = StartInstance.createClient();
|
||||
// com.aliyun.ecs20140526.models.StartInstanceRequest startInstanceRequest = new com.aliyun.ecs20140526.models.StartInstanceRequest();
|
||||
// com.aliyun.teautil.models.RuntimeOptions runtime = new com.aliyun.teautil.models.RuntimeOptions();
|
||||
// try {
|
||||
// // 复制代码运行请自行打印 API 的返回值
|
||||
// client.startInstanceWithOptions(startInstanceRequest, runtime);
|
||||
// } catch (TeaException error) {
|
||||
// // 此处仅做打印展示,请谨慎对待异常处理,在工程项目中切勿直接忽略异常。
|
||||
// // 错误 message
|
||||
// System.out.println(error.getMessage());
|
||||
// // 诊断地址
|
||||
// System.out.println(error.getData().get("Recommend"));
|
||||
// com.aliyun.teautil.Common.assertAsString(error.message);
|
||||
// } catch (Exception _error) {
|
||||
// TeaException error = new TeaException(_error.getMessage(), _error);
|
||||
// // 此处仅做打印展示,请谨慎对待异常处理,在工程项目中切勿直接忽略异常。
|
||||
// // 错误 message
|
||||
// System.out.println(error.getMessage());
|
||||
// // 诊断地址
|
||||
// System.out.println(error.getData().get("Recommend"));
|
||||
// com.aliyun.teautil.Common.assertAsString(error.message);
|
||||
// }
|
||||
// }
|
||||
//}
|
|
@ -1,56 +0,0 @@
|
|||
// This file is auto-generated, don't edit it. Thanks.
|
||||
package com.muyu.cloud.vehicle.gateway.test.example;
|
||||
|
||||
import com.aliyun.tea.*;
|
||||
|
||||
/**
|
||||
* 启动一台实例
|
||||
*/
|
||||
public class StartInstance {
|
||||
|
||||
/**
|
||||
* <b>description</b> :
|
||||
* <p>使用AK&SK初始化账号Client</p>
|
||||
* @return Client
|
||||
*
|
||||
* @throws Exception
|
||||
*/
|
||||
public static com.aliyun.ecs20140526.Client createClient() throws Exception {
|
||||
// 工程代码泄露可能会导致 AccessKey 泄露,并威胁账号下所有资源的安全性。以下代码示例仅供参考。
|
||||
// 建议使用更安全的 STS 方式,更多鉴权访问方式请参见:https://help.aliyun.com/document_detail/378657.html。
|
||||
com.aliyun.teaopenapi.models.Config config = new com.aliyun.teaopenapi.models.Config()
|
||||
// 必填,请确保代码运行环境设置了环境变量 ALIBABA_CLOUD_ACCESS_KEY_ID。
|
||||
.setAccessKeyId("LTAI5t7Fnx2QLTYLSu9357wP")
|
||||
// 必填,请确保代码运行环境设置了环境变量 ALIBABA_CLOUD_ACCESS_KEY_SECRET。
|
||||
.setAccessKeySecret("3LOnydNZ25ytsTGczuSygElx0HJ6nN");
|
||||
// Endpoint 请参考 https://api.aliyun.com/product/Ecs
|
||||
config.endpoint = "ecs.cn-shanghai.aliyuncs.com";
|
||||
return new com.aliyun.ecs20140526.Client(config);
|
||||
}
|
||||
|
||||
public static void main(String[] args_) throws Exception {
|
||||
java.util.List<String> args = java.util.Arrays.asList(args_);
|
||||
com.aliyun.ecs20140526.Client client = StartInstance.createClient();
|
||||
com.aliyun.ecs20140526.models.StartInstanceRequest startInstanceRequest = new com.aliyun.ecs20140526.models.StartInstanceRequest();
|
||||
com.aliyun.teautil.models.RuntimeOptions runtime = new com.aliyun.teautil.models.RuntimeOptions();
|
||||
try {
|
||||
// 复制代码运行请自行打印 API 的返回值
|
||||
client.startInstanceWithOptions(startInstanceRequest, runtime);
|
||||
} catch (TeaException error) {
|
||||
// 此处仅做打印展示,请谨慎对待异常处理,在工程项目中切勿直接忽略异常。
|
||||
// 错误 message
|
||||
System.out.println(error.getMessage());
|
||||
// 诊断地址
|
||||
System.out.println(error.getData().get("Recommend"));
|
||||
com.aliyun.teautil.Common.assertAsString(error.message);
|
||||
} catch (Exception _error) {
|
||||
TeaException error = new TeaException(_error.getMessage(), _error);
|
||||
// 此处仅做打印展示,请谨慎对待异常处理,在工程项目中切勿直接忽略异常。
|
||||
// 错误 message
|
||||
System.out.println(error.getMessage());
|
||||
// 诊断地址
|
||||
System.out.println(error.getData().get("Recommend"));
|
||||
com.aliyun.teautil.Common.assertAsString(error.message);
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue