列出 Azure table 值

List Azure table value

我有一个问题,这可能很愚蠢,但我花了将近一周的时间。我想使用 http 触发器创建 Azure 函数,它将检查 URL 哪个请求在 Azure table 中,如果 url 存在,它将把它重定向到另一个 url ,这将是例如给定数据库记录中的 3 个值。重定向过程和记录匹配时从数据库中检索数据的过程我一直在工作,唯一的问题是如何从给定行中获取特定值。 如何获取属性的值?将 'data' 转换为列表无效。

using System.IO;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Extensions.Http;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using Microsoft.Azure.Cosmos.Table;
using System.Linq;
using static System.Environment;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Net;
using System.Collections.Generic;

namespace Company.Function
{
    public static class Redirect2
    {
        [FunctionName("Redirect2")]
        public static async Task<IActionResult> GetAll(
        [HttpTrigger(AuthorizationLevel.Function, "get", Route = "todo")] HttpRequest req,
        [Table("tablename", Connection = "AzureWebJobsStorage")] CloudTable cloudTable,
        ILogger log)
    {
        string OriginUrl = "test.domain.com";

        TableQuery<TodoTable> query = new TableQuery<TodoTable>().Where(
        TableQuery.CombineFilters(TableQuery.GenerateFilterCondition("PartitionKey", QueryComparisons.Equal, OriginUrl),
        TableOperators.Or, TableQuery.GenerateFilterCondition("RowKey", QueryComparisons.Equal, OriginUrl)));  

        var segment =  cloudTable.ExecuteQuery(query);
        var data = segment.Select(TodoExtensions.ToTodo);

        return new OkObjectResult(data);

    }
    }

}

public class TodoTable : TableEntity
{
    public string URL { get; set; }

    public string wwwURL { get; set; } 

    public string ClientURL { get; set; }
}

public class Todo
{
    public string URL { get; set; }

    public string wwwURL { get; set; } 

    public string ClientURL { get; set; }
}

public static class TodoExtensions
{
    public static TodoTable ToTable(this Todo todo)
    {
        return new TodoTable
        {
            PartitionKey = todo.URL,
            RowKey = todo.wwwURL,
            ClientURL = todo.URL
        };
    }

    public static Todo ToTodo(this TodoTable todoTable)
    {
        return new Todo
        {
            URL = todoTable.PartitionKey,
            wwwURL = todoTable.RowKey,
            ClientURL = todoTable.URL
        };
    }
}

你搞砸了分区

 TableQuery<TodoTable> query = new TableQuery<TodoTable>().Where(
    TableQuery.CombineFilters(TableQuery.GenerateFilterCondition("PartitionKey", QueryComparisons.Equal, OriginUrl),
    TableOperators.Or, TableQuery.GenerateFilterCondition("RowKey", QueryComparisons.Equal, OriginUrl)));  

所以在这里您尝试通过 rowKey 或 partitionKey 进行匹配,这将是昂贵的,因为它需要遍历 Table 存储中的所有数据。你真正想做的是始终查询单个分区。

所以我建议您将 PartitionKey 设置为“默认”。然后你总是查询单个分区。

如果我对你的理解是正确的,那么你应该这样做

public static async Task<IActionResult> GetAll(
        [HttpTrigger(AuthorizationLevel.Function, "get", Route = "todo")]
        HttpRequest req,
        [Table("tablename", Connection = "AzureWebJobsStorage")]
        CloudTable cloudTable,
        ILogger log)
    {
        string OriginUrl = req.Host.Value;

         TableQuery<TodoTable> query = new TableQuery<TodoTable>().Where(
            TableQuery.CombineFilters(
                TableQuery.GenerateFilterCondition(nameof(TableEntity.PartitionKey), QueryComparisons.Equal, OriginUrl),
                TableOperators.And,
                TableQuery.GenerateFilterCondition(nameof(TableEntity.RowKey), QueryComparisons.Equal, OriginUrl)))
            .Take(1);

        var segment = cloudTable.ExecuteQuery(query);
        var data = segment.Select(TodoExtensions.ToTodo).FirstOrDefault();

         return new OkObjectResult(new { url = data?.URL});

    }