Atihska Atihska - 6 months ago 129
Java Question

FileNotFoundException for config.properties in aws lambda test console

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 {

@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

Answer

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")