步骤 1:创建 DynamoDB 客户端 - Amazon DynamoDB
AWS 文档中描述的 AWS 服务或功能可能因区域而异。要查看适用于中国区域的差异,请参阅中国的 AWS 服务入门

本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。

步骤 1:创建 DynamoDB 客户端

Microsoft .NET 和 教程DynamoDB的第一步是创建一个客户端,该客户端向您提供对 API 的访问权限。Amazon DynamoDBDynamoDB_intro 中的 Main 函数通过调用在 01_CreateClient.cs 文件中实现的 createClient 函数来完成此操作。

using System; using System.Net; using System.Net.NetworkInformation; using Amazon.DynamoDBv2; namespace DynamoDB_intro { public static partial class DdbIntro { /*----------------------------------------------------------------------------------- * If you are creating a client for the Amazon DynamoDB service, make sure your credentials * are set up first, as explained in: * https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/SettingUp.DynamoWebService.html, * * If you are creating a client for DynamoDBLocal (for testing purposes), * DynamoDB-Local should be started first. For most simple testing, you can keep * data in memory only, without writing anything to disk. To do this, use the * following command line: * * java -Djava.library.path=./DynamoDBLocal_lib -jar DynamoDBLocal.jar -inMemory * * For information about DynamoDBLocal, see: * https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/DynamoDBLocal.html. *-----------------------------------------------------------------------------------*/ // So we know whether local DynamoDB is running private static readonly string Ip = "localhost"; private static readonly int Port = 8000; private static readonly string EndpointUrl = "http://" + Ip + ":" + Port; private static bool IsPortInUse() { bool isAvailable = true; // Evaluate current system TCP connections. This is the same information provided // by the netstat command line application, just in .Net strongly-typed object // form. We will look through the list, and if our port we would like to use // in our TcpClient is occupied, we will set isAvailable to false. IPGlobalProperties ipGlobalProperties = IPGlobalProperties.GetIPGlobalProperties(); IPEndPoint[] tcpConnInfoArray = ipGlobalProperties.GetActiveTcpListeners(); foreach (IPEndPoint endpoint in tcpConnInfoArray) { if (endpoint.Port == Port) { isAvailable = false; break; } } return isAvailable; } public static bool createClient(bool useDynamoDbLocal) { if (useDynamoDbLocal) { // First, check to see whether anyone is listening on the DynamoDB local port // (by default, this is port 8000, so if you are using a different port, modify this accordingly) var portUsed = IsPortInUse(); if (portUsed) { Console.WriteLine("The local version of DynamoDB is NOT running."); return (false); } // DynamoDB-Local is running, so create a client Console.WriteLine( " -- Setting up a DynamoDB-Local client (DynamoDB Local seems to be running)" ); AmazonDynamoDBConfig ddbConfig = new AmazonDynamoDBConfig(); ddbConfig.ServiceURL = EndpointUrl; try { Client = new AmazonDynamoDBClient(ddbConfig); } catch( Exception ex ) { Console.WriteLine( " FAILED to create a DynamoDBLocal client; " + ex.Message ); return false; } } else { Client = new AmazonDynamoDBClient(); } return true; } } }

MainuseDynamoDBLocal 参数设置为 true 时调用此函数。 因此,DynamoDB 的本地测试版本必须已在您的计算机上使用默认端口 (8000) 运行,否则调用将失败。如果尚未安装此版本,请参阅在您的计算机上运行 DynamoDB

useDynamoDBLocal 参数设置为 将为 false 服务本身而不是本地测试程序创建客户端。DynamoDB

下一步

步骤 2:使用低级 API 创建 DynamoDB 表