简体   繁体   English

如何使用 CloudStorageAccount 验证 Azure 存储帐户是否存在?

[英]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.在我的应用程序中,我使用了多个 azure 存储帐户,最近碰巧很少有帐户不可用,但使用这些帐户的代码分散在我的应用程序中。 Any method call on these accounts such as create table/query/blob, retrieve started throwing.对这些帐户的任何方法调用,例如创建表/查询/blob、检索开始抛出。

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.我们可以在调用存储客户端库上的方法时提供IRequestOptions ,但我不想在所有地方更改我的代码。 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因为在超时等情况下调用帐户上的每个方法会导致 500

The remote server returned an error: (500) Internal Server Error.远程服务器返回错误:(500) 内部服务器错误。

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.我查看了 Microsoft.WindowsAzure.Storage 上的CloudStorageAccount类,它没有任何方法来检查它是否是有效帐户。 It can so happen that the connection string might be wrong for an account and in that case also all methods start throwing 500.可能会发生一个帐户的连接字符串可能是错误的,在这种情况下,所有方法也开始抛出 500。

Any pointers on this will help.任何有关此的指示都会有所帮助。

The easiest way to see if the account exists is to go ahead and run an operation on it within a try catch block.查看帐户是否存在的最简单方法是继续并在 try catch 块中对其运行操作。 In your catch block, handle your account does not exist logic.在您的 catch 块中,处理您的帐户不存在的逻辑。

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.如果它们在集中配置中,未来的帐户更新会更容易。

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如果您知道资源组,则只需按照此过程列出资源组的存储帐户, 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 .如果您不确定资源组,请先列出资源组,列出所有资源组: https://msdn.microsoft.com/en-us/library/azure/dn790529.aspx?f=255&MSPPError=-2147217396 Then list each storage account within each resource group found.然后列出找到的每个资源组中的每个存储帐户。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM