.NET SDK

更新时间:
复制 MD 格式

Use Tablestore SDK for .NET to manage data in wide column tables.

Quick start

Get started with Tablestore SDK for .NET. Set up your environment, install the SDK, and initialize the client.

Prerequisites

The SDK is built on .NET Standard 2.0 and supports the following platforms and runtimes:

Platform

Runtime

Windows

  • .NET Core 2.0 or later, or .NET Framework 4.0 or later

  • Visual Studio 2010 or later

Linux and macOS

  • .NET Core 2.0 or later

Install the SDK

Install Tablestore SDK for .NET from NuGet or from source.

NuGet

Install with the .NET CLI:

dotnet add package Aliyun.TableStore.SDK

Alternatively, install from the NuGet Package Manager in Visual Studio:

  1. Open or create a project in Visual Studio, and then choose Tools > NuGet Package Manager > Manage NuGet Packages for Solution.

    Note

    If NuGet is not installed in Visual Studio, download and install it from NuGet.

  2. Search for aliyun.tablestore and select Aliyun.TableStore.SDK from the results.

  3. Select the latest version and click Install. The SDK is automatically added to your project.

Source code

Import the SDK source code into your project.

  1. Clone the SDK repository from GitHub:

    git clone https://github.com/aliyun/aliyun-tablestore-csharp-sdk.git
    Note

    If Git is not installed, download and install it from Git.

  2. In Visual Studio, right-click Solution and select Add > Existing Project.

  3. In the dialog box, select the aliyun-tablestore-sdk.csproj file and click Open.

  4. Right-click Project and select References > Add Reference. In the dialog box, switch to the Projects tab and select aliyun-tablestore-sdk.

  5. Click OK.

Configure access credentials

Create an AccessKey for your Alibaba Cloud account or a RAM user. Then, configure the AccessKey in the environment variables as shown below. Configuring the AccessKey in environment variables improves security because it prevents you from hard-coding sensitive information in your code.

After the configuration is complete, you must restart or refresh your development environment. This includes your IDE, command-line interface, other desktop applications, and background services. This ensures that the latest system environment variables are loaded. For more information about other types of access credentials, see Configure access credentials.

Linux

  1. Run the following commands to append the environment variable settings to the ~/.bashrc file.

    echo "export TABLESTORE_ACCESS_KEY_ID='YOUR_ACCESS_KEY_ID'" >> ~/.bashrc
    echo "export TABLESTORE_ACCESS_KEY_SECRET='YOUR_ACCESS_KEY_SECRET'" >> ~/.bashrc
  2. Run the following command to apply the changes.

    source ~/.bashrc
  3. Run the following commands to verify that the environment variables are effective.

    echo $TABLESTORE_ACCESS_KEY_ID
    echo $TABLESTORE_ACCESS_KEY_SECRET

macOS

  1. Run the following command in the terminal to view your default shell type.

    echo $SHELL
  2. Perform the following operations based on your default shell type.

    Zsh
    1. Run the following commands to append the environment variable settings to the ~/.zshrc file.

      echo "export TABLESTORE_ACCESS_KEY_ID='YOUR_ACCESS_KEY_ID'" >> ~/.zshrc
      echo "export TABLESTORE_ACCESS_KEY_SECRET='YOUR_ACCESS_KEY_SECRET'" >> ~/.zshrc
    2. Run the following command to apply the changes.

      source ~/.zshrc
    3. Run the following commands to verify that the environment variables are effective.

      echo $TABLESTORE_ACCESS_KEY_ID
      echo $TABLESTORE_ACCESS_KEY_SECRET
    Bash
    1. Run the following commands to append the environment variable settings to the ~/.bash_profile file.

      echo "export TABLESTORE_ACCESS_KEY_ID='YOUR_ACCESS_KEY_ID'" >> ~/.bash_profile
      echo "export TABLESTORE_ACCESS_KEY_SECRET='YOUR_ACCESS_KEY_SECRET'" >> ~/.bash_profile
    2. Run the following command to apply the changes.

      source ~/.bash_profile
    3. Run the following commands to verify that the environment variables are effective.

      echo $TABLESTORE_ACCESS_KEY_ID
      echo $TABLESTORE_ACCESS_KEY_SECRET

