文档

快速入门

更新时间:

本节介绍如何快速使用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访问的身份验证。
        具体配置操作(或者配置环境变量),请参见https://help.aliyun.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访问的身份验证。
        具体配置操作(或者配置环境变量),请参见https://help.aliyun.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访问的身份验证。
        具体配置操作(或者配置环境变量),请参见https://help.aliyun.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());
        }

    }
}