Java依赖项找不到自己的资源-代码在AWS Lambda中运行
我的lambda代码使用一个依赖项,该依赖项使用Java依赖项找不到自己的资源-代码在AWS Lambda中运行,java,amazon-web-services,aws-lambda,Java,Amazon Web Services,Aws Lambda,我的lambda代码使用一个依赖项,该依赖项使用ClassOfDependency.class.getResourceAsStream(“/filename.jks”)加载自己的资源。当我创建一个使用这种依赖关系的简单main时,这很好,但是当我将此代码部署到lambda时,它抛出一个错误,表示找不到“/filename.txt”。我是不是遗漏了什么?“filename.jsk”位于src/resources/中,其中资源在IntelliJ中被标记为资源目录 谢谢我是这样做的-下面的方法接受文件
ClassOfDependency.class.getResourceAsStream(“/filename.jks”)
加载自己的资源。当我创建一个使用这种依赖关系的简单main时,这很好,但是当我将此代码部署到lambda时,它抛出一个错误,表示找不到“/filename.txt”。我是不是遗漏了什么?“filename.jsk”位于src/resources/中,其中资源在IntelliJ中被标记为资源目录
谢谢我是这样做的-下面的方法接受
文件名
并返回文件
private File getSampleSourceFile(String fileName) {
try {
ClassLoader classLoader = getClass().getClassLoader();
URL url = classLoader.getResource(fileName);
if(url == null) {
logger.log("unable to find " + fileName + " in the resource");
return null;
}
File txtFile = File.createTempFile("sample_" + System.currentTimeMillis(), ".txt");
FileUtils.copyInputStreamToFile(url.openStream(), txtFile);
return txtFile;
} catch (Exception e) {
logger.log("failed to load file: " + e.toString());
return null;
}
}
至于文件位置,我的res
文件夹位于src
文件夹之外。我不确定您使用什么IDE来构建Jar工件,但我使用IntelliJ IDEA。这是我的模块的屏幕截图,显示了jar包文件夹的层次结构
注意:如果您想要
InputStream
而不是File
,您可以返回url.openStream()
而不是将InputStream
转换为文件
您是否尝试过用归档资源管理器打开JAR文件以查看资源的位置,如果有的话?是的,我尝试过,该文件位于resources文件夹中。我运行了“jarxf foo.jar”
private File getSampleSourceFile(String fileName) {
try {
ClassLoader classLoader = getClass().getClassLoader();
URL url = classLoader.getResource(fileName);
if(url == null) {
logger.log("unable to find " + fileName + " in the resource");
return null;
}
File txtFile = File.createTempFile("sample_" + System.currentTimeMillis(), ".txt");
FileUtils.copyInputStreamToFile(url.openStream(), txtFile);
return txtFile;
} catch (Exception e) {
logger.log("failed to load file: " + e.toString());
return null;
}
}