gpt4 book ai didi

java - aws lambda 测试控制台中 config.properties 的 FileNotFoundException

转载 作者:行者123 更新时间:2023-11-29 04:44:45 24 4
gpt4 key购买 nike

我有一个 AWS lambda 示例,使用 AWS Toolkit for eclipse 创建。我在 eclipse 的项目中添加了一个 config.properties 文件。然后,我还使用右键单击项目->Amazon Web Services -> 上传

进行上传

但是当我在 aws 控制台上测试时,它为我提供了 filenotfound for config.properties。

请帮忙!

这是我的项目结构:我在第 33 行收到错误消息,提示找不到 config.properties 文件。 enter image description here

这是我的 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")

关于java - aws lambda 测试控制台中 config.properties 的 FileNotFoundException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37690100/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com