如何设置 HTTPS 端点来接收 json 文件并通过 REST-API 实时提供最新的 json 文件

问题描述 投票:0回答:1

物联网设备每 20 到 40 秒生成一次测量结果,需要推送数据(json 文件,<1kB) to a HTTPS Endpoint with a key for authentication. The most recent json file must be made available via a REST-API with another key authentication. The data must be more or less real-time (no delay >30 秒)。

理想情况下,该设置是可扩展的(且经济实惠),可以接收来自多个物联网设备的测量结果,并使测量结果可以在不同的 API 上使用,或者通过一种基于 ID 选择测量的方法来实现。

我查看了 Azure 事件中心和事件网格,但我不知道我是否走在正确的轨道上。非常感谢有关 Azure 平台或任何其他合适平台的任何指导。

azure rest https iot
1个回答
0
投票

除非有比你提到的更复杂的事情,否则我会选择便宜而简单的东西,例如:

  • HttpTrigger
    POST Azure 函数以接收 JSON。将其存储在 Azure Blob 存储容器中。

  • HttpTrigger
    获取 Azure Function,通过从同一 Blob 存储中检索来提供最新的 JSON。

  • 通过在 Azure 函数中使用中间件进行身份验证,使用最适合您的任何身份验证实现。

...如果它真的像你说的那么简单!如果冷启动延迟(几秒钟)不是问题,并且每天不是数百万个请求,那么您可以在消耗计划资源中免费运行它。而且 Blob 存储也非常便宜。

编辑 - 请求有关从 Blob 存储/检索的信息

遵循使用 Azure Functions 和 Blob 的原则,并假设您使用的是 .NET 8 独立函数。我假设您使用简单的标头令牌作为身份验证(这可能/不够 - 它超出了此处的范围。)

首先您需要在

Program.cs

进行设置
  • Azure Blob 库的依赖注入
  • 将您的身份验证类添加到中间件管道中(这只是 1 个选项,还有其他选项)

这是一个例子:

var host = new HostBuilder()
    .ConfigureFunctionsWorkerDefaults(worker =>
    {
        worker.UseMiddleware<AuthMiddleware>(); // Name of your auth class
    })
    .ConfigureServices(services =>
    {
        services
            .AddAzureClients(configureClients =>
            {
                configureClients.AddBlobServiceClient(Environment
                    .GetEnvironmentVariable("YourBlobStorageResource")); // Wherever your Blob Container is going
            });
    })
    .Build();

host.Run();

然后在 Azure Function 端点类中,您可以将

BlobServiceClient
作为构造函数依赖项,并使用它来存储 JSON。我没有展示相应的
get
函数,但我相信你可以根据
post
来弄清楚。

public class MyFunctions
{
    private readonly ILogger _logger;
    private readonly BlobServiceClient _blob;
 
    public MyFunctions(ILoggerFactory loggerFactory, BlobServiceClient blobServiceClient)
    {
        _logger = loggerFactory.CreateLogger<MyFunctions>();
        _blob = blobServiceClient ?? throw new ArgumentNullException(nameof(blobServiceClient));
    }

    [Function(nameof(PostIoTData))]
    public async Task<HttpResponseData> PostIoTData([HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = "your-route/{iotId}")] HttpRequestData req, string iotId)
    {
        var container = _blob.GetBlobContainerClient(iotId); // Assuming you have a container per IoT
        await container.CreateIfNotExistsAsync();
        var blob = container.GetBlobClient("latest-data.json"); // Or if you want a history, maybe this would be a unique filename

        // ...then write your json to the Blob.
        // ...and return an HttpResponseData
    }
}

最后,你的

AuthMiddleware
类可能是这样的,假设你只是检查一个简单的标题:

public class AuthMiddleware : IFunctionsWorkerMiddleware
{
    private readonly ILogger _logger;

    public AuthMiddleware(ILogger<AuthMiddleware> logger)
    {
        _logger = logger;
    }

    public async Task Invoke(FunctionContext context, FunctionExecutionDelegate next)
    {
        var requestData = await context.GetHttpRequestDataAsync();

        IEnumerable<string>? apiKeyValues;
        if (requestData?.Headers.TryGetValues("Api-Key", out apiKeyValues) == true)
        {
            string? apiKey = apiKeyValues?.SingleOrDefault();
            if (apiKey != null && Guid.TryParse(apiKey, out _) == true)
            {
                // Validate your API key here.

                // If it's authorised, then...
                await next(context);
            }
        }

        throw new InvalidOperationException("Not authorised");
    }
}

注释

这绝对不是实现您的要求的唯一方法,但它符合您迄今为止给出的要求。希望它能给您一些值得考虑的有趣观点。

© www.soinside.com 2019 - 2024. All rights reserved.