Help Center/ Object Storage Service/ SDK Reference/ .NET/ Initialization/ Creating an Instance of ObsClient
Updated on 2024-05-08 GMT+08:00

Creating an Instance of ObsClient

If you have any questions during the development, post them on the Issues page of GitHub. For details about parameters and usage of each API, see the API Reference.

ObsClient functions as the .NET client for accessing OBS. It offers callers a series of APIs for interaction with OBS and is used for managing and operating resources, such as buckets and objects, stored in OBS. To use OBS .NET SDK to send a request to OBS, you need to initialize an instance of ObsClient and modify the default configurations in ObsConfig based on needs.

  • If you use the endpoint to create an instance of ObsClient, all parameters are in their default values and cannot be modified.
    • Using permanent AK (access key ID)/SK(secret access key)
    // Initialize configuration parameters.
    // Hard-coded or plaintext AK/SK are risky. For security purposes, encrypt your AK/SK and store them in the configuration file or environment variables. In this example, the AK/SK are stored in environment variables for identity authentication. Before running this example, configure environment variables AccessKeyID and SecretAccessKey.
    // Obtain an AK/SK pair on the management console. For details, see https://support.huaweicloud.com/intl/en-us/usermanual-ca/ca_01_0003.html.
    string accessKey= Environment.GetEnvironmentVariable("AccessKeyID", EnvironmentVariableTarget.Machine);
    string secretKey= Environment.GetEnvironmentVariable("SecretAccessKey", EnvironmentVariableTarget.Machine);
    // Create an instance of ObsClient.
    ObsClient client = new ObsClient(accessKey, secretKey, "https://your-endpoint");
    // Use the instance to access OBS.
    • Using temporary access credentials (AKs/SKs and security tokens)
    // Initialize configuration parameters.
    // Hard-coded or plaintext AK/SK are risky. For security purposes, encrypt your AK/SK and store them in the configuration file or environment variables. In this example, the AK/SK are stored in environment variables for identity authentication. Before running this example, configure environment variables AccessKeyID and SecretAccessKey.
    // Obtain an AK/SK pair on the management console. For details, see https://support.huaweicloud.com/intl/en-us/usermanual-ca/ca_01_0003.html.
    string accessKey= Environment.GetEnvironmentVariable("AccessKeyID", EnvironmentVariableTarget.Machine);
    string secretKey= Environment.GetEnvironmentVariable("SecretAccessKey", EnvironmentVariableTarget.Machine);
    string securityToken= "your_securityToken"
    // Create an instance of ObsClient.
    ObsClient client = new ObsClient(accessKey, secretKey,securityToken, "https://your-endpoint");
    // Use the instance to access OBS.
  • If you use the ObsConfig configuration class to create an instance of ObsClient, you can set any parameters as needed during the creation. After the instance has been created, the parameters cannot be modified. For parameter details, see Configuring an Instance of ObsClient
// Create an instance of ObsConfig.
ObsConfig config = new ObsConfig();
config.Endpoint = "https://your-endpoint";
// Hard-coded or plaintext AK/SK are risky. For security purposes, encrypt your AK/SK and store them in the configuration file or environment variables. In this example, the AK/SK are stored in environment variables for identity authentication. Before running this example, configure environment variables AccessKeyID and SecretAccessKey.
// Obtain an AK/SK pair on the management console. For details, see https://support.huaweicloud.com/intl/en-us/usermanual-ca/ca_01_0003.html.
string accessKey= Environment.GetEnvironmentVariable("AccessKeyID", EnvironmentVariableTarget.Machine);
string secretKey= Environment.GetEnvironmentVariable("SecretAccessKey", EnvironmentVariableTarget.Machine);
// Create an instance of ObsClient.
ObsClient client = new ObsClient(accessKey, secretKey, config);
// Use the instance to access OBS.
  • The project can contain one or more instances of ObsClient.
  • ObsClient is thread-safe and can be simultaneously used by multiple threads.