I am trying to read an image and text files and upload to aws s3 bucket
using nodejs fs module
. I am not using any express server, but plain javascript that calls aws-sdk and uploads items to aws-s3
.
Here is how my project structure looks like
Inside s3.js I am trying to read the 2.png and friends.json files, s3.js
const fs = require('fs');
const file = fs.readFileSync('../public/2.png', (err)=>console.log(err.message));
But this throw and error
Error: ENOENT: no such file or directory, open '../public/2.png'
What could be going wrong?
Could always try an absolute path instead of a relative and add encoding:
const fs = require('fs')
const path = require('path')
const image = path.join(__dirname, '../public/2.png')
const file = fs.readFileSync(image, {encoding:'utf8'})
Could also use upng-js with a Promise
const png = require("upng-js")
const fs = require('fs')
async function pngCode(img) {
try {
return png.decode(await fs.promise.promisify(fs.readFile)(img)
} catch (err) {
console.error(err)
}
}
pngCode('.../public/2.png')
None of the code is tested wrote on my phone.