如何读取用Java编写的AWS Lambda函数中的文件? [英] How to read a file in AWS Lambda Function written in Java ?
问题描述
我编写了一个AWS Lambda Handler,如下所示:
package com.lambda;
import com.amazonaws.services.lambda.runtime.Context;
import com.amazonaws.services.lambda.runtime.LambdaLogger;
import com.amazonaws.services.lambda.runtime.RequestStreamHandler;
import java.io. *;
公共类TestDetailsHandler实现RequestStreamHandler {
public void handleRequest(InputStream input,OutputStream output,Context context){
//获取Lambda Logger
LambdaLogger logger = context.getLogger();
//接收来自Inputstream的输入抛出异常,如果有任何
文件起始=新文件(System.getProperty(user.dir));
System.out.println(源位置+开始);
文件cityFile = new文件(起始+City.db);
FileInputStream fis = null;
try {
fis = new FileInputStream(cityFile);
System.out.println(要读取的文件总大小(以字节为单位):
+ fis.available());
int content;
while((content = fis.read())!= -1){
//转换为char并显示它
System.out.print((char)content);
}
} catch(IOException e){
e.printStackTrace();
} finally {
try {
if(fis!= null)
fis.close();
} catch(IOException ex){
ex.printStackTrace();
}
}
}
}
它读取了一个文件:City.db,在资源文件夹中可用,即使我一直到处都看到如下:
Pom.xml文件的内容:
<?xml version =1.0encoding =UTF-8?>
< project xmlns =http://maven.apache.org/POM/4.0.0
xmlns:xsi =http://www.w3.org/2001/XMLSchema-instance
xsi:schemaLocation =http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd\">
< modelVersion> 4.0.0< / modelVersion>
< groupId> com.lambda< / groupId>
< artifactId> testdetails< / artifactId>
< version> 1.0-SNAPSHOT< / version>
< packaging> jar< / packaging>
< name> test-handler< / name>
< dependencies>
< dependency>
< groupId> com.amazonaws< / groupId>
< artifactId> aws-lambda-java-core< / artifactId>
< version> 1.1.0< / version>
< / dependency>
<依赖>
< groupId> junit< / groupId>
< artifactId> junit< / artifactId>
< version> 4.11< / version>
< scope> test< / scope>
< / dependency>
< / dependencies>
< build>
< plugins>
< plugin>
< groupId> org.apache.maven.plugins< / groupId>
< artifactId> maven-shade-plugin< / artifactId>
< version> 2.3< / version>
< configuration>
< createDependencyReducedPom> false< / createDependencyReducedPom>
< / configuration>
< executions>
< execution>
< phase> package< / phase>
< goals>
< goal> shade< / goal>
< / goals>
< / execution>
< / executions>
< / plugin>
< / plugins>
< / build>
< / project>
我已经使用各种方法来保存文件,但最后它无法正常工作。请你告诉我这里有什么问题?
然而在我的另一个项目中,我将xyz.properties文件保存在resources文件夹中并从PropertyManager文件中读取,工作正常。当我在我的系统上测试它工作正常,但在AWS Lambda函数上它不起作用。
我在代码中进行了以下更改,现在它的工作非常完美:
主要更改以下两行:
ClassLoader classLoader = getClass()。getClassLoader();
文件cityFile =新文件(classLoader.getResource(City.db)。getFile());
package com.lambda;
import com.amazonaws.services.lambda.runtime.Context;
import com.amazonaws.services.lambda.runtime.LambdaLogger;
import com.amazonaws.services.lambda.runtime.RequestStreamHandler;
import java.io. *;
公共类TestDetailsHandler实现RequestStreamHandler {
public void handleRequest(InputStream input,OutputStream output,Context context){
//获取Lambda Logger
LambdaLogger logger = context.getLogger();
//如果任何
ClassLoader classLoader = getClass()。getClassLoader();接收来自Inputstream抛出异常的输入。
文件cityFile = new File(classLoader.getResource(City.db)。getFile());
FileInputStream fis = null;
try {
fis = new FileInputStream(cityFile);
System.out.println(要读取的文件总大小(以字节为单位):
+ fis.available());
int content;
while((content = fis.read())!= -1){
//转换为char并显示它
System.out.print((char)content);
}
} catch(IOException e){
e.printStackTrace();
} finally {
try {
if(fis!= null)
fis.close();
} catch(IOException ex){
ex.printStackTrace();
}
}
}
I have written an AWS Lambda Handler as below :
package com.lambda;
import com.amazonaws.services.lambda.runtime.Context;
import com.amazonaws.services.lambda.runtime.LambdaLogger;
import com.amazonaws.services.lambda.runtime.RequestStreamHandler;
import java.io.*;
public class TestDetailsHandler implements RequestStreamHandler {
public void handleRequest(InputStream input,OutputStream output,Context context){
// Get Lambda Logger
LambdaLogger logger = context.getLogger();
// Receive the input from Inputstream throw exception if any
File starting = new File(System.getProperty("user.dir"));
System.out.println("Source Location" + starting);
File cityFile = new File(starting + "City.db");
FileInputStream fis = null;
try {
fis = new FileInputStream(cityFile);
System.out.println("Total file size to read (in bytes) : "
+ fis.available());
int content;
while ((content = fis.read()) != -1) {
// convert to char and display it
System.out.print((char) content);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (fis != null)
fis.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
}
Its read a file : City.db , available in resources folder, even I kept to everywhere see below :
But it showing following message on execution of this lambda function :
START RequestId: 5216ea47-fc43-11e5-96d5-83c1dcdad75d Version: $LATEST
Source Location/
java.io.FileNotFoundException: /city.db (No such file or directory)
at java.io.FileInputStream.open0(Native Method)
at java.io.FileInputStream.open(FileInputStream.java:195)
at java.io.FileInputStream.<init>(FileInputStream.java:138)
at com.lambda.TestDetailsHandler.handleRequest(TestDetailsHandler.java:26)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at lambdainternal.EventHandlerLoader$StreamMethodRequestHandler.handleRequest(EventHandlerLoader.java:511)
at lambdainternal.EventHandlerLoader$2.call(EventHandlerLoader.java:972)
at lambdainternal.AWSLambda.startRuntime(AWSLambda.java:231)
at lambdainternal.AWSLambda.<clinit>(AWSLambda.java:59)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:348)
at lambdainternal.LambdaRTEntry.main(LambdaRTEntry.java:93)
END RequestId: 5216ea47-fc43-11e5-96d5-83c1dcdad75d
REPORT RequestId: 5216ea47-fc43-11e5-96d5-83c1dcdad75d Duration: 58.02 ms Billed Duration: 100 ms Memory Size: 1024 MB Max Memory Used: 50 MB
Contents of the Pom.xml file :
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.lambda</groupId>
<artifactId>testdetails</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<name>test-handler</name>
<dependencies>
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-lambda-java-core</artifactId>
<version>1.1.0</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>2.3</version>
<configuration>
<createDependencyReducedPom>false</createDependencyReducedPom>
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
I have used various ways to keep file here and there , but at the end its not working. May you please let me know what is wrong here ?
However in my another project where I have kept xyz.properties file in resources folder and reading from a PropertyManager file, its working fine. When I tested it on my system its working fine, but on AWS Lambda function it doesn't work.
I have made following changes in my code and now its works perfect :
Majorly changed following two lines :
ClassLoader classLoader = getClass().getClassLoader();
File cityFile = new File(classLoader.getResource("City.db").getFile());
package com.lambda;
import com.amazonaws.services.lambda.runtime.Context;
import com.amazonaws.services.lambda.runtime.LambdaLogger;
import com.amazonaws.services.lambda.runtime.RequestStreamHandler;
import java.io.*;
public class TestDetailsHandler implements RequestStreamHandler {
public void handleRequest(InputStream input,OutputStream output,Context context){
// Get Lambda Logger
LambdaLogger logger = context.getLogger();
// Receive the input from Inputstream throw exception if any
ClassLoader classLoader = getClass().getClassLoader();
File cityFile = new File(classLoader.getResource("City.db").getFile());
FileInputStream fis = null;
try {
fis = new FileInputStream(cityFile);
System.out.println("Total file size to read (in bytes) : "
+ fis.available());
int content;
while ((content = fis.read()) != -1) {
// convert to char and display it
System.out.print((char) content);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (fis != null)
fis.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
这篇关于如何读取用Java编写的AWS Lambda函数中的文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!