我有一个逻辑应用程序,它将azure函数用作http触发器并获取返回字符串。 当azure函数要接收Base64字符串,使用信息创建文件并上传到分配的存储帐户时,每次运行它时,我都会不断从Azure函数中获取状态代码500内部服务器错误。经过多次尝试和错误,我推断出问题发生在从Base64字符串创建文件以及创建Blob容器客户端的时候。
所以请帮助我。
更新:根据您的一些建议,我对应用程序的见解进行了几次,并两次出现此错误:
Azure.RequestFailedException
消息:执行函数:BlobAdd时发生异常指定的资源名称包含无效字符
状态:400(指定的资源名称包含无效字符。)
ErrorCode:InvalidResourceName
失败的方法: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
}
);
}
答案 0 :(得分:0)
您正在尝试写入“ D”盘。由于Azure功能无法直接访问磁盘,因此在尝试写入不存在的磁盘时会出现500错误。
要从Azure函数写入文件,可以使用Azure存储SDK。