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

前端 未结 4 474
庸人自扰
庸人自扰 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 15:59

    If the file is located under resources folder, you can use it directly in lambda by using something like the following code:

    final BufferedReader br = new BufferedReader(new FileReader("/flows/cancellation/MessageArray.json"));
    

    I wanted to read a json file, you can have different use case, but the code works.

    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2021-02-04 16:05

    This is how I did it, let's say this is how your project structure looks like -

    And you want to read the file config.properties which is inside the project-dir/resources directory.

    The code for reading the content of the file would be -

    InputStream input = null;
    try {
        Path path = Paths.get(PropertyUtility.class.getResource("/").toURI());
    
        // The path for config file in Lambda Instance -
        String resourceLoc = path + "/resources/config.properties";
    
        input = new FileInputStream(resourceLoc);
    } catch(Exception e) {
        // Do whatever
    }
    

    If you are following this project structure and using this code, then it will work in AWS Lambda.

    PropertyUtility is just a utility class that I have created to read the contents of the config file. The PropertyUtility class looks like this -

    As you can see in the above code, the path of the config file is different in the local system and in Lambda Instance.

    In your local machine, PropertyUtility.class.getResource("/") points to bin, that is why you have to do path.getParent(), to point it to the project-directory which is HelloLambda in this example.

    For the Lambda Instance, PropertyUtility.class.getResource("/") points directly to the project-directory.

    0 讨论(0)
  • 2021-02-04 16:08

    I have made following changes in my code and now its works perfect :

    Majorly changed following two lines :

    ClassLoader classLoader = getClass().getClassLoader();

    File cityFile = new File(classLoader.getResource("City.db").getFile());

    package com.lambda;
    
    
    import com.amazonaws.services.lambda.runtime.Context;
    import com.amazonaws.services.lambda.runtime.LambdaLogger;
    import com.amazonaws.services.lambda.runtime.RequestStreamHandler;
    import java.io.*;
    
    public class TestDetailsHandler implements RequestStreamHandler {
    
    
        public void  handleRequest(InputStream input,OutputStream output,Context context){
    
            // Get Lambda Logger
            LambdaLogger logger = context.getLogger();
    
            // Receive the input from Inputstream throw exception if any
    
            ClassLoader classLoader = getClass().getClassLoader();
    
            File cityFile = new File(classLoader.getResource("City.db").getFile());
            FileInputStream fis = null;
    
            try {
                fis = new FileInputStream(cityFile);
    
                System.out.println("Total file size to read (in bytes) : "
                        + fis.available());
    
                int content;
                while ((content = fis.read()) != -1) {
                    // convert to char and display it
                    System.out.print((char) content);
                }
    
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                try {
                    if (fis != null)
                        fis.close();
                } catch (IOException ex) {
                    ex.printStackTrace();
                }
            }
        }
    

    0 讨论(0)
提交回复
热议问题