I am using azure function app V3 and I am reading an excel from Data folder in the same project.
I have set Copy-always for excel and it's been copied into bin folder.
So In code I'm referring path as
var binDirectory = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)
but after publishing the Data folder is out side bin folder and my code is unable to find the excel.
Path of my excel after publish:-
C:\home\site\wwwroot\Data
path my code is looking at:-
C:\home\site\wwwroot\bin\Data
Kudu Path
Is there a generic way to make path to work in local and after published to azure.
Any help is appreciated!! Thanks in advance.
Try to add ExecutionContext context parameter in your function method, the use the code below:
public static void Run(other parameter, ExecutionContext context)
{
string templatePath = Path.Combine(context.FunctionAppDirectory, "Data");
}
Related
In my .net core web API , I'm using OpenHtmlToPdf nuget package for rendering HTML documents to PDF format. The implementation working fine locally but not in the server production K8 environment. I'm getting following message from the logs.
Permission denied
I did several workaround and was able to find out, a lib called OpenHtmltoPdf uses Path.GetTempPath() and Guid.NewGuid() to create a temp file. seems above permission denied error occurs when it trying to access that temp path. this is code snippet from the OpenHtmltoPdf lib. OpenHtmlToPdf git repo
//inside TemporaryPdf class
public static string TemporaryFilePath()
{
return Path.Combine(Path.GetTempPath(), "OpenHtmlToPdf", TemporaryPdf.TemporaryFilename());
}
private static string TemporaryFilename()
{
return Guid.NewGuid().ToString("N") + ".pdf";
}
I just added following line to my application to determine the temp path and its returns File Path is: /tmp/.
Console.WriteLine("File path is:" + Path.GetTempPath());
But in the Kubernetes pods have rwx permissions for /tmp/ folder for the all users.
My problem is, after deploying the API, do the NuGet packages use a separate temp path? So how exactly identify it?
This is a Unity project building to Hololens 2. I'm trying to create a new folder within the Application.PersistentDataPath but it's failing due to UnauthorizedAccessException. Strange thing is it's been working and only recently with seemingly unrelated changes it stopped.
Here's the function that is failing.
static DirectoryInfo EnsureDirectory(string subFolder)
{
Debug.Log($"starting EnsureDirectory() for {subFolder}.");
string directoryPath = Path.Combine(Application.persistentDataPath, subFolder.ValidatePath());
Debug.Log($"About to create directory {directoryPath}");
var dir = Directory.CreateDirectory(directoryPath);
Debug.Log($"Successfully created directory {dir.FullName}");
return dir;
}
At Directory.CreateDirectory I get the error as follows:
Hololens screenshot of debug log
EXCEPTION: UnauthorizedAccessException: Access to the path "C:\" is denied.
This error only happens when deployed to the device. In the Unity editor it works perfectly. I also don't know why it would say "C:" when that's not the path I'm trying to use.
Any help would be greatly appreciate.
Thank you #aybe for the answer that did the job.
This is probably because it checks for every path segment, starting
with the first one which is root and obviously forbidden. Try new
DirectoryInfo(Application.persistentDataPath).CreateSubdirectory(...);
instead to see if the error vanishes
I'm having some issues trying to access a folder outside the application root folder with an asp.net application deployed on Linux.
The applcation is deployed at the moment (for testing purposes) in /home/pbl/projects/pbl-web/.
I want to access a folder of an external hard drive I've mounted on the system, and it's located in /mnt/ExtDist/Data.
I'm using a PhysicalFileProvider with the following path: /mnt/ExtDist/Data. This path is configured in app.settings.json file and retrieved through the IConfiguration configuration variable.
Here's a part of the code in the Startup.cs file :
public void ConfigureServices(IServiceCollection services)
{
...
var imagePath = configuration.GetSection("PhotoManagementSettings")["ImagesFolderPath"];
var rootPath = Path.GetDirectoryName(Assembly.GetEntryAssembly().Location);
this.imagePhysicalFileProvider = new PhysicalFileProvider(imagePath));
I've tried in different ways with no luck so far:
passing the absolute path
passing the relative path and combining with the rootPath variable (see the code above).
The PhysicalFileProvider is getting me the following error:
Unhandled exception. System.IO.DirectoryNotFoundException: /mnt/ExtDist/Data/
Testing the code in windows and giving it an absolute path like i.e "C:\Test" works fine.
So there's something weird in linux that is failing, but I cannot understand why. Any clues ?
Thanks in advance
Paolo
I have created a Azure Function, which is having a dependency on ini file.
public class DataProcessingFunction
{
FunctionName("DataProcessingFunction")]
public async Task Run([EventGridTrigger]EventGridEvent eventGridEvent,ILogger log)
{
string iniFolderPath = $#"{Directory.GetCurrentDirectory()}\Ini\";
string iniFileName = "Sample.ini";
var iniConfig = FileManager.ReadFile(iniFolderPath, iniFileName);
}
}
I have selected Copy if never option in Visual Studio while publishing the code to Azure function
Also, I have tried selecting Embedded Resource. But I am not able to find the file
I get an exception
File not found.
Add/Upload option in Azure portal is disabled because I am publishing the function from Visual studio
Question: Do I need to upload file to blob and then refer it in a code?
The final conclusion is that the file was successfully uploaded, the problem is that an error occurred while reading the path. It seems that using Directory.GetCurrentDirectory() in Azure is not reliable.
I just tried it and Directory.GetCurrentDirectory() got the wrong path in azure (I printed it out and it showed "D:\Program Files (x86)\SiteExtensions\Functions\2.0.12961\32bit", This is obviously not the current folder), and eventually it fails to find the Sample.ini file. Since the function is your own, you can set the path to something like "D:\\home\\site\\wwwroot>\\Ini\\Sample.ini". This should read the Sample.ini file.
Here is the way to do it.
ExecutionContext context; // You can modify your function method to take an additional parameter of type ExecutionContext
public static async Task Run(<.... Other parameters>, ILogger log, ExecutionContext context)
Then build the path by combining function directory, "templates" is the directory I have in the function app project which is deployed along with other code, emails.html is file within templates (For each of these files you have to set, copy-always or copy-if-newer in the properties.). Instead of templates you can have your .ini file.
string templatePath = Path.Combine(context.FunctionAppDirectory, "Templates", "emails.html");
Im developing an Android app using the Xamarin platform and Im trying to create a folder on my local storage but I have had no success.
First I tried using the System.IO namespace through a FileManager class created by Xamarin Forms Labs. A snippet of the functions I was passing the path of "/storage/emulated/0/`".
public void CreateDirectory(string path)
{
this.isolatedStorageFile.CreateDirectory(path);
}
public Stream OpenFile(string path, FileMode mode, FileAccess access)
{
return (Stream)this.isolatedStorageFile.OpenFile(path, (System.IO.FileMode)mode, (System.IO.FileAccess)access);
}
This didn't work so I then opted to using PCLStorage a library for cross platform file operations. I tried this code.
IFolder rootFolder = FileSystem.Current.LocalStorage;
folder = await rootFolder.CreateFolderAsync("wakesocial",
CreationCollisionOption.OpenIfExists);
Didn't work. I navigated to the root of the internal storage and I didn't see the folder.
So the error doesn't seem to be occurring because of the libraries in use but something specific to Android. I have read and write to external storage in manifest. So the question is. Does an app have permission to create a file or folder at the root level of the storage device or does it have to create it at particular location such as in Android/data/packagename
string folderPath = Environment.ExternalStorageDirectory.AbsolutePath; //Android
public async Task PCLGenaratePdf(string folderPath )
{
IFolder rootFolder = await FileSystem.Current.GetFolderFromPathAsync(folderPath );
IFolder folder = await rootFolder.CreateFolderAsync("folder", CreationCollisionOption.OpenIfExists);
IFile file = await folder.CreateFileAsync("file.pdf", CreationCollisionOption.ReplaceExisting);
}
After lot of trying I solved this problem with this
string rootPath = Android.App.Application.Context.GetExternalFilesDir(null).ToString();
var filePathDir = Path.Combine(rootPath, "Folder");
if (!File.Exists(filePathDir))
{
Directory.CreateDirectory(filePathDir);
}
The folder created is reachable through adb # /sdcard/Android/Data/Folder or something like this, or in device memory # /Android/data/<< AppName >>/files/Folder
On Android, PCL Storage's LocalStorage folder corresponds to the "My Documents" Special Folder.
I'm not sure exactly what path you're trying to create a folder in, but if you know the path and want to use PCL Storage, you should be able to use FileSystem.Current.GetFolderFromPathAsync to get a PCL Storage IFolder corresponding to an existing folder.