简体   繁体   中英

Azure Function Status Code 500 internal server error

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM