如何连接到Azure Table / Cosmos存储模拟器

时间:2017-07-21 18:36:42

标签: c# azure azure-storage-emulator

此处的文档(https://docs.microsoft.com/en-us/azure/storage/storage-use-emulator)表示端点应采用此格式来访问模拟表存储:

http://127.0.0.1:10002/<account-name>/<resource-path>

但是,我从哪里获取模拟器中的内容?

任何人都知道连接到模拟器的工作演示?我似乎唯一找到的是连接到Azure。

1 个答案:

答案 0 :(得分:4)

如果我们要连接到存储模拟器,则代码演示与Azure存储相同。不同之处在于存储模拟器使用众所周知的帐户名和密钥。

DefaultEndpointsProtocol=http;AccountName=devstoreaccount1;
AccountKey=Eby8vdM02xNOcqFlqUwJPLlmEtlCDXJ1OUzFT50uSRZ6IFsuFq2UVErCz4I6tq/K1SZFPTOtr/KBHBeksoGMGw==;
  

要定位存储模拟器,您可以使用映射到众所周知的帐户名称和密钥的快捷方式。

在这种情况下,您的连接字符串设置为:

<add key="StorageConnectionString" value="UseDevelopmentStorage=true;" />

我们可以从Azure的官方文档中获取代码演示。

// Retrieve the storage account from the connection string.
CloudStorageAccount storageAccount = CloudStorageAccount.Parse(
    CloudConfigurationManager.GetSetting("StorageConnectionString"));

// Create the table client.
CloudTableClient tableClient = storageAccount.CreateCloudTableClient();

// Retrieve a reference to the table.
CloudTable table = tableClient.GetTableReference("people");

// Create the table if it doesn't exist.
table.CreateIfNotExists();

关于如何使用Cosmos模拟器,我们可以从Use the Azure Cosmos DB Emulator for local development and testing得到答案。

我们需要在本地安装Cosmos emulator

  

他的帐户和密钥是允许与Azure Cosmos数据库模拟器一起使用的唯一凭据。他们是:

Account name: localhost:<port>
Account key: C2y6yDjf5/R+ob0N8A7Cgv30VRDJIWEHLM+4QDU5DE2nQ9nDuVTqobD4b8mGGyPMbIZnqyMsEcaGQy67XIw/Jw==

演示代码:

// Connect to the Azure Cosmos DB Emulator running locally
DocumentClient client = new DocumentClient(
    new Uri("https://localhost:8081"), 
    "C2y6yDjf5/R+ob0N8A7Cgv30VRDJIWEHLM+4QDU5DE2nQ9nDuVTqobD4b8mGGyPMbIZnqyMsEcaGQy67XIw/Jw==");
相关问题