【发布时间】:2021-05-21 09:04:40
【问题描述】:
我制作了一个下载图像并将其保存到名为storage 的目录的应用程序。在开发中一切正常,但在容器中我得到了错误:
{"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: 没有这样的文件或目录,打开'/usr/src/app/src/storage/samsung-Galaxy-Note20-Ultra-thumbnail.jpg'","stack":"错误:ENOENT:没有这样的文件或目录,打开'/usr /src/app/src/storage/samsung-Galaxy-Note20-Ultra-thumbnail.jpg'"}
这是我的下载代码:
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);
}
}
此代码使用path node js 模块,这是下载功能:
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));
};
这是我的文件夹结构:
还有我在 docker-comspoe.yml 中的 docker 卷:
volumes:
- ./storage:/src/storage
和我的 dockerfile 中的工作目录:
WORKDIR /usr/src/app
我不知道是什么导致了这个错误。
【问题讨论】:
-
这是 Windows/MacOS 吗?
-
Linux VPS上MacOS生产开发
标签: javascript node.js docker-api