gpt4 book ai didi

java - 如何从aws lambda java中的类路径读取文件

转载 作者:搜寻专家 更新时间:2023-11-01 03:18:28 25 4
gpt4 key购买 nike

当我尝试执行我的 aws lambda 函数时,我看到了这个错误

1) Error injecting constructor, java.lang.NullPointerException
at in.nikhilbhardwaj.path.route.resources.ServicesResource.<init>(ServicesResource.java:66)
at in.nikhilbhardwaj.path.route.resources.ServicesResource.class(ServicesResource.java:56)
while locating in.nikhilbhardwaj.path.route.resources.ServicesResource
for parameter 0 at in.nikhilbhardwaj.path.alexa.intent.HelloWorldIntentAction.<init>(HelloWorldIntentAction.java:44)
while locating in.nikhilbhardwaj.path.alexa.intent.HelloWorldIntentAction
while locating in.nikhilbhardwaj.path.alexa.intent.IntentAction annotated with @com.google.inject.multibindings.Element(setName=,uniqueId=10, type=MAPBINDER, keyType=java.lang.String)
at in.nikhilbhardwaj.path.alexa.intent.IntentModule.configure(IntentModule.java:17) (via modules: in.nikhilbhardwaj.path.alexa.AlexaStarterApplicationModule -> in.nikhilbhardwaj.path.alexa.intent.IntentModule -> com.google.inject.multibindings.MapBinder$RealMapBinder)
while locating java.util.Map<java.lang.String, in.nikhilbhardwaj.path.alexa.intent.IntentAction>
for parameter 0 at in.nikhilbhardwaj.path.alexa.intent.IntentHandlerServiceImpl.<init>(IntentHandlerServiceImpl.java:16)
while locating in.nikhilbhardwaj.path.alexa.intent.IntentHandlerServiceImpl
while locating in.nikhilbhardwaj.path.alexa.intent.IntentHandlerService
for parameter 0 at in.nikhilbhardwaj.path.alexa.AlexaStarterSpeechlet.<init>(AlexaStarterSpeechlet.java:26)
while locating in.nikhilbhardwaj.path.alexa.AlexaStarterSpeechlet
Caused by: java.lang.NullPointerException
at java.io.Reader.<init>(Reader.java:78)
at java.io.InputStreamReader.<init>(InputStreamReader.java:113)
at in.nikhilbhardwaj.path.route.resources.ServicesResource.initializeServiceIds(ServicesResource.java:88)
at in.nikhilbhardwaj.path.route.resources.ServicesResource.<init>(ServicesResource.java:68)
at in.nikhilbhardwaj.path.route.resources.ServicesResource$$FastClassByGuice$$3d6e91ec.newInstance(<generated>)

我修改了我的代码以从 classpath 而不是文件系统中读取,方法是遵循 this question

这是抛出异常的行(ServicesResource.java 中的第 88 行)-

Iterable<CSVRecord> records = CSVFormat.RFC4180.withFirstRecordAsHeader()
.parse(new InputStreamReader(this.getClass().getResourceAsStream(RESOURCES_ROOT + RESOURCE_CALENDAR), StandardCharsets.UTF_8));

这些是我正在使用的常量 -

public static final String RESOURCE_CALENDAR = "calendar.txt";
public static final String RESOURCES_ROOT = "/path-nj-us/";

我认为我已经正确打包了我上传到 aws lambda 的代码,当我删除这些引用时它可以正常工作。这是 zip 包含的内容(我省略了不相关的文件)-

➜  distributions git:(master) ✗ jar tf path-alexa-skills-kit.zip
in/
in/nikhilbhardwaj/
in/nikhilbhardwaj/path/
in/nikhilbhardwaj/path/route/
in/nikhilbhardwaj/path/alexa/
in/nikhilbhardwaj/path/alexa/AlexaStarterApplicationModule.class
in/nikhilbhardwaj/path/alexa/AlexaStarterSpeechlet.class
in/nikhilbhardwaj/path/alexa/AlexaStarterSpeechletRequestStreamHandler.class
in/nikhilbhardwaj/path/route/model/Constants.class
in/nikhilbhardwaj/path/route/resources/
in/nikhilbhardwaj/path/route/resources/ServicesResource.class
in/nikhilbhardwaj/path/route/resources/StopsResource.class
in/nikhilbhardwaj/path/route/resources/StopTimesResource.class
in/nikhilbhardwaj/path/route/resources/TripsResource.class
log4j.properties
path-nj-us/
path-nj-us/agency.txt
path-nj-us/calendar.txt
path-nj-us/calendar_dates.txt
path-nj-us/fare_attributes.txt
path-nj-us/fare_rules.txt
path-nj-us/feed_info.txt
path-nj-us/frequencies.txt
path-nj-us/route_directions.txt
path-nj-us/routes.txt
path-nj-us/shapes.txt
path-nj-us/stop_times.txt
path-nj-us/stops.txt
path-nj-us/timetable_stop_order-new.txt
path-nj-us/timetables-new.txt
path-nj-us/transfers.txt
path-nj-us/trips.txt

我非常感谢任何指出这里缺少什么的指示或进一步调试它的一般提示。这是我编写的第一个 aws lambda 函数,因此我完全有可能忽略了文档中的一些重要内容或者不知道要查找什么。

我添加了一些调试代码并运行它,看来资源不可用-

URL r = this.getClass().getClassLoader().getResource("path-nj-us/calendar.txt");
if (r != null && r.getFile() != null) {
System.out.println("Resource file name" + r.getFile());
} else {
System.out.println("Resource not found in classpath");
}

这会打印出 Resource not found in classpath。我还尝试了 this.getClass().getResource() 不变量,结果相同。

我将尝试在 aws 论坛 上发帖,看看是否存在这种行为的已知原因,或者是否不允许从类路径读取文件。

最佳答案

awsLambda 用户代码仅访问两个目录。

  1. /变量/任务
  2. /tmp

all lambda functions keep the source code on /var/task

在您的代码中,使用此路径中的任何一个,而不是当前目录路径 "path-nj-us"。像这样

URL r = this.getClass().getClassLoader().getResource("/tmp/calendar.txt");
if (r != null && r.getFile() != null) {
System.out.println("Resource file name" + r.getFile());
} else {
System.out.println("Resource not found in classpath");
}

关于java - 如何从aws lambda java中的类路径读取文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39380810/

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