AWS Lambda Java如何读取属性文件

gre*_*reg 5 java amazon-web-services aws-lambda

我尝试将属性从文件加载到 Properties 类中。我希望这个解决方案能够工作: How to load property file from classpath in AWS lambda java

\n\n

我有一个带有很少静态方法的类,我想将其用作配置持有者。里面有这么一行

\n\n
final InputStream inputStream = \nConfig.class.getClass().getResourceAsStream("/application-main.properties");\n
Run Code Online (Sandbox Code Playgroud)\n\n

并且它总是返回 null。我下载了 Lambda 使用的 zip 包,该文件位于根目录中。尽管如此,还是不​​行。

\n\n

有人有类似的问题吗?

\n\n

编辑:\n配置文件在这里:

\n\n
project\n\xe2\x94\x94\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80src\n\xe2\x94\x82   \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80main\n\xe2\x94\x82      \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80resources\n\xe2\x94\x82            application-main.properties\n
Run Code Online (Sandbox Code Playgroud)\n\n

编辑:\n我的“临时”解决方法如下所示:

\n\n
// LOAD PROPS FROM CLASSPATH...\ntry (InputStream is = Config.class.getResourceAsStream(fileName)) {\n        PROPS.load(is);\n    } catch (IOException|NullPointerException exc) {\n        // ...OR FROM FILESYSTEM\n        File file = new File(fileName);\n        try (InputStream is = new FileInputStream(file)) {\n            PROPS.load(is);\n        } catch (IOException exc2) {\n            throw new RuntimeException("Could not read properties file.");\n        }\n    }\n
Run Code Online (Sandbox Code Playgroud)\n\n

在测试期间,它从类路径读取,在 AWS Lambda 运行时部署后,它使用文件系统。为了识别文件,我使用了 env 变量:

\n\n
fileName = System.getenv("LAMBDA_TASK_ROOT")  + "/application-main.properties";\n
Run Code Online (Sandbox Code Playgroud)\n\n

但我宁愿只使用类路径而不解决这个问题。

\n

Tom*_*elo 0

假设您有以下文件夹结构:

\n\n

\r\n
\r\n
project\r\n\xe2\x94\x94\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80src\r\n\xe2\x94\x82   \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80main\r\n\xe2\x94\x82      \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80resources\r\n\xe2\x94\x82            config.properties
Run Code Online (Sandbox Code Playgroud)\r\n
\r\n
\r\n

\n\n

还有你的 Lambda 处理程序:

\n\n

\r\n
\r\n
ClassLoader loader = Config.class.getClassLoader();\r\nInputStream input = loader.getResourceAsStream("config.properties");
Run Code Online (Sandbox Code Playgroud)\r\n
\r\n
\r\n

\n\n

这可能有效...

\n