Configure environment variables, install package dependencies, and use the C# management SDK with CommonRequest. Sample code is provided.
Configure environment variables
Configure the ALIBABA_CLOUD_ACCESS_KEY_ID and ALIBABA_CLOUD_ACCESS_KEY_SECRET environment variables.
The AccessKey pair of an Alibaba Cloud account can be used to access all API operations. We recommend that you use a Resource Access Management (RAM) user to call API operations or perform routine O&M. For information about how to use a RAM user, see Create a RAM user.
For information about how to create an AccessKey pair, see Create an AccessKey pair.
If you use the AccessKey pair of a RAM user, make sure that the required permissions are granted to the AliyunServiceRoleForOpenSearch role by using your Alibaba Cloud account. For more information, see AliyunServiceRoleForOpenSearch and Access authorization rules.
We recommend that you do not include your AccessKey pair in materials that are easily accessible to others, such as the project code. Otherwise, your AccessKey pair may be leaked and resources in your account become insecure.
Linux and macOS
Run the following commands. Replace
<access_key_id>and<access_key_secret>with the AccessKey ID and AccessKey secret of the RAM user that you use.export ALIBABA_CLOUD_ACCESS_KEY_ID=<access_key_id> export ALIBABA_CLOUD_ACCESS_KEY_SECRET=<access_key_secret>Windows
Create an environment variable file, add the ALIBABA_CLOUD_ACCESS_KEY_ID and ALIBABA_CLOUD_ACCESS_KEY_SECRET environment variables to the file, and then set the environment variables to your AccessKey ID and AccessKey secret.
Restart Windows for the AccessKey pair to take effect.
Install package dependencies
Package dependency URL: https://www.nuget.org/packages
dotnet add package aliyun-net-sdk-core
Sample code
Sample code for the C# management SDK:
using System;
using System.Collections.Generic;
using System.Text;
using Aliyun.Acs.Core;
using Aliyun.Acs.Core.Profile;
using Aliyun.Acs.Core.Exceptions;
using Aliyun.Acs.Core.Http;
using Aliyun.Acs.Core.Auth;
namespace CommonRequestDemo
{
class Program
{
static void Main(string[] args)
{
// User identity information.
// Read the AccessKey ID and AccessKey secret from environment variables.
// Before you run the sample code, you must configure environment variables. For more information, see the "Configure environment variables" section in this topic.
Environment.GetEnvironmentVariable("ALIBABA_CLOUD_ACCESS_KEY_ID"),
Environment.GetEnvironmentVariable("ALIBABA_CLOUD_ACCESS_KEY_SECRET"));
/* use STS Token
AlibabaCloudCredentialsProvider provider = new StsCredentialProvider("<your-access-key-id>", "<your-access-key-secret>", "<your-sts-token>");
*/
IClientProfile profile = DefaultProfile.GetProfile("cn-qingdao");
DefaultAcsClient client = new DefaultAcsClient(profile, provider);
CommonRequest request = new CommonRequest();
request.Method = MethodType.POST;
request.Domain = "opensearch.cn-qingdao.aliyuncs.com";
request.Version = "2017-12-25";
request.UriPattern = "/v4/openapi/intervention-dictionaries/huan/entries/actions/bulk";
// request.Protocol = ProtocolType.HTTP;
request.AddHeadParameters("Content-Type", "application/json");
String requestBody = "[{\"word\":\"asdf\",\"cmd\":\"add\",\"tokens\":[{\"weight\":7,\"token\":\"asdf\"}]}]";
request.SetContent(Encoding.UTF8.GetBytes(requestBody), "utf-8", FormatType.JSON);
try {
CommonResponse response = client.GetCommonResponse(request);
Console.WriteLine(response.Data);
}
catch (ServerException e)
{
Console.WriteLine(e);
}
catch (ClientException e)
{
Console.WriteLine(e);
}
}
}
}
This sample code is a temporary solution that may become unavailable after the official SDK is released. Use it for testing only. Adjust request.UriPattern and String requestBody to call different management APIs.