2

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

enter image description here

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?

0

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.