gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 05:36:00 25 4
gpt4 key购买 nike

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

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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com