本節介紹如何快速使用OOS Java SDK完成常見操作,如創建模板、啟動執行、查詢執行等。
創建模板
以下代碼用于創建模板:
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.profile.DefaultProfile;
import com.google.gson.Gson;
import com.aliyuncs.oos.model.v20190601.*;
/*
pom.xml
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>aliyun-java-sdk-core</artifactId>
<version>4.6.0</version>
</dependency>
*/
public class CreateTemplate {
public static void main(String[] args) {
/**
阿里云賬號AccessKey擁有所有API的訪問權限,建議您使用RAM用戶進行API訪問或日常運維。
強烈建議不要把AccessKey ID和AccessKey Secret保存到工程代碼里,否則可能導致AccessKey泄露,威脅您賬號下所有資源的安全。
本示例使用了阿里云Credentials工具托管AccessKey,來實現API訪問的身份驗證。
具體配置操作(或者配置環境變量),請參見http://bestwisewords.com/document_detail/378657.html。
**/
// Please ensure that the environment variables ALIBABA_CLOUD_ACCESS_KEY_ID and ALIBABA_CLOUD_ACCESS_KEY_SECRET are set.
DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID"), System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET"));
/** use STS Token
DefaultProfile profile = DefaultProfile.getProfile(
"<your-region-id>", // The region ID
System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID"), // The AccessKey ID of the RAM account
System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET"), // The AccessKey Secret of the RAM account
System.getenv("ALIBABA_CLOUD_SECURITY_TOKEN")); // STS Token
**/
IAcsClient client = new DefaultAcsClient(profile);
CreateTemplateRequest request = new CreateTemplateRequest();
request.setTemplateName("MyTemplate");
request.setContent("{\n \\\"FormatVersion\\\": \\\"OOS-2019-06-01\\\",\n \\\"Description\\\": \\\"Describe instances of given status\\\",\n \\\"Parameters\\\": {\n \\\"Status\\\": {\n \\\"Type\\\": \\\"String\\\",\n \\\"Description\\\": \\\"(Required) The status of the Ecs instance.\\\"\n }\n },\n \\\"Tasks\\\": [\n {\n \\\"Properties\\\": {\n \\\"Parameters\\\": { \\\"Status\\\": \\\"{{ Status }}\\\" },\n \\\"API\\\": \\\"DescribeInstances\\\",\n \\\"Service\\\": \\\"ECS\\\"\n },\n \\\"Name\\\": \\\"describeInstances\\\",\n \\\"Action\\\": \\\"ACS::ExecuteAPI\\\"\n }\n ]\n}");
try {
CreateTemplateResponse response = client.getAcsResponse(request);
System.out.println(new Gson().toJson(response));
} catch (ServerException e) {
e.printStackTrace();
} catch (ClientException e) {
System.out.println("ErrCode:" + e.getErrCode());
System.out.println("ErrMsg:" + e.getErrMsg());
System.out.println("RequestId:" + e.getRequestId());
}
}
}
啟動執行
以下代碼用于啟動執行:
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.profile.DefaultProfile;
import com.google.gson.Gson;
import com.aliyuncs.oos.model.v20190601.*;
/*
pom.xml
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>aliyun-java-sdk-core</artifactId>
<version>4.6.0</version>
</dependency>
*/
public class StartExecution {
public static void main(String[] args) {
/**
阿里云賬號AccessKey擁有所有API的訪問權限,建議您使用RAM用戶進行API訪問或日常運維。
強烈建議不要把AccessKey ID和AccessKey Secret保存到工程代碼里,否則可能導致AccessKey泄露,威脅您賬號下所有資源的安全。
本示例使用了阿里云Credentials工具托管AccessKey,來實現API訪問的身份驗證。
具體配置操作(或者配置環境變量),請參見http://bestwisewords.com/document_detail/378657.html。
**/
// Please ensure that the environment variables ALIBABA_CLOUD_ACCESS_KEY_ID and ALIBABA_CLOUD_ACCESS_KEY_SECRET are set.
DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID"), System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET"));
/** use STS Token
DefaultProfile profile = DefaultProfile.getProfile(
"<your-region-id>", // The region ID
System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID"), // The AccessKey ID of the RAM account
System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET"), // The AccessKey Secret of the RAM account
System.getenv("ALIBABA_CLOUD_SECURITY_TOKEN")); // STS Token
**/
IAcsClient client = new DefaultAcsClient(profile);
StartExecutionRequest request = new StartExecutionRequest();
request.setTemplateName("MyTemplate");
request.setRegionId("cn-hangzhou");
try {
StartExecutionResponse response = client.getAcsResponse(request);
System.out.println(new Gson().toJson(response));
} catch (ServerException e) {
e.printStackTrace();
} catch (ClientException e) {
System.out.println("ErrCode:" + e.getErrCode());
System.out.println("ErrMsg:" + e.getErrMsg());
System.out.println("RequestId:" + e.getRequestId());
}
}
}
查詢執行
以下代碼用于查詢執行:
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.profile.DefaultProfile;
import com.google.gson.Gson;
import com.aliyuncs.oos.model.v20190601.*;
/*
pom.xml
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>aliyun-java-sdk-core</artifactId>
<version>4.6.0</version>
</dependency>
*/
public class ListExecutions {
public static void main(String[] args) {
/**
阿里云賬號AccessKey擁有所有API的訪問權限,建議您使用RAM用戶進行API訪問或日常運維。
強烈建議不要把AccessKey ID和AccessKey Secret保存到工程代碼里,否則可能導致AccessKey泄露,威脅您賬號下所有資源的安全。
本示例使用了阿里云Credentials工具托管AccessKey,來實現API訪問的身份驗證。
具體配置操作(或者配置環境變量),請參見http://bestwisewords.com/document_detail/378657.html。
**/
// Please ensure that the environment variables ALIBABA_CLOUD_ACCESS_KEY_ID and ALIBABA_CLOUD_ACCESS_KEY_SECRET are set.
DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID"), System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET"));
/** use STS Token
DefaultProfile profile = DefaultProfile.getProfile(
"<your-region-id>", // The region ID
System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID"), // The AccessKey ID of the RAM account
System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET"), // The AccessKey Secret of the RAM account
System.getenv("ALIBABA_CLOUD_SECURITY_TOKEN")); // STS Token
**/
IAcsClient client = new DefaultAcsClient(profile);
ListExecutionsRequest request = new ListExecutionsRequest();
request.setExecutionId("<ExecutionId>");
try {
ListExecutionsResponse response = client.getAcsResponse(request);
System.out.println(new Gson().toJson(response));
} catch (ServerException e) {
e.printStackTrace();
} catch (ClientException e) {
System.out.println("ErrCode:" + e.getErrCode());
System.out.println("ErrMsg:" + e.getErrMsg());
System.out.println("RequestId:" + e.getRequestId());
}
}
}
文檔內容是否對您有幫助?