Reputation: 503
How do I make a connection string to a cloud storage account so I can access tables, blobs, and queues? Sample code appreciated.
Upvotes: 35
Views: 132619
Reputation: 70184
Microsoft has a nice guide called Connect an app to Azure Storage that goes through everything you need.
Description:
Create a simple application and add configuration, client library references, and code to connect it to Azure Storage.
https://learn.microsoft.com/en-us/learn/modules/connect-an-app-to-azure-storage/
Step 7 there is Connect to your Azure storage account
Connection strings example:
DefaultEndpointsProtocol=https;AccountName={your-storage};
AccountKey={your-access-key};
EndpointSuffix=core.windows.net
The REST endpoint is a combination of your storage account name, the data type, and a known domain. For example:
Data type | Example endpoint |
---|---|
Blobs | https://[name].blob.core.windows.net/ |
Queues | https://[name].queue.core.windows.net/ |
Table | https://[name].table.core.windows.net/ |
Files | https://[name].file.core.windows.net/ |
To get a connection string I usually follow @user3459730 example and copy it from Azure Portal. Go to Storage account -> Access keys, click on Show keys and copy the Connection string.
Upvotes: 15
Reputation: 379
If you look in the Azure portal under the storage account in question, and look in the "Access Keys" item in the left-side nav, there it shows you the two provided keys and the entire connection string needed to access the storage account.
Upvotes: 26
Reputation: 599
Reference: Azure Documentation
Connection string to an Azure storage account:
DefaultEndpointsProtocol=[http|https];AccountName=myAccountName;AccountKey=myAccountKey
example:
DefaultEndpointsProtocol=https;AccountName=storagesample;AccountKey=<account-key>
Connection string to the storage emulator:
config.xml
<appSettings>
<add key="StorageConnectionString" value="UseDevelopmentStorage=true" />
</appSettings>
DefaultEndpointsProtocol=http;AccountName=testacc1;
AccountKey=1gy3lpE7Du1j5ljKiupgKzywSw2isjsdfdsfsdfsdsgfsgfdgfdgfd/YThisv/OVVLfIOv9kQ==;
BlobEndpoint=http://127.0.0.1:8440/testacc1;
TableEndpoint=http://127.0.0.1:8440/testacc1;
QueueEndpoint=http://127.0.0.1:8440/testacc1;
Ex:
<connectionStrings>
<add name="AzureStorageAccount" connectionString="DefaultEndpointsProtocol=https;AccountName=testdata;AccountKey=1gy3lpE7Du1j5ljKiupgKzywSw2isjsdfdsfsdfsdsgfsgfdgfdgfd/YThisv/OVVLfIOv9kQ==;"/>
</connectionStrings>
But sometimes it won't work and will through the error
An unhandled exception of type 'System.FormatException' occurred in Microsoft.WindowsAzure.Storage.dll
Additional information: No valid combination of account information found.
then please try with below code: tested and working 100%
var accountName = "test2rdsfdg462";
var keyValue = "1gy3lpE7Du1j5ljKiupgKzywSfsdfdsfsdfsdfsdfsdfsdqGxd7/YThisv/OVVLfIOv9kQ==";
var useHttps = true;
var connValid = true;
var storageCredentials = new StorageCredentials(accountName, keyValue);
var storageAccount = new CloudStorageAccount(storageCredentials, useHttps);
var conString = storageAccount.ToString(connValid);
CloudStorageAccount sa = CloudStorageAccount.Parse(connString);
Upvotes: 17
Reputation: 815
Notating this because it's a top Google hit and the information is no longer current.
You can configure CloudStorageAccount
via a connection string passed to FromConfigurationSetting()
.
You build a configuration string per below: https://learn.microsoft.com/en-gb/azure/storage/common/storage-configure-connection-string
There's also a helper in the IDE if you right click on the Role.
Upvotes: 16