The Azure request table with the shared signal does not return, but it works with the connection string

Spend many hours trying to figure it out.

I have a table and you want to create read-only SAS and give client components read access. But it never works out.

If you just use the connection string and connect the table like this:

CloudStorageAccount storageAccount = CloudStorageAccount.Parse(SliStorageConnection); // Create the table client. CloudTableClient tableClient = storageAccount.CreateCloudTableClient(); var table = tableClient.GetTableReference(GlobalFilterTable); TableOperation tableOperation = TableOperation.Retrieve<TableEntity>(FilterTablePartition, "filter1"); TableResult tableResult = table.Execute(tableOperation); 

It works great. But with SAS, as shown below, always returns 501 Not Implemented

  var policy = new SharedAccessTablePolicy { SharedAccessExpiryTime = DateTime.Now.AddMinutes(30), Permissions = SharedAccessTablePermissions.Query }; string sas = table.GetSharedAccessSignature( policy, null, FilterTablePartition, String.Empty, FilterTablePartition, String.Empty); Uri tableSasUri = new Uri(table.Uri, sas); AccessTable(tableSasUri.AbsoluteUri.ToString()); private static void AccessTable(string tableSas) { string filterTableBaseUrl = tableSas.Substring(0, tableSas.IndexOf('?')); var filterTableSasCredentials = new StorageCredentials(tableSas.Substring(filterTableBaseUrl.Length)); CloudTableClient tableClient = new CloudTableClient(new Uri(filterTableBaseUrl), filterTableSasCredentials); var _manifestFilterCloudTable = tableClient.GetTableReference(GlobalFilterTable); TableOperation tableOperation = TableOperation.Retrieve<TableEntity>(FilterTablePartition, "filter1"); TableResult tableResult = _manifestFilterCloudTable.Execute(tableOperation); } 

We tried different methods, gave less than 1 hour, a named or anonymous policy identifier, use only a signature ("sig") to create StorageCredentials . All failed with various errors. Mostly 501 Not Implemented , sometimes resource not found , sometimes 403 Forbidden .

Could not find useful information on the Internet. I am using the Microsoft.WindowsAzure.Storage version 3.1 SDK.

Any help is appreciated

+5
source share
1 answer

I believe the problem is the following two lines of code:

 CloudTableClient tableClient = new CloudTableClient(new Uri(filterTableBaseUrl), filterTableSasCredentials); var _manifestFilterCloudTable = tableClient.GetTableReference(GlobalFilterTable); 

Basically what happens is that the table name is repeated twice in the URL of the table in manifestFilterCloudTable . When you create a CloudTableClient , the URI should not include the table name. It should only be like https://[youraccountname].table.core.windows.net .

Please use the following code in the AccessTable method:

  string filterTableBaseUrl = tableSas.Substring(0, tableSas.IndexOf('?')); var filterTableSasCredentials = new StorageCredentials(tableSas.Substring(filterTableBaseUrl.Length)); filterTableBaseUrl = filterTableBaseUrl.Substring(0, filterTableBaseUrl.LastIndexOf("/")); tableClient = new CloudTableClient(new Uri(filterTableBaseUrl), filterTableSasCredentials); var _manifestFilterCloudTable = tableClient.GetTableReference("Address"); TableOperation tableOperation = TableOperation.Retrieve<TableEntity>(FilterTablePartition, "filter1"); TableResult tableResult = _manifestFilterCloudTable.Execute(tableOperation); 

Another thing I noticed for SharedAccessPolicy is you are using DateTime.Now . Depending on the time zone in which the code is executed, you may encounter 403 errors, since the date / time in Azure is in UTC. Use DateTime.UtcNow instead.

+4
source

Source: https://habr.com/ru/post/1212335/


All Articles