json - 具有 HTTP 触发器和 Blob 输入绑定(bind)的 Azure 函数 - 无法读取文件夹中的 JSON 文件。可能的 blob 文件路径错误?

标签 json azure azure-functions azure-blob-storage blob

我使用的 Azure 函数具有 HTTP 触发器,其路由参数 {id} 是我要读取的 JSON 文件的 fileId。 我正在使用 Blob 输入绑定(bind)来绑定(bind) JSON 文件的存储位置。 JSON 文件存储在名为“conversations”的容器中,然后存储在名为“Conversation”的文件夹中。 文件路由的示例是 "https://<STORAGE_ACCOUNT_NAME>/conversations/Conversation/8da3d7ad3e35273-1aWpKU4rVghHiTaYkjOjVC-eu%7C0000000.json"

下面是我的代码。

public static class GetConvo
    {
        [FunctionName("GetConvo")]
        public static async Task<IActionResult> Run(
            [HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = "getConvo/{id}")] HttpRequest req,
            [Blob("conversations/{id}", FileAccess.Read, Connection = "AzureWebJobsStorage")] string json,
            ILogger log, string id)

        {
            
            log.LogInformation($"File name: {id}");

            if (json == null)
            {
                log.LogInformation($"File {id} not found");
                return new NotFoundResult();
            }
            else
            {
                log.LogInformation($"Content: {json}");
            }


            return new OkObjectResult(JsonConvert.DeserializeObject<Message>(json));

如果我将 JSON 文件移动到“Conversation”文件夹之外,上面的代码就可以工作,我可以访问它并收到 200OK 代码。

enter image description here

我尝试将 Blob 输入绑定(bind)路径更改为“conversations/Conversation/{id}”,如下所示,但返回 404 代码。

[FunctionName("GetConvo")]
        public static async Task<IActionResult> Run(
            [HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = "getConvo/{id}")] HttpRequest req,
            [Blob("conversations/Conversation/{id}", FileAccess.Read, Connection = "AzureWebJobsStorage")] string json,
            ILogger log, string id)

这是 Blob 输入路径问题吗?

如何使用 azure 函数读取 Blob 容器中文件夹中的 JSON 文件?

最佳答案

@AjgB,是的,Blob 路径不正确。您需要提供文件扩展名。

假设该文件直接放置在您的“对话”文件夹中。那么你的 BLOB 输入绑定(bind)应该是 -

[Blob("conversations/{id}.json", FileAccess.Read, Connection = "AzureWebJobsStorage")] string json

注意 blob 路径中的 .json

关于json - 具有 HTTP 触发器和 Blob 输入绑定(bind)的 Azure 函数 - 无法读取文件夹中的 JSON 文件。可能的 blob 文件路径错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72543246/

相关文章:

无需 HttpTrigger 的 Azure 持久函数调用(自动启动)

azure - 空 ARM 存储帐户上的异常事件

c# - 天蓝色函数 : How to access the host key programmatically using C# code?

python - 使用 Azure Function for python 和 ActiveDirectoryMSI 身份验证在 Azure SQL Server 数据库连接中出错

json - 在powershell中用空字符串键解析json

java - JSON Jackson,如何使用只读属性别名来实现向后兼容性?

php - 从 PHP 解析 JSON 时出现问题

Azure Blob 创建未触发 Azure 数据工厂事件触发器

python - Azure Function App 上的 EasyAuth 错误自定义 oidc 提供程序

.net - .Net 微框架的 Json 库