【问题标题】:How to connect to Azure Table/Cosmos storage emulator如何连接到 Azure Table/Cosmos 存储模拟器
【发布时间】:2017-07-21 18:36:41
【问题描述】:

这里的文档 (https://docs.microsoft.com/en-us/azure/storage/storage-use-emulator) 说端点应该是这种格式来访问模拟表存储:

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

但是,我在哪里可以从模拟器中获得 &lt;account-name&gt;&lt;resource-path&gt; 项目?

有人知道连接到模拟器的工作演示吗?我似乎找到的唯一一个是用于连接到 Azure。

【问题讨论】:

    标签: c# azure azure-storage-emulator


    【解决方案1】:

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

    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 DB 模拟器的凭据。它们是:

    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==");
    

    【讨论】:

    • 我已经安装了模拟器并运行了示例代码,但它在 table.CreateIfNotExists(); 行失败,出现异常“Microsoft.WindowsAzure.Storage.StorageException : '无法连接到远程服务器' - SocketException: 无法建立连接,因为目标机器主动拒绝了它”
    • 您似乎没有启动 Azure Storage Emulator。我们还可以从您提到的document 找到如何启动它
    • 它已启动。我可以看到它运行的本地网站。
    • 我假设您启动 Cosmos Db emulator 而不是 Azure storage emulator
    • 是的,我正在运行 Cosmos 模拟器。我刚刚意识到我使用的是 Install-Package WindowsAzure.Storage 而不是 Install-Package WindowsAzure.Storage-PremiumTable -Version 0.1.0-preview -Pre 所以它连接到 Azure但是一旦我将连接字符串更改为“UseDevelopmentStorage=true;”,仍然会失败
    猜你喜欢
    • 1970-01-01
    • 2019-02-24
    • 2018-06-14
    • 2020-02-29
    • 1970-01-01
    • 1970-01-01
    • 2022-11-28
    • 2020-10-14
    • 2018-02-08
    相关资源
    最近更新 更多