Windows

CMD
  1. Run the following commands in Command Prompt (CMD) to set the environment variables.

    setx TABLESTORE_ACCESS_KEY_ID "YOUR_ACCESS_KEY_ID"
    setx TABLESTORE_ACCESS_KEY_SECRET "YOUR_ACCESS_KEY_SECRET"
  2. Restart CMD and run the following commands to verify that the environment variables are effective.

    echo %TABLESTORE_ACCESS_KEY_ID%
    echo %TABLESTORE_ACCESS_KEY_SECRET%
PowerShell
  1. Run the following commands in PowerShell.

    [Environment]::SetEnvironmentVariable("TABLESTORE_ACCESS_KEY_ID", "YOUR_ACCESS_KEY_ID", [EnvironmentVariableTarget]::User)
    [Environment]::SetEnvironmentVariable("TABLESTORE_ACCESS_KEY_SECRET", "YOUR_ACCESS_KEY_SECRET", [EnvironmentVariableTarget]::User)
  2. Run the following commands to verify that the environment variables are effective.

    [Environment]::GetEnvironmentVariable("TABLESTORE_ACCESS_KEY_ID", [EnvironmentVariableTarget]::User)
    [Environment]::GetEnvironmentVariable("TABLESTORE_ACCESS_KEY_SECRET", [EnvironmentVariableTarget]::User)

Initialize the client

Tablestore SDK for .NET is thread-safe. When you use multiple threads, share a single OTSClient instance. After you initialize the client, list all tables in the instance to verify the connection.

Important

Public network access is disabled by default for new instances. To access instance resources over the public network, enable public access in Network Management.

using System;
using Aliyun.OTS;
using Aliyun.OTS.Request;
using Aliyun.OTS.Response;

namespace Aliyun.OTS.Samples
{
    public class Sample
    {
        public static void InitializeClient()
        {
            // Replace yourEndpoint with the instance endpoint
            string endpoint = "yourEndpoint";
            // Replace yourInstanceName with the instance name
            string instanceName = "yourInstanceName";
            // Obtain the AccessKey ID and AccessKey Secret from environment variables
            string accessKeyId = Environment.GetEnvironmentVariable("TABLESTORE_ACCESS_KEY_ID");
            string accessKeySecret = Environment.GetEnvironmentVariable("TABLESTORE_ACCESS_KEY_SECRET");
            OTSClientConfig config = new OTSClientConfig(endpoint, accessKeyId, accessKeySecret, instanceName)
            {
                OTSDebugLogHandler = null,
                OTSErrorLogHandler = null
            };

            try
            {
                // Initialize the Tablestore client
                OTSClient client = new OTSClient(config);
                // List and print all tables in the instance
                ListTableResponse response = client.ListTable(new ListTableRequest());
                foreach (var tableName in response.TableNames)
                {
                    Console.WriteLine(tableName);
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("List table failed, exception:{0}", ex.Message);
            }
        }
    }
}

Version compatibility

The latest SDK version is 6.x.x. Compatibility with earlier versions:

Version

Compatibility

Description

5.x.x

Compatible

N/A

4.x.x

Compatible

N/A

3.x.x

Compatible

N/A

2.x.x

Incompatible

The following interfaces are removed: Condition.IGNORE, Condition.EXPECT_EXIST, and Condition.EXPECT_NOT_EXIST. The DLL file is renamed from Aliyun.dll to Aliyun.TableStore.dll.

For detailed version history, see Version history of Tablestore SDK for .NET.

FAQ

Common issues with Tablestore SDK for .NET:

References

For information about error handling in Tablestore, see Error handling.