1

I have created a WebApi project and deployed this as a serverless application to AWS Lambda. It all works fine except that for one method, I need to read from a file. I can see that this file is deployed as it's in the zip file that gets pushed up but for some reason, when I come to read from it in the code, it can't find it.

I've tried both embedded resource and content for build action but neither seem to work. Is there something in AWS that needs configuring so that my serverless application can access local files?

3
  • I have a Lambda that accesses HTML template files. I have set Build Action to None and Copy to Output Directory to Copy Always. Seems to work fine. Commented Jul 6, 2018 at 10:02
  • 1
    How do you access those files? I've tried using Directory.GetCurrentDirectory() and the LAMBDA_TASK_ROOT native environment variable to get the path to the file but neither seem to work. Commented Jul 6, 2018 at 10:06
  • Ahh, nevermind. I've realised I didn't need to create an absolute path. I've removed the Directory.GetCurrentDirectory() and used a relative path and it works. Thanks for your help. Commented Jul 6, 2018 at 10:14

1 Answer 1

3

For anyone that needs more detail. Add your embedded files into your .csproj file as below (resources is your folder, note the "resources\" in .csproj and "resources/" in .cs):

<ItemGroup>
    <Content Include="resources\*.*">
      <CopyToOutputDirectory>Always</CopyToOutputDirectory>
    </Content>
  </ItemGroup>

In your .cs file, to get the file path:

var path = Path.Combine(Directory.GetCurrentDirectory(), "resources/example.txt");
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.