Azure Function无法用于参数化的API [英] Azure Function not working for parameterized API

查看:73
本文介绍了Azure Function无法用于参数化的API的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从下面的线程中找到了解决问题的方法

I followed a solution to my problem from this below thread

https://stackoverflow.com/questions/62596741/azure-function-not-working-cannot-declare-namespace-in-script-code

我在本地创建了一个代码,然后在线发布了该代码-它在两个版本上均有效-本地&Azure功能.

I created a code locally and published it online - it was working on both - local & Azure function.

下面的代码段都可以使用-本地&Azure功能.请注意,文件名是硬编码的.

Below code snippet isworking on both - local & Azure function. Notice that filename is hardcoded.

using System;
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 Azure.Storage.Blobs;

namespace FunctionApp1
{
    public static class Function1
    {
        [FunctionName("Function1")]
        public static async Task<IActionResult> Run(
            [HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequest req,
            ILogger log)
        {
            log.LogInformation("C# HTTP trigger function processed a request.");

            string name = req.Query["name"];

            string requestBody = await new StreamReader(req.Body).ReadToEndAsync();
            dynamic data = JsonConvert.DeserializeObject(requestBody);
            name = name ?? data?.name;

            string responseMessage = string.IsNullOrEmpty(name)
                ? "This HTTP triggered function executed successfully. Pass a name in the query string or in the request body for a personalized response."
                : $"Hello, {name}. This HTTP triggered function executed successfully.";

            string filename = "success.png";
            string storageconnstring = "**********";
            BlobServiceClient blobServiceClient = new BlobServiceClient(storageconnstring);

            BlobContainerClient containerClient = blobServiceClient.GetBlobContainerClient("demo");

            BlobClient blobClient = containerClient.GetBlobClient(filename);
            var blobUri = blobClient.Uri;
            BlobContainerClient targetContainerClient = blobServiceClient.GetBlobContainerClient("demo-copy");//This is the container where we want to copy the blob
            BlobClient targetBlobClient = targetContainerClient.GetBlobClient(filename);
            await targetBlobClient.StartCopyFromUriAsync(blobUri);


            return new OkObjectResult(responseMessage);
        }
    }
}

现在此代码仅在本地运行-我所做的唯一更改是现在您可以传递文件名了.我在做什么错了?

Now this code is working only locally - the ONLY change I did was that now you can pass the filename. What I am doing wrong?

using System;
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 Azure.Storage.Blobs;

namespace FunctionApp1
{
    public static class Function1
    {
        [FunctionName("Function1")]
        public static async Task<IActionResult> Run(
            [HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequest req,
            ILogger log)
        {
            log.LogInformation("C# HTTP trigger function processed a request.");

            string filename = req.Query["filename"];

            string requestBody = await new StreamReader(req.Body).ReadToEndAsync();
            dynamic data = JsonConvert.DeserializeObject(requestBody);
            filename = filename ?? data?.filename;

            string responseMessage = string.IsNullOrEmpty(filename)
                ? "This HTTP triggered function executed successfully. Pass a file name in the query string or in the request body for initiating a temporary copy"
                : $"This HTTP triggered function executed successfully.";

            string storageconnstring = "DefaultEndpointsProtocol=https;AccountName=mainices;AccountKey=lrlWVXi9tWDfkjv6XMcMgylPG2fU78nOcK3AwJkRJTBKDQ4FdxJkieYiGBhfFTYULl+IHey0OJASpkHlg25Eaw==;EndpointSuffix=core.windows.net";
            BlobServiceClient blobServiceClient = new BlobServiceClient(storageconnstring);

            BlobContainerClient containerClient = blobServiceClient.GetBlobContainerClient("demo");

            BlobClient blobClient = containerClient.GetBlobClient(filename);
            var blobUri = blobClient.Uri;
            BlobContainerClient targetContainerClient = blobServiceClient.GetBlobContainerClient("demo-copy");//This is the container where we want to copy the blob
            BlobClient targetBlobClient = targetContainerClient.GetBlobClient(filename);
            await targetBlobClient.StartCopyFromUriAsync(blobUri);


            return new OkObjectResult(targetBlobClient.Uri);
        }
    }
}

推荐答案

在您将自动化级别定义为函数时,它需要一个函数键:

As you defined the autorization level as functions, it is expecting a function key:

[HttpTrigger(AuthorizationLevel.Function

您需要将其添加到您的查询字符串中:

you'll need to add it to your querystring:

https://< APP_NAME> .azurewebsites.net/api/< FUNCTION_NAME>?code =< API_KEY>

https://<APP_NAME>.azurewebsites.net/api/<FUNCTION_NAME>?code=<API_KEY>

PS:也不要忘记传递filename参数

PS: do not forget to pass the filename parameter too

这篇关于Azure Function无法用于参数化的API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