0
votes

I have a logic app that uses azure function as a http trigger and gets a return string. When the azure function is to receive a Base64 string, create a file with the information and uploads to the assigned storage account, I keep getting status code 500 internal server error from the Azure function every time I run it. After many trial and error I deduced the problem occurs from when the file is to be created from the Base64 string and when the blob container client is created.

So Help me Please.

UPDATE: As per some of your suggestions, I implemented application insights ran it a few times and got this error occuring twice:

Azure.RequestFailedException

Message: Exception while executing function: BlobAdd The specifed resource name contains invalid characters

Status: 400 (The specifed resource name contains invalid characters.)

ErrorCode: InvalidResourceName

FailedMethod: 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
        }
        );
}
1
Try running it locally and check for exceptions, or if you have Application Insights enabled, monitor your function's execution there and see if there's any errors. Update your question with any error you get because there isn't enough info. - Martin Frøhlich
and what is the error? You should enable Application Insights in order to get more info about the error (stack, error line, etc) - Thiago Custodio

1 Answers

0
votes

You are trying to write to the "D" disk. Since the Azure function does not have direct access to disks, you get a 500 error when trying to write to a disk that does not exist.

To write to a file from an Azure function you can use the Azure Storage SDK.