简体   繁体   English

访问我的资源文件夹中的文件 Elastic Beanstalk/Spring Boot

[英]Access Files in My Resources Folder Elastic Beanstalk/ Spring Boot

I have deployed a Spring Boot application that needs to read files stores in the resources folder in a jar and also save files while doing some processing.我已经部署了一个 Spring Boot 应用程序,它需要读取存储在 jar 中资源文件夹中的文件,并在进行一些处理时保存文件。 Trying to access my resources folder on Elastic beanstalk (t2.small) gives:尝试访问 Elastic beanstalk (t2.small) 上的资源文件夹会给出:

Input path does not exist: file:/var/app/current/resources/data/url_list1.csv输入路径不存在:file:/var/app/current/resources/data/url_list1.csv

I tried to access the file using "System.getProperty("user.dir") + "/resources/data/url_list1.csv"我尝试使用 "System.getProperty("user.dir") + "/resources/data/url_list1.csv" 访问该文件

I have tried how it is done on localhost on my system:我已经尝试过它是如何在我的系统上的本地主机上完成的:

System.getProperty("user.dir") + "/src/main/resources/data/url_list1.csv" System.getProperty("user.dir") + "/src/main/resources/data/url_list1.csv"

It is not working.它不工作。 I know a lot of people have experience in this.我知道很多人都有这方面的经验。

I found a solution:我找到了一个解决方案:

I saw this answer on where the jar files is unzipped on aws elastic beanstalk and I had to look for the file.我在 aws elastic beanstalk 上解压缩 jar 文件的位置看到了这个答案,我不得不寻找该文件。

AWS Elastic Beanstalk application folder on EC2 instance after deployed? 部署后 EC2 实例上的 AWS Elastic Beanstalk 应用程序文件夹?

find / -name YOUR_FILE.py -print find / -name YOUR_FILE.py -print
Found the file here in /tmp folder在 /tmp 文件夹中找到该文件

/tmp/eb_extracted_jar/BOOT-INF/classes/data/url_list1.csv /tmp/eb_extracted_jar/BOOT-INF/classes/data/url_list1.csv

So that is where elastic beanstalk kept the files unzipped from the jar I'm not an expert in Linux.所以这就是弹性 beanstalk 将文件从 jar 中解压缩的地方,我不是 Linux 专家。 So I had to hardcode it into the application.所以我不得不将它硬编码到应用程序中。 I don't know if there is any other way to access it dynamically from a Spring boot application with relative to the class path.我不知道是否还有其他方法可以从相对于类路径的 Spring 启动应用程序动态访问它。 I was feeding the file path to JavaSparkContext.textFile which requires a string to the file path.我正在将文件路径提供给 JavaSparkContext.textFile,它需要一个字符串到文件路径。 Meanwhile the classpath for the application defaults to /var/app/current/ when the application is running.同时,当应用程序运行时,应用程序的类路径默认为 /var/app/current/ 。

All resourses are moved to target folder.所有资源都移动到目标文件夹。 You can get the path by calling getClass().getResourceAsStream("/data/url_list1.csv");您可以通过调用getClass().getResourceAsStream("/data/url_list1.csv");获取路径getClass().getResourceAsStream("/data/url_list1.csv");

Using Spring Boot Elastic Beanstalk and OpenPDF (itext open source version), here is how i got it to work使用 Spring Boot Elastic Beanstalk 和 OpenPDF(itext 开源版本),这是我让它工作的方式

Resource input = new ClassPathResource("assets/Logo.png");
Image freightOneLogo=Image.getInstance(input.getURI().toString());

I tried several other ways but could not get it to work.我尝试了其他几种方法,但无法使其正常工作。
The file is located in "resources/assets/Logo.png"该文件位于“resources/assets/Logo.png”

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM