如何在Jasper报告中显示Amazon S3图像

发布于 2025-01-22 05:11:17 字数 325 浏览 2 评论 0 原文

当我使用Jasper-Reports使用本地图像路径时,我可以将图像显示为PDF,但是我需要从Amazon S3获取图像,如何将Amazon S3 Image显示为Java的PDF?我应该先从Amazons3下载图像吗?还是链接Jasper报告中的完整图像路径? 例如,我通过jasper报告将本地图像路径链接在一起,如果我想从Amazons3获取图像,我该怎么做?请我。

<imageExpression class="java.lang.String"><![CDATA["image_name.jpg"]]></imageExpression>

i can display image to pdf when i use local image path with jasper-reports, but i need to get the image from amazon S3, how can i display amazon S3 image to pdf by java? should i download the image from amazonS3 first? or link the full image path in jasper report?
for example, i linked the local image path by jasper-reports, if i want to get the image from amazonS3 , how can i do that? please hlpe me.

<imageExpression class="java.lang.String"><![CDATA["image_name.jpg"]]></imageExpression>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

嘿看小鸭子会跑 2025-01-29 05:11:17

您可以使用AWS Java SDK生成S3图像对象的预先签名的URL,并在Jasper报告中使用预签名的URL。这样,您就不必从S3下载图像。请注意,可以从Java设置的预先签名的有效期。

以下是用于引用的代码段以生成S3预签名的URL。

此链接中的更多详细信息。

public class GenerateS3SignedUrl implements BiFunction<String, String, String> {
    @Override
    @SneakyThrows
    public String apply(String bucketName, String objectKey) {
        String awsRegion = StringUtils.isEmpty(System.getenv(Constant.REGION)) ? Constant.DEFAULT_REGION :
                System.getenv(Constant.REGION);

        log.info("calculating expiration hrs defaults to 2 hrs");
        int expirationHrs;
        if (StringUtils.isEmpty(System.getenv(Constant.EXPIRATION_DURATION))) {
            expirationHrs = 2;
        } else if (!StringUtils.isNumeric(System.getenv(Constant.EXPIRATION_DURATION))) {
            expirationHrs = 2;
        } else {
            expirationHrs = Integer.parseInt(System.getenv(Constant.EXPIRATION_DURATION));
        }

        long expirationInMillis = 1000L * 60 * 60 * expirationHrs;
        log.info("create pre-signed url generate request..");
        GeneratePresignedUrlRequest generatePresignedUrlRequest =
                new GeneratePresignedUrlRequest(bucketName, objectKey)
                        .withMethod(HttpMethod.GET)
                        .withExpiration(Date.from(Instant.now().plusMillis(expirationInMillis)));

        log.info("generate pre-signed url..");
        URL preSignedUrl = AwsCommonConfig.getAmazonS3Client(awsRegion)
                .generatePresignedUrl(generatePresignedUrlRequest);

        log.info("return pre-signed url for file : {} with expiration in {} hrs.", objectKey, expirationHrs);
        return preSignedUrl.toString();
    }
}    
public class AwsCommonConfig {
    private static AmazonS3 amazonS3;

    public static AmazonS3 getAmazonS3Client (String awsRegion) {
        if (amazonS3 == null) {
            amazonS3 = AmazonS3ClientBuilder.standard()
                    .withRegion(awsRegion)
                    .withPathStyleAccessEnabled(true)
                    .build();
        }
        return amazonS3;
    }

}

You can generate a pre-signed URL of the S3 image object using AWS Java SDK and use the pre-signed URL in the jasper-reports. In this way, you don't have to download the image from S3. Please note that there is an expiry time for the pre-signed which can be set from java.

Below is the code snippet for reference to generate S3 pre-signed URL.

More details in this link. https://docs.aws.amazon.com/AmazonS3/latest/userguide/ShareObjectPreSignedURL.html

public class GenerateS3SignedUrl implements BiFunction<String, String, String> {
    @Override
    @SneakyThrows
    public String apply(String bucketName, String objectKey) {
        String awsRegion = StringUtils.isEmpty(System.getenv(Constant.REGION)) ? Constant.DEFAULT_REGION :
                System.getenv(Constant.REGION);

        log.info("calculating expiration hrs defaults to 2 hrs");
        int expirationHrs;
        if (StringUtils.isEmpty(System.getenv(Constant.EXPIRATION_DURATION))) {
            expirationHrs = 2;
        } else if (!StringUtils.isNumeric(System.getenv(Constant.EXPIRATION_DURATION))) {
            expirationHrs = 2;
        } else {
            expirationHrs = Integer.parseInt(System.getenv(Constant.EXPIRATION_DURATION));
        }

        long expirationInMillis = 1000L * 60 * 60 * expirationHrs;
        log.info("create pre-signed url generate request..");
        GeneratePresignedUrlRequest generatePresignedUrlRequest =
                new GeneratePresignedUrlRequest(bucketName, objectKey)
                        .withMethod(HttpMethod.GET)
                        .withExpiration(Date.from(Instant.now().plusMillis(expirationInMillis)));

        log.info("generate pre-signed url..");
        URL preSignedUrl = AwsCommonConfig.getAmazonS3Client(awsRegion)
                .generatePresignedUrl(generatePresignedUrlRequest);

        log.info("return pre-signed url for file : {} with expiration in {} hrs.", objectKey, expirationHrs);
        return preSignedUrl.toString();
    }
}    
public class AwsCommonConfig {
    private static AmazonS3 amazonS3;

    public static AmazonS3 getAmazonS3Client (String awsRegion) {
        if (amazonS3 == null) {
            amazonS3 = AmazonS3ClientBuilder.standard()
                    .withRegion(awsRegion)
                    .withPathStyleAccessEnabled(true)
                    .build();
        }
        return amazonS3;
    }

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文