azure - Azure 函数状态码 500 内部服务器错误
问题描述
我有一个逻辑应用程序,它使用 azure 函数作为 http 触发器并获取返回字符串。当 azure 函数要接收 Base64 字符串,使用该信息创建一个文件并上传到分配的存储帐户时,我每次运行 Azure 函数时都会不断收到状态代码 500 内部服务器错误。经过多次试验和错误后,我推断问题发生在从 Base64 字符串创建文件的时间以及创建 blob 容器客户端的时间。
所以请帮助我。
更新:根据您的一些建议,我实施了应用程序洞察,运行了几次,并且出现了两次此错误:
Azure.RequestFailedException
消息:执行函数时出现异常:BlobAdd 指定的资源名称包含无效字符
状态:400(指定的资源名称包含无效字符。)
错误代码:无效资源名称
失败方法:Azure.Storage.Blobs.BlobRestClient+Container.CreateAsync_CreateResponse。
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 requestBody = await new StreamReader(req.Body).ReadToEndAsync();
return await Base64(requestBody);
}
public static async Task<IActionResult> Base64(string Base64Body)
{
string HoldDBase = "";
string TestBlobData = "null";
if (Base64Body.Length > 10)
{
HoldDBase = Base64Body;
}
else
{
TestBlobData = "The Base64Body did not Pass";
return (ActionResult)new OkObjectResult
(
new
{
TestBlobData
}
);
}
//Connection string of the Storage Account Azure
string ConnectionString = "xxxxxxxxx";
// Create a BlobServiceClient object which will be used to create a container client
BlobServiceClient blobServiceClient = new BlobServiceClient(ConnectionString);
//Create a unique name of the container
string ContainerName = "Base64_Blob" + Guid.NewGuid().ToString();
//create the container and return a container client Object
BlobContainerClient ContainerClient = await blobServiceClient.CreateBlobContainerAsync(ContainerName); //Problem Here
//create a local file in the Storage
string localPath = "D:/Reliance/OlaForm/uploadsO";
string fileName = "quickstart" + Guid.NewGuid().ToString() + ".txt";
string localFilePath = Path.Combine(localPath, fileName);
//convert string to bytes
byte[] BaseBytes = Convert.FromBase64String(HoldDBase);
//create file in local data
await File.WriteAllBytesAsync(localFilePath,BaseBytes); //Problem Here
//get reference to a blob
BlobClient blobclient = ContainerClient.GetBlobClient(fileName);
// Open the file and upload its data
FileStream uploadFileStream = File.OpenRead(localFilePath);
await blobclient.UploadAsync(uploadFileStream);
// blobclient.Upload(uploadFileStream);
uploadFileStream.Close();
//blob id from blobclient and return it
TestBlobData = blobclient.ToString();
TestBlobData = HoldDBase;
return (ActionResult)new OkObjectResult
(
new {
TestBlobData
}
);
}
解决方案
您正在尝试写入“D”磁盘。由于 Azure 函数无法直接访问磁盘,因此在尝试写入不存在的磁盘时会收到 500 错误。
要从 Azure 函数写入文件,可以使用 Azure 存储 SDK。
推荐阅读
- python - Python使用变量作为键从dict中检索值
- google-app-engine - 应用引擎flex nodejs worker_connections错误
- javascript - Socket.io 套接字接收类型错误和数据解析器错误
- java - Java 垃圾收集器会运行吗?
- python - 我如何每天在python中创建一个变量
- arduino - 根据条件执行中断
- xcode - xcode 不构建 arm64/apple 硅通用 dylib
- javascript - 在网页上显示页面加载时间
- c# - Unity:试图让我的子弹在 X 轴上水平射击
- python - 将 Keras 生成器传递给 My Model 的 __call__ 方法