azure - 我如何从java spring-boot AZURE函数中的资源文件夹中读取任何文件?

标签 azure azure-function-app azure-spring-boot

我正在使用 Spring Boot 编写一个 Azure 函数,我想读取资源文件夹中的文件,但每次都会出现空指针异常。 请帮助我解决这个问题? 要么我必须将文件放入 blob 存储中,要么 azure 函数中有其他东西可以使用 java spring boot 应用程序读取文件?

下面是我的处理程序类:

公共(public)类 FunctionHandler 扩展了 AzureSpringBootRequestHandler {

    @FunctionName("generateSignature")
    public HttpResponseMessage execute(@HttpTrigger(name = "request", methods = { HttpMethod.GET,
                    HttpMethod.POST }, authLevel = AuthorizationLevel.ANONYMOUS) HttpRequestMessage<Optional<User>> request,
                    ExecutionContext context) throws InvalidKeyException, NoSuchAlgorithmException,
                    InvalidKeySpecException, SignatureException, IOException {
            User user = request.getBody().filter((u -> u.getName() != null)).orElseGet(() -> new User(request
                            .getQueryParameters()
                            .getOrDefault("name", "<no name supplied> please provide a name as "
                                            + "either a query string parameter or in a POST body")));
            context.getLogger().info("Greeting user name: " + user.getName());

            try {
                    InputStream resourceInputStream = new FileInputStream(
                                    FunctionHandler.class.getClassLoader().getResource("").getPath()
                                                    + "../../src/main/java/com/tomtom/resources/private_key.der");

                    DataInputStream dis = new DataInputStream(resourceInputStream);
                    byte[] privateBytes = new byte[resourceInputStream.available()];

                    dis.readFully(privateBytes);
                    dis.close();

                    PKCS8EncodedKeySpec privSpec = new PKCS8EncodedKeySpec(privateBytes);
                    KeyFactory keyFactory = KeyFactory.getInstance("RSA");
                    RSAPrivateKey privateKey = (RSAPrivateKey) keyFactory.generatePrivate(privSpec);

                    Signature s = Signature.getInstance("SHA256withRSA");
                    s.initSign(privateKey);
                    s.update(user.getName().getBytes("UTF-8"));
                    byte[] binarySignature = s.sign();
                    String signature = DatatypeConverter.printBase64Binary(binarySignature);
                    context.getLogger().info("sign is: " + signature);

            } catch (IOException e) {
                    e.printStackTrace();
            }

            return request.createResponseBuilder(HttpStatus.OK).body(handleRequest(user, context))
                            .header("Content-Type", "application/json").build();
    }

}

my project structure

最佳答案

尝试使用以下代码:

InputStream resourceInputStream = new FileInputStream(HelloFunction.class.getClassLoader().getResource("").getPath()+"../../src/main/java/com/example/resources/hello.json");

我的源代码结构如下:

enter image description here

<小时/>

更新:

String pathToResources = "hello.json";
// this is the path within the jar file
InputStream input = HelloHandler.class.getResourceAsStream("/resources/" + pathToResources);

// here is inside IDE
if (input == null) {
    input = HelloHandler.class.getClassLoader().getResourceAsStream(pathToResources);
}

// convert InputStream to String
ByteArrayOutputStream result = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int length;
while ((length = input.read(buffer)) != -1) {
    result.write(buffer, 0, length);
}

System.out.println(result.toString("UTF-8"));

enter image description here

关于azure - 我如何从java spring-boot AZURE函数中的资源文件夹中读取任何文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64963590/

相关文章:

spring-boot - 使用 Spring 设置 Azure 客户端凭据流

javascript - Javascript/TypeScript 中的 Azure 函数 : How to DI?

azure - 如何在 Azure Spring 应用程序的 Azure 日志中查看原始文本日志?

azure - 如何使用 Azure 数据工厂管道中的服务主体连接到 SSIS 中的 Azure SQL 托管实例?

azure - 使用 azure 函数应用程序 VNET 集成将 JSON 发送到 privateEndpoint azure

docker - 在 Azure 函数中从 ACR 提取图像时出错

python - “func”不被识别为内部或外部命令、可操作程序或批处理文件

azure - 如何处理 Azure VM 中 Terraform 中的多个 storage_os_disk block ?

Azure 交换槽,当前处理请求已完成?