GoogleCloudPlatform/functions-framework-dotnet

Failure to read a file when deploying on Google Cloud Functions.

NguyenMinhTri opened this issue · 1 comments

I am trying to read file but I am getting suck "File does not exist" when the code is deployed on Google Cloud with the following images and code.

public class Function : IHttpFunction
{
 
    private IWebHostEnvironment _hostEnvironment;

    public Function(IWebHostEnvironment environment)
    {
        _hostEnvironment = environment;
    }

    public async Task HandleAsync(HttpContext context)
    {
        string path = _hostEnvironment.ContentRootPath + "\\Book.xlsx";
        Console.WriteLine(File.Exists(path) ? "File exists." : "File does not exist.");
        await context.Response.WriteAsync(path);
    }
}

Log of my program on Google Cloud:
image

Log of my program on local host (Window 10)
image

Project structure as below
image

I suggest you use Path.Combine(_hostEnvironment.ContentRootPath, "Book.xlsx) instead of _hostEnvironment.ContentRootPath + "\\Book.xlsx" - your function will be executed on Linux, where the path file separator is / rather than \\.

That may not be the problem - we don't know anything about how you're ensuring that Book.xlsx is actually deployed alongside your function.
(You might like to list all the files in _hostEnvironment.ContentRootPath as your function output, to find that out.)