Amit
Amit

Reputation: 26266

How to verify if an azure storage account exists with CloudStorageAccount?

On my application I use multiple azure storage accounts and recently it so happened that few accounts were not available but the code to use those accounts were scattered across my application. Any method call on these accounts such as create table/query/blob, retrieve started throwing.

I was checking around to see if there is a way to check whether the account exists before calling a method on it. But I did not find any. We can provide IRequestOptions while calling method on storage client library, but I don't want to change my code in all the places. I just want to check whether the account exists, if not then don't call any of the methods on that account at all. Because calling each method on the account with a timeout and so on causes a 500

The remote server returned an error: (500) Internal Server Error.

which I don't want.

I took a look at CloudStorageAccount class on Microsoft.WindowsAzure.Storage which doesn't have any method to check if it is a valid account. It can so happen that the connection string might be wrong for an account and in that case also all methods start throwing 500.

Any pointers on this will help.

Upvotes: 1

Views: 2940

Answers (2)

Jan David Narkiewicz
Jan David Narkiewicz

Reputation: 314

If you know the Resource Group then simply follow this process to List Storage Accounts for Resource Group, https://msdn.microsoft.com/en-us/library/azure/mt163554.aspx

If you are unsure of the resource group then first list the resource groups, List all resource groups: https://msdn.microsoft.com/en-us/library/azure/dn790529.aspx?f=255&MSPPError=-2147217396 . Then list each storage account within each resource group found.

Upvotes: 1

Brian P
Brian P

Reputation: 1587

The easiest way to see if the account exists is to go ahead and run an operation on it within a try catch block. In your catch block, handle your account does not exist logic.

Side note: It sounds as if your account credentials are hard coded throughout your application, which is not actually the best practice. Future account updates will be easier if they are in a centralized config.

Upvotes: 1

Related Questions