具有共享访问签名的查询Azure表未实现,但连接字符串有效

时间:2015-01-31 17:13:57

标签: azure azure-table-storage

花费很多时间尝试解决这个问题。

我有一个表,想要创建一个只读SAS并给客户端组件读取访问权限。但永远不会成功。

如果只是直接使用连接字符串和挂钩表:

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

工作正常。但是使用下面的SAS,总是返回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);
}

尝试了不同的方式,给出不到1小时的时间,命名或匿名策略标识符,仅使用签名(“sig”)来创建StorageCredentials。所有失败都有不同的错误。主要是501 not implemented,有时resource not found,有时403 Forbidden

无法在线查找有用信息。我正在使用Microsoft.WindowsAzure.Storage版本3.1 SDK。

非常感谢任何帮助

1 个答案:

答案 0 :(得分:4)

我认为问题出在以下两行代码中:

CloudTableClient tableClient = new CloudTableClient(new Uri(filterTableBaseUrl), filterTableSasCredentials);

    var _manifestFilterCloudTable = tableClient.GetTableReference(GlobalFilterTable);

基本上发生的事情是表名在manifestFilterCloudTable的表格网址中重复两次。在创建CloudTableClient时,URI不应包含表名。它应该只是https://[youraccountname].table.core.windows.net

请在AccessTable方法中使用以下代码:

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

我注意到另一件事,对于SharedAccessPolicy,你正在使用DateTime.Now。根据执行代码的时区,您可能会遇到403错误,因为Azure中的日期/时间是UTC。请改用DateTime.UtcNow