How to read a file in AWS Lambda Function written in Java ?

前端 未结 4 484
庸人自扰
庸人自扰 2021-02-04 15:24

I have written an AWS Lambda Handler as below :

package com.lambda;
import com.amazonaws.services.lambda.runtime.Context;
import com.amazonaws.services.lambda.r         


        
4条回答
  •  清歌不尽
    2021-02-04 16:02

    If the file in located under resources directory, then the following solution should work:

    String fileName = "resources/config.json";
    
    Path path = Paths.get(this.getClass().getResource("/").toURI());
    Path resourceLocation = path.resolve(fileName);
    
    try(InputStream configStream = Files.newInputStream(resourceLocation)) {
                //use your file stream as you need.
    }
    

    Here the most important part is "resources/config.json", it must not be "/resources/config.json", because the file location is /var/task/resources/config.json in lambda, I checked.

    Hope this helps who still face problem in reading file in aws lambda.

提交回复
热议问题