Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to connect to Azure Table/Cosmos storage emulator

The docs here (https://docs.microsoft.com/en-us/azure/storage/storage-use-emulator) say the endpoint should be this format to access emulated table storage:

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

However, where do I get the <account-name> and <resource-path> items from within the emulator?

Anyone know of a working demo for connecting to the emulator? The only one I seem to find is for connecting to Azure.

like image 256
deejbee Avatar asked Jul 21 '17 18:07

deejbee


People also ask

How do I connect my cosmos emulator?

Begin typing Azure Cosmos DB Emulator, and select the emulator from the list of applications. When the emulator has started, you'll see an icon in the Windows taskbar notification area. It automatically opens the Azure Cosmos DB data explorer in your browser at this URL https://localhost:8081/_explorer/index.html URL.

How do I connect to Azure Cosmos database?

Access Azure Cosmos DB Explorer Sign in to Azure portal. From All resources, find and navigate to your Azure Cosmos DB account, select Keys, and copy the Primary Connection String. Go to https://cosmos.azure.com/, paste the connection string and select Connect.

How do I connect to Azure Table storage?

Creating a connectionProvide Azure Storage account name (or table endpoint) and Access Key to access your Azure Table Storage. Provide Azure Storage account name (or table endpoint) and Access Key to access your Azure Table Storage. Use Azure Active Directory to access your Azure Table storage.


1 Answers

If we want to connect to the storage emulator, the code demo is the same as Azure Storage. The difference is the storage emulator uses a well-known account name and key.

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

To target the storage emulator, you can use a shortcut that maps to the well-known account name and key.

In that case, your connection string setting is:

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

We could get the code demo from Azure's official documentation.

// 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();

Regarding how to use Cosmos emulator, we may get the answer from Use the Azure Cosmos DB Emulator for local development and testing.

We need to install the Cosmos emulator locally.

his account and key are the only credentials permitted for use with the Azure Cosmos DB Emulator. They are:

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

Demo code:

// Connect to the Azure Cosmos DB Emulator running locally
DocumentClient client = new DocumentClient(
    new Uri("https://localhost:8081"), 
    "C2y6yDjf5/R+ob0N8A7Cgv30VRDJIWEHLM+4QDU5DE2nQ9nDuVTqobD4b8mGGyPMbIZnqyMsEcaGQy67XIw/Jw==");
like image 117
Tom Sun - MSFT Avatar answered Sep 27 '22 22:09

Tom Sun - MSFT