.NET SDK Usage Example
This section describes how to use the Huawei Cloud .NET SDK by calling the CreateInstanceNamespace API of SWR.
Prerequisites
- You have obtained the AK and SK corresponding to your Huawei Cloud account. For details, see Creating Access Keys.
- .NET Framework 4.7.2 or a later version, or .NET 8.0 or a later version has been installed in the environment where the SDK runs.
Installing the SDK
You can install the Huawei Cloud .NET SDK using the following methods. .NET SDK 3.1.213 is used as an example. You can obtain the latest SDK version from SDK Center.
- Use .NET CLI to install the SDK.
dotnet add package HuaweiCloud.SDK.Core --version 3.1.213 dotnet add package HuaweiCloud.SDK.Swr --version 3.1.213
- Use Package Manager to install the SDK.
Install-Package HuaweiCloud.SDK.Core -Version 3.1.213 Install-Package HuaweiCloud.SDK.Swr -Version 3.1.213
Using the SDK
The following is a complete example demonstrating how to use the Huawei Cloud .NET SDK to call the CreateInstanceNamespace API.
- To obtain the SDK example code for other APIs, see Obtaining SDK Sample Code.
- This example provides only the basic API parameter settings. For more advanced settings, see the User Manual of the SDK.
- There will be huge security risks if the AK and SK used for authentication are written into the code. You are advised to encrypt the AK and SK and store them in a configuration file or environment variables, and decrypt them when needed to ensure security. In this example, the AK and SK are stored as the environment variables CLOUD_SDK_AK and CLOUD_SDK_SK. For details, see Environment Variables.
using System; using System.Collections.Generic; using HuaweiCloud.SDK.Core; using HuaweiCloud.SDK.Core.Auth; using HuaweiCloud.SDK.Swr; using HuaweiCloud.SDK.Swr.V2; using HuaweiCloud.SDK.Swr.V2.Model; namespace CreateInstanceNamespaceSolution { class Program { static async Task Main(string[] args) { // The AK and SK used for authentication are hard-coded or stored in plaintext, which has great security risks. It is recommended that the AK and SK be stored in ciphertext in configuration files or environment variables and decrypted during use to ensure security. // In this example, AK and SK are stored in environment variables for authentication. Before running this example, set environment variables CLOUD_SDK_AK and CLOUD_SDK_SK in the local environment string ak = Environment.GetEnvironmentVariable("CLOUD_SDK_AK"); string sk = Environment.GetEnvironmentVariable("CLOUD_SDK_SK"); var config = HttpConfig.GetDefaultConfig(); config.IgnoreSslVerification = true; var auth = new BasicCredentials(ak, sk); var client = SwrAsyncClient.NewBuilder() .WithCredential(auth) .WithRegion(SwrRegion.ValueOf("cn-north-4")) .WithHttpConfig(config) .Build(); var req = new CreateInstanceNamespaceRequest { InstanceId = "instance_id" }; NamespaceMetadata metadatabody = new NamespaceMetadata() { Public = NamespaceMetadata.PublicEnum.FromValue("true") }; req.Body = new CreateInstanceNamespaceRequestBody() { Metadata = metadatabody, NamespaceName = "test" }; try { var resp = await client.CreateInstanceNamespace(req); var respStatusCode = resp.HttpStatusCode; Console.WriteLine(respStatusCode); } catch (RequestTimeoutException requestTimeoutException) { Console.WriteLine(requestTimeoutException.ErrorMessage); } catch (ServiceResponseException clientRequestException) { Console.WriteLine(clientRequestException.HttpStatusCode); Console.WriteLine(clientRequestException.RequestId); Console.WriteLine(clientRequestException.ErrorCode); Console.WriteLine(clientRequestException.ErrorMsg); } catch (ConnectionException connectionException) { Console.WriteLine(connectionException.ErrorMessage); } } } }
What is your overall rating for this page?
Thank you very much for your feedback. We will continue working to improve the documentation.See the reply and handling status in My Cloud VOC.
For any further questions, feel free to contact us through the chatbot.
Chatbot