文档

快速入门

更新时间:

本节介绍如何快速使用OOS C# SDK完成常见操作,如创建模板、启动执行、查询执行等。

创建模板

以下代码用于创建模板:

using System;
using System.Collections.Generic;
using Aliyun.Acs.Core;
using Aliyun.Acs.Core.Exceptions;
using Aliyun.Acs.Core.Profile;
using Aliyun.Acs.Core.Auth;
using Aliyun.Acs.oos.Model.V20190601;

namespace AlibabaCloud.SDK.Sample
{
    class Program
    {
        static void Main(string[] args)
        {		
          	/*
            阿里云账号AccessKey拥有所有API的访问权限,建议您使用RAM用户进行API访问或日常运维。
            强烈建议不要把AccessKey ID和AccessKey Secret保存到工程代码里,否则可能导致AccessKey泄露,威胁您账号下所有资源的安全。
            本示例使用了阿里云Credentials工具托管AccessKey,来实现API访问的身份验证。
            具体配置操作(或者配置环境变量),请参见https://help.aliyun.com/document_detail/378661.html。
            */
            // Please ensure that the environment variables ALIBABA_CLOUD_ACCESS_KEY_ID and ALIBABA_CLOUD_ACCESS_KEY_SECRET are set.
        AlibabaCloudCredentialsProvider provider = new AccessKeyCredentialProvider(
            Environment.GetEnvironmentVariable("ALIBABA_CLOUD_ACCESS_KEY_ID"), 
            Environment.GetEnvironmentVariable("ALIBABA_CLOUD_ACCESS_KEY_SECRET"));
            /* use STS Token
            AlibabaCloudCredentialsProvider provider = new StsCredentialProvider(
                Environment.GetEnvironmentVariable("ALIBABA_CLOUD_ACCESS_KEY_ID"), 
                Environment.GetEnvironmentVariable("ALIBABA_CLOUD_ACCESS_KEY_SECRET"), 
                Environment.GetEnvironmentVariable("ALIBABA_CLOUD_SECURITY_TOKEN"));
            */
            IClientProfile profile = DefaultProfile.GetProfile("cn-hangzhou");
            DefaultAcsClient client = new DefaultAcsClient(profile, provider);

            
            var request = new CreateTemplateRequest();
            request.TemplateName = "MyTemplate";
            request.Content = "{\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 {
                var response = client.GetAcsResponse(request);
                Console.WriteLine(System.Text.Encoding.Default.GetString(response.HttpResponse.Content));
            }
            catch (ServerException e)
            {
                Console.WriteLine(e);
            }
            catch (ClientException e)
            {
                Console.WriteLine(e);
            }
        }
    }
}

启动执行

以下代码用于启动执行:

using System;
using System.Collections.Generic;
using Aliyun.Acs.Core;
using Aliyun.Acs.Core.Exceptions;
using Aliyun.Acs.Core.Profile;
using Aliyun.Acs.Core.Auth;
using Aliyun.Acs.oos.Model.V20190601;

namespace AlibabaCloud.SDK.Sample
{
    class Program
    {
        static void Main(string[] args)
        {
          	/*
            阿里云账号AccessKey拥有所有API的访问权限,建议您使用RAM用户进行API访问或日常运维。
            强烈建议不要把AccessKey ID和AccessKey Secret保存到工程代码里,否则可能导致AccessKey泄露,威胁您账号下所有资源的安全。
            本示例使用了阿里云Credentials工具托管AccessKey,来实现API访问的身份验证。
            具体配置操作(或者配置环境变量),请参见https://help.aliyun.com/document_detail/378661.html。
            */
            // Please ensure that the environment variables ALIBABA_CLOUD_ACCESS_KEY_ID and ALIBABA_CLOUD_ACCESS_KEY_SECRET are set.
        AlibabaCloudCredentialsProvider provider = new AccessKeyCredentialProvider(
            Environment.GetEnvironmentVariable("ALIBABA_CLOUD_ACCESS_KEY_ID"), 
            Environment.GetEnvironmentVariable("ALIBABA_CLOUD_ACCESS_KEY_SECRET"));
            /* use STS Token
            AlibabaCloudCredentialsProvider provider = new StsCredentialProvider(
                Environment.GetEnvironmentVariable("ALIBABA_CLOUD_ACCESS_KEY_ID"), 
                Environment.GetEnvironmentVariable("ALIBABA_CLOUD_ACCESS_KEY_SECRET"), 
                Environment.GetEnvironmentVariable("ALIBABA_CLOUD_SECURITY_TOKEN"));
            */
            IClientProfile profile = DefaultProfile.GetProfile("cn-hangzhou");
            DefaultAcsClient client = new DefaultAcsClient(profile, provider);

            
            var request = new StartExecutionRequest();
            request.TemplateName = "MyTemplate";

            try {
                var response = client.GetAcsResponse(request);
                Console.WriteLine(System.Text.Encoding.Default.GetString(response.HttpResponse.Content));
            }
            catch (ServerException e)
            {
                Console.WriteLine(e);
            }
            catch (ClientException e)
            {
                Console.WriteLine(e);
            }
        }
    }
}

查询执行

以下代码用于查询执行:

using System;
using System.Collections.Generic;
using Aliyun.Acs.Core;
using Aliyun.Acs.Core.Exceptions;
using Aliyun.Acs.Core.Profile;
using Aliyun.Acs.Core.Auth;
using Aliyun.Acs.oos.Model.V20190601;

namespace AlibabaCloud.SDK.Sample
{
    class Program
    {
        static void Main(string[] args)
        {	
          	/*
            阿里云账号AccessKey拥有所有API的访问权限,建议您使用RAM用户进行API访问或日常运维。
            强烈建议不要把AccessKey ID和AccessKey Secret保存到工程代码里,否则可能导致AccessKey泄露,威胁您账号下所有资源的安全。
            本示例使用了阿里云Credentials工具托管AccessKey,来实现API访问的身份验证。
            具体配置操作(或者配置环境变量),请参见https://help.aliyun.com/document_detail/378661.html。
            */
            // Please ensure that the environment variables ALIBABA_CLOUD_ACCESS_KEY_ID and ALIBABA_CLOUD_ACCESS_KEY_SECRET are set.
        AlibabaCloudCredentialsProvider provider = new AccessKeyCredentialProvider(
            Environment.GetEnvironmentVariable("ALIBABA_CLOUD_ACCESS_KEY_ID"), 
            Environment.GetEnvironmentVariable("ALIBABA_CLOUD_ACCESS_KEY_SECRET"));
            /* use STS Token
            AlibabaCloudCredentialsProvider provider = new StsCredentialProvider(
                Environment.GetEnvironmentVariable("ALIBABA_CLOUD_ACCESS_KEY_ID"), 
                Environment.GetEnvironmentVariable("ALIBABA_CLOUD_ACCESS_KEY_SECRET"), 
                Environment.GetEnvironmentVariable("ALIBABA_CLOUD_SECURITY_TOKEN"));
            */
            IClientProfile profile = DefaultProfile.GetProfile("cn-hangzhou");
            DefaultAcsClient client = new DefaultAcsClient(profile, provider);

            
            var request = new ListExecutionsRequest();
            request.ExecutionId = "<ExecutionId>";

            try {
                var response = client.GetAcsResponse(request);
                Console.WriteLine(System.Text.Encoding.Default.GetString(response.HttpResponse.Content));
            }
            catch (ServerException e)
            {
                Console.WriteLine(e);
            }
            catch (ClientException e)
            {
                Console.WriteLine(e);
            }
        }
    }
}
  • 本页导读 (0)
文档反馈