I want to upload files into azure blob storage using AzureFunctions in c#.
So this is my azure function :
public static class AppartementFunc
{
[Function("AppartementFunc")]
public static async Task<IActionResult> Run(
[HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = null)] HttpRequest req, ILogger log)
{
string Connection = Environment.GetEnvironmentVariable("AzureWebJobsStorage");
string containerName = Environment.GetEnvironmentVariable("ContainerName");
Stream myBlob = new MemoryStream();
var file = req.Form.Files["File"];
myBlob = file.OpenReadStream();
var blobClient = new BlobContainerClient(Connection, containerName);
var blob = blobClient.GetBlobClient(file.FileName);
await blob.UploadAsync(myBlob);
return new OkObjectResult("file uploaded successfylly");
}
}
I tried to test this function from postman like this :
but this line var file = req.Form.Files["File"]
trigger an error :
`System.NullReferenceException: 'Object reference not set to an instance of an object.'
req was null.`
I dont know why the HttpRequest req is NULL ! Can anyone help me with that ?
My guess is you're using code for Azure Functions "in-process" hosting model while the application is actually hosted via "isolated worker" model.
For Azure Functions Isolated Worker hosting model, the HttpTrigger
parameter should be of type HttpRequestData
and HTTP response return types should be Task<HttpResponseData>
Differences Between In-Process & Isolated Worker Models
Guide for running C# Azure Functions in an isolated worker process