2

I have an AWS lambda sample, created using AWS Toolkit for eclipse. I added a config.properties file in the project from eclipse. I am also then uploading using right click project->Amazon Web Services -> Upload

But when I test on aws console, it gives me filenotfound for config.properties.

Please help!

Here is my project structure: I get error at line 33 telling that config.properties file not found. enter image description here

here is my lambda function:

import com.amazonaws.services.lambda.runtime.Context;
import com.amazonaws.services.lambda.runtime.RequestHandler;

public class LambdaFunctionHandler implements RequestHandler<String, WebConnectResponse> {

    @Override
    public void handleRequest(String input, Context context) {

        context.getLogger().log("Input: " + input);

        try {
            new PreviewService().GetPreview(input);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return null;
    }

    public void GetPreview(String downloadUrl) throws Exception{    

        input = new FileInputStream("config.properties"); //ERROR HERE: FileNotFoundException by aws lambda when testing on aws lambda console. 

        props.load(input);

        //Download File
        downloadFileFromUrl(new URL(downloadUrl));


        return null;

    }

    public void downloadFileFromUrl(URL downloadUrl)throws Exception{

        FileUtils.copyURLToFile(downloadUrl, new File("<filepath>"));

        uploadFileToServer("<filepath>");


    }

    public void uploadFileToServer(String filePath) throws Exception 
    {

        String fileExternalRefId = "id";
        String param = getProperty("param");
        URL uploadUrl = new URL(getProperty("uploadurl")); 

        File contents = new File("<filepath>"); 

        String boundary = Long.toHexString(System.currentTimeMillis());

        String CRLF = "\r\n"; //Line Separator required by multipart/form-data

        URLConnection connection = uploadUrl.openConnection();
        connection.setDoOutput(true);
        connection.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + boundary);
        connection.addRequestProperty("file_name", contents.getName());
        connection.addRequestProperty("id", fileId);

        try(
                OutputStream output = connection.getOutputStream();
                PrintWriter writer = new PrintWriter(new OutputStreamWriter(output, "UTF-8"), true);
                ) {

            //Send headers/params
            writer.append("--" + boundary).append(CRLF);
            writer.append("Content-Disposition: form-data; name=\"param\"").append(CRLF);
            writer.append("Content-Type: application/xml; charset=UTF-8").append(CRLF);
            writer.append(CRLF).append(param).append(CRLF).flush();

            //Send contents
            writer.append("--" + boundary).append(CRLF);
            writer.append("Content-Disposition: form-data; name=\"file-content\"; filename=\"" +  contents.getName() + "\"").append(CRLF);
            writer.append("Content-Type: application/xml; charset=UTF-8").append(CRLF);
            writer.append(CRLF).flush();

            Files.copy(contents.toPath(), output);
            //IOUtils.copy(in, output);
            output.flush();
            writer.append(CRLF).flush();//It indicates end of boundary

            writer.append("--" + boundary + "--").append(CRLF).flush();

        }
        int responseCode = ((HttpURLConnection) connection).getResponseCode();

        if(responseCode == 200)
        {
            System.out.println(responseCode);
            String viewUrl = props.getProperty("url") 
            System.out.println(viewUrl);
        }


    }

    public String getProperty(String key)
    {
        return props.getProperty(key);
    }

}

Here is my config.properties that looks like

key1=value1
key2=value2
kdabir
  • 9,623
  • 3
  • 43
  • 45
Atihska
  • 4,803
  • 10
  • 56
  • 98
  • Help Us and we sure will help you. Show your code. We can't guess what you did to cause the error. – Jorge Campos Jun 07 '16 at 22:04
  • @JorgeCampos Thanks for replying. Apologies before, now added code and screenshot! – Atihska Jun 07 '16 at 22:22
  • Did you take into take into account that the `` Should be right for the OS your code is Running? I'm saying this because I see in the cutted image an `c:\...` which indicates windows file system and you are deploying it into a *nix system (amazon). Also your config.properties file is in the wrong place. Put it on the src folder. Right now it is not been deployed alongside your project. To confirm that rename your jar/war file to a .zip file and see if it is inside it. – Jorge Campos Jun 07 '16 at 22:27
  • @JorgeCampos: Yes, the file is there in the zip.. I agree on that part that I should store not in windows path but in place where amazon os can pick. So i can use maybe AWS S3 filepath. That approach should be good. Correct? – Atihska Jun 07 '16 at 22:55
  • 1
    You should code it as OS agnostic. So it would work everywhere. The suggestion from Marco answer is also good: `getClass().getClassLoader().getResource("config.properties")` – Jorge Campos Jun 07 '16 at 23:05

2 Answers2

2

I have little experience with AWS, but when you work with java Files or FileInputStreams must use the file path and you are using just the file name.

I think your code should be:

input = new FileInputStream("/[appDeployPath]/config.properties");

Maybe a better approach is to use:

getClass().getClassLoader().getResource("config.properties")
Jorge Campos
  • 22,647
  • 7
  • 56
  • 87
  • 1
    Marco A. Hernandez @JorgeCampos: I also added config file in src folder instead of the project and then used class loader as explained above. Thanks to both of you! – Atihska Jun 07 '16 at 23:17
1

I also had config file in my project, and this is how I read the content of this file, I have answered the question here -

https://stackoverflow.com/a/42757653/5892553

Community
  • 1
  • 1
Rito
  • 3,092
  • 2
  • 27
  • 40