Reading a file in Adobe IO runtime project | Community
Skip to main content
Level 7
January 20, 2025
Solved

Reading a file in Adobe IO runtime project

  • January 20, 2025
  • 1 reply
  • 535 views
Hello Team,
I am writing Nodejs actions, utility for Adobe IO runtime project.
Here, I have kept the mail templates under config folder. My logic has to read the config folder based on input condition.
While searcing, I got below details to read the file
 
const templateLoader = new TemplateLoader(path.join(__dirname, '../mail-templates'));
 
 // Check if country template exists
        if (fs.existsSync(countryPath)) {
            return fs.readFileSync(countryPath, 'utf-8');
        }
 
Also, I need to write below mentioned script in package.json file
Hello Team,
 "scripts": {
"build": "cp -R src/actions/mail-templates dist/mail-templates"
 }
 
Note: cp is a unix command. Was, not able to test in my local windows machine.
Was facing issues. Then, again I got solution that to use Nodejs specific commands.
So, I am bit sceptical about reading the file in serverless microservices. (In  my case, Adobe IO runtime)
Can someone guide me on "Best way to read the file in Adobe IO runtime project?
 
Thanks in advance 
 
This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
Best answer by AmitVishwakarma
To read files in Adobe I/O Runtime:

For local testing: Use fs.readFileSync() as you did, but note this won't work in the serverless environment.

In Adobe I/O Runtime:
Store templates in cloud storage (e.g., AWS S3, Adobe Cloud) or deploy them alongside your app.
Access files via API calls to cloud storage instead of local file system calls.

Cross-platform file copying: Replace the Unix cp command in package.json with Node.js fs-extra for cross-platform compatibility:

"scripts": {
"build": "node -e \"require('fs-extra').copySync('src/actions/mail-templates', 'dist/mail-templates')\""
}

This approach ensures your solution works both locally and in the serverless Adobe I/O Runtime.

1 reply

AmitVishwakarma
Community Advisor
AmitVishwakarmaCommunity AdvisorAccepted solution
Community Advisor
January 20, 2025
To read files in Adobe I/O Runtime:

For local testing: Use fs.readFileSync() as you did, but note this won't work in the serverless environment.

In Adobe I/O Runtime:
Store templates in cloud storage (e.g., AWS S3, Adobe Cloud) or deploy them alongside your app.
Access files via API calls to cloud storage instead of local file system calls.

Cross-platform file copying: Replace the Unix cp command in package.json with Node.js fs-extra for cross-platform compatibility:

"scripts": {
"build": "node -e \"require('fs-extra').copySync('src/actions/mail-templates', 'dist/mail-templates')\""
}

This approach ensures your solution works both locally and in the serverless Adobe I/O Runtime.
Level 7
January 20, 2025

Hi @amitvishwakarma 

Thanks for your reply.

I am doing file - Read only operation. Actions are kept under: /src/actions/
Mail templates are kept under: /src/mail-templates/ I hope, this is fine.

Now, How can I access the file in local as well in serverless Adobe I/O Runtime ?

 

For testing purpose, manually I have kept the mail-templates folder under dist folder. Still, logic is not able to read the file in local.  Am I missing anything?