.net - Azure Function 不适用于参数化 API

标签 .net azure

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

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

我在本地创建了一个代码并将其发布到网上 - 它可以在本地和 Azure 功能上运行。

下面的代码片段适用于本地和 Azure 功能。 请注意,文件名是硬编码的

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

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

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

最佳答案

当您将授权级别定义为函数时,它需要一个功能键:

[HttpTrigger(AuthorizationLevel.Function

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

https://.azurewebsites.net/api/?code=

enter image description here

https://learn.microsoft.com/en-us/azure/azure-functions/functions-bindings-http-webhook-trigger?tabs=csharp#obtaining-keys

PS:不要忘记传递文件名参数

关于.net - Azure Function 不适用于参数化 API,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62599809/

相关文章:

.net - PostSharp 与 DynamicProxy2 接口(interface)拦截

.net - 使用 ReaderWriterLockSlim 会导致内存障碍吗?

macos - 在 mac os 上运行 azure 时 Node.js 异常 201

sql - Azure/U-SQL - 将查询输出到屏幕

interop - 托管到 Windows Azure 上的 native 互操作 NO P/Invoke

java - 尝试查看数据时,Azure Android CloudAppendBlob 导致错误 409(冲突)

c# - 如何使用 C# 安全连接到 MySQL,反编译时不显示任何信息

.net - 带有值的字典的环境变量列表 - 惯用的 F#

.net - 将 HTML 转换为 PDF

python - 在 azure 中哪里可以找到 "storage_account_name","storage_account_key","storage_container_name"?