I made an app that download images and save them to a directory called storage
. everything works great in development but in container I got error:
{"errno":-2,"code":"ENOENT","syscall":"open","path":"/usr/src/app/src/storage/samsung-Galaxy-Note20-Ultra-thumbnail.jpg","level":"error","service":"user-service","timestamp":"2021-05-20T20:27:42.545Z","message":"ENOENT: no such file or directory, open '/usr/src/app/src/storage/samsung-Galaxy-Note20-Ultra-thumbnail.jpg'","stack":"Error: ENOENT: no such file or directory, open '/usr/src/app/src/storage/samsung-Galaxy-Note20-Ultra-thumbnail.jpg'"}
and this is my download code:
for (const [i, product] of products.entries()) {
const { thumbnail, name } = product;
const ext = thumbnail.slice(thumbnail.lastIndexOf("."));
const filePath = `${path.resolve("./src/storage")}/${
brand.text
}-${slugify(name)}-thumbnail${ext}`;
try {
await download({
url: thumbnail,
path: filePath,
});
products[i].thumbnail = filePath.slice(filePath.indexOf("src"));
logger.info(`"${name}" thumbnail saved to ${filePath}`);
} catch (error) {
logger.error(error);
}
}
this code use path
node js module and this is download function:
const { createWriteStream } = require("fs");
const { pipeline } = require("stream");
const { promisify } = require("util");
const fetch = require("node-fetch");
const download = async ({ url, path }) => {
const streamPipeline = promisify(pipeline);
const response = await fetch(url);
if (!response.ok) {
throw new Error(`unexpected response ${response.statusText}`);
}
await streamPipeline(response.body, createWriteStream(path));
};
This is my folder structure:
and my docker volume in docker-comspoe.yml:
volumes:
- ./storage:/src/storage
and working dir in my dockerfile:
WORKDIR /usr/src/app
I don't know What causes this error.