从S3下载Thymeleaf模板

huangapple 未分类评论45阅读模式
英文:

Downloading Thymeleaf template from S3

问题

我目前正在将我的Thymeleaf模板存储并从S3下载。

我正在使用以下函数从S3检索模板:

public String getTemplateFile(String name, File localFile) {
    ObjectMetadata object = s3Client.getObject(new GetObjectRequest(connectionProperties.getBucket(), name), localFile);

    boolean success = localFile.exists() && localFile.canRead();

    return localFile.getPath();
}

执行此操作后,文件成功下载到所需位置。

但是,当尝试从FlyingSaucer PDF生成器访问文件时,尽管文件已经下载到FILE_LOCATION_PATH,文件不存在。(我可以打开文件...文件在那里,但函数看不到它)

String xHtmlStringDocument = convertHtmlToXhtml(templateEngine.process(FILE_LOCATION_PATH, initializeLetterHtmlTemplateContext(letter)));

我一遍又一遍地运行程序,结果相同。但是,当我停止程序然后再次运行它时,一切正常,因为上次执行的文件现在被程序识别了。

对我来说,这听起来像是异步函数的问题。

有人知道我怎么可以解决这个问题吗?

提前感谢。

编辑(根据建议进行了编辑)

新函数:结果相同:
(并且文件已创建,从S3下载成功)

public String getTemplateFileN(String name, File localFile) throws IOException {
    S3Object fullObject = null;
    InputStream in = null;
    try {
        fullObject = s3Client.getObject(new GetObjectRequest(connectionProperties.getBucket(), name));

        System.out.println("Content-Type: " + fullObject.getObjectMetadata().getContentType());
        System.out.println("Content:");
        displayTextInputStream(fullObject.getObjectContent());
        in = fullObject.getObjectContent();
        System.out.println(localFile.toPath());
        Files.copy(in, localFile.toPath());

    } finally {
        // 为了确保网络连接不保持打开状态,关闭任何打开的输入流。
        if (fullObject != null) {
            fullObject.close();
        }
        if (in != null) {
            in.close();
        }
    }

    return localFile.getPath();
}
英文:

I am currently storing and downloading my Thymeleaf templates in S3.

I am using the following function to retrieve the Template from S3:

    public String getTemplateFile(String name, File localFile) {

    ObjectMetadata object = s3Client.getObject(new GetObjectRequest(connectionProperties.getBucket(), name), localFile);

    boolean success = localFile.exists() && localFile.canRead();

    return localFile.getPath();

}

After doing this the file is successfully downloaded in the desired location.

But when trying to access the file from the FlyingSaucer PDF generator the file doesn't exist, despite it is already downloaded in FILE_LOCATION_PATH. (I can open the file... the file is there but the function doesn't see it)

 String xHtmlStringDocument =
            convertHtmlToXhtml(templateEngine
                    .process(FILE_LOCATION_PATH,
                            initializeLetterHtmlTemplateContext(letter)));

When I run the program again and again I get the same result. But when I STOP the program and RUN it AGAIN then everything works because the file form the last execution is now recognized by the program.

This sounds to me like an asynchronous function issue.

Does anybody know how can I fix this?

Thanks in advance.

EDITED (following suggestion)

New function: Same result:
(And the file was created, the Download from S3 was successful)

> java.io.FileNotFoundException: ClassLoader resource "static/templates/template.html" could not be resolved

    public String getTemplateFileN(String name, File localFile) throws IOException {
    S3Object fullObject = null;
    InputStream in = null;
    try {
        fullObject = s3Client.getObject(new GetObjectRequest(connectionProperties.getBucket(), name));

        System.out.println("Content-Type: " + fullObject.getObjectMetadata().getContentType());
        System.out.println("Content: ");
        displayTextInputStream(fullObject.getObjectContent());
        in = fullObject.getObjectContent();
        System.out.println(localFile.toPath());
        Files.copy(in, localFile.toPath());

    }   //then later
    finally {
        // To ensure that the network connection doesn't remain open, close any open input streams.
        if (fullObject != null) {
            fullObject.close();
        }
        if (in != null) {
            in.close();
        }
    }

    return localFile.getPath();

}

答案1

得分: 0

检查 Javadoc
https://docs.aws.amazon.com/AWSJavaSDK/latest/javadoc/com/amazonaws/services/s3/AmazonS3Client.html#getObject-com.amazonaws.services.s3.model.GetObjectRequest-java.io.File-

我看到没有方法签名 ObjectMetadata getObject(GetObjectRequest getObjectRequest, String file)

有一个方法是

ObjectMetadata getObject(GetObjectRequest getObjectRequest,
                                File destinationFile)

在这个方法中,你需要提供一个 File(而不是 String)作为第二个参数。确保在尝试读取文件之前,该文件没有被打开以进行写操作!

英文:

Checking javadoc
https://docs.aws.amazon.com/AWSJavaSDK/latest/javadoc/com/amazonaws/services/s3/AmazonS3Client.html#getObject-com.amazonaws.services.s3.model.GetObjectRequest-java.io.File-

I see not method signature ObjectMetadata getObject(GetObjectRequest getObjectRequest,String file)

There is

ObjectMetadata getObject(GetObjectRequest getObjectRequest,
                                File destinationFile)

Where you provide File (not String) as second argument. Make sure the file is not opened for write before you try reading it!

huangapple
  • 本文由 发表于 2020年7月23日 23:16:01
  • 转载请务必保留本文链接:https://java.coder-hub.com/63057554.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定