aws lambda 测试控制台中 config.properties 的 FileNotFoundException
FileNotFoundException for config.properties in aws lambda test console
我有一个 AWS lambda 示例,使用 AWS Toolkit for eclipse 创建。我在 eclipse 的项目中添加了一个 config.properties 文件。然后我还使用右键单击项目->亚马逊网络服务->上传
上传
但是当我在 aws 控制台上测试时,它为我提供了 config.properties.
的 filenotfound
请帮忙!
这是我的项目结构:我在第 33 行收到错误消息,提示未找到 config.properties 文件。
这是我的 lambda 函数:
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);
}
}
这是我的 config.properties 看起来像
key1=value1
key2=value2
我对 AWS 没有什么经验,但是当你使用 java 文件或 FileInputStreams 时必须使用文件路径,而你只使用文件名。
我认为你的代码应该是:
input = new FileInputStream("/[appDeployPath]/config.properties");
也许更好的方法是使用:
getClass().getClassLoader().getResource("config.properties")
我的项目中也有 config 文件,这就是我读取该文件内容的方式,我在这里回答了问题 -
我有一个 AWS lambda 示例,使用 AWS Toolkit for eclipse 创建。我在 eclipse 的项目中添加了一个 config.properties 文件。然后我还使用右键单击项目->亚马逊网络服务->上传
上传但是当我在 aws 控制台上测试时,它为我提供了 config.properties.
的 filenotfound请帮忙!
这是我的项目结构:我在第 33 行收到错误消息,提示未找到 config.properties 文件。
这是我的 lambda 函数:
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);
}
}
这是我的 config.properties 看起来像
key1=value1
key2=value2
我对 AWS 没有什么经验,但是当你使用 java 文件或 FileInputStreams 时必须使用文件路径,而你只使用文件名。
我认为你的代码应该是:
input = new FileInputStream("/[appDeployPath]/config.properties");
也许更好的方法是使用:
getClass().getClassLoader().getResource("config.properties")
我的项目中也有 config 文件,这就是我读取该文件内容的方式,我在这里回答了问题 -