如何从aws lambda java

时间:2016-09-08 00:20:28

标签: java classpath aws-lambda

我尝试执行aws lambda function

时发现此错误
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>)

我已修改我的代码,以便按照this question

classpath而非文件系统中读取

这是抛出异常的行(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 forums以查看是否存在此行为的已知原因,或者是否禁止从类路径中读取文件。

1 个答案:

答案 0 :(得分:1)

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

  1. / var / task
  2. / tmp
  3.   

    所有lambda函数都将源代码保存在/ var / task

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

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