Reputation: 38
I am building a web service using Azure Function app V2 using .net core 2.2 stack. My function is working perfectly as per requirement. What i am trying to achieve is function read from JSON file for fetching particular values from it to further process. I am using Visual studio 2019 and publishing app through IDE itself. So my app is in read-mode by default. I have uploaded JSON file in same file as my function file. Also i have tried deleting WEBSITE_RUN_FROM_PACKAGE from config but no luck as it doesnt show function in portal anymore.
When i am running app after publishing the app through VS2019 way (read from zip) making sure that json is in same folder as function file, i am getting an error as follow:
"Could not find file 'D:\home\site\wwwroot\functionname\filename.json'."
I am trying to access file using following code:
var filePath = Path.Combine(context.FunctionDirectory, "filename.json");
Upvotes: 0
Views: 14253
Reputation: 462
If you want to deploy additional files along with your function app, change the .csproj file. Find the ItemGroup nodes and update like so. I am adding two files json1.json, and index.html. I had to manually add the CopyToPublishDirectory nodes, which are not present by default for user added items. Deploy using zip deploy from VS2019.
<ItemGroup>
<None Remove="index.html" />
<None Remove="json1.json" />
<None Remove="local.settings.json" />
</ItemGroup>
<ItemGroup>
<Content Include="json1.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
<CopyToPublishDirectory>PreserveNewest</CopyToPublishDirectory>
</Content>
<Content Include="index.html">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
<CopyToPublishDirectory>PreserveNewest</CopyToPublishDirectory>
</Content>
<Content Include="local.settings.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
<CopyToPublishDirectory>Never</CopyToPublishDirectory>
</Content>
</ItemGroup>
Then use this in your code:
string path = System.IO.Path.Combine(context.FunctionAppDirectory, "json1.json");
string content = System.IO.File.ReadAllText(path);
Upvotes: 1
Reputation: 23111
According to my test, if we deploy the function to Azure in Visual Studio 2019, we cannot deploy the json file to Azure. We need to manually upload the file to Azure function via kudo.
Besides, if your file is too big, I suggest you store the file in Azure Blob storage then use the blob in your function. For more details, please refer to the document.
Regarding how to configure Azure Blob Storage input, please refer to the following steps.
Update your project
a. Install the sdk in Visual Studio
Install-Package Microsoft.Azure.WebJobs.Extensions.Storage -Version 3.0.8
b. Update local.settings.json Please add the connecting string of the account you use to store json file.
{
"IsEncrypted": false,
"Values": {
"AzureWebJobsStorage": " your function storage",
"MyStorage": "the storage you use to store file",
"FUNCTIONS_WORKER_RUNTIME": "dotnet"
}
}
c. Update Code
[FunctionName("Function1")]
public static async Task<IActionResult> Run(
[HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = null)] HttpRequest req,
[Blob("test/test.json", FileAccess.Read, Connection = "MyStorage")] Stream blob,
ILogger log,ExecutionContext context)
{
log.LogInformation("C# HTTP trigger function processed a request.");
string text=null;
blob.Seek(0,SeekOrigin.Begin);
using (StreamReader streamReader = new StreamReader(blob)) {
while (!streamReader.EndOfStream) {
string textLine = await streamReader.ReadLineAsync();
text += textLine;
}
dynamic data = JsonConvert.DeserializeObject(text);
return (ActionResult)new OkObjectResult(data);
}
}
Publish your project
Upvotes: 2