你好,我想使用deno下载pdf文件。
我正在使用
export default async function downloadPDF(uid: string): Promise<void> {
const res = await fetch(`${Deno.env.get('MY_PDF_URL')}/${uid}`);
const file = await Deno.open(`./pdfs/${uid}.pdf`, { create: true, write: true })
if (res?.body) {
for await(const chunk of res.body) {
await Deno.writeAll(file, chunk);
}
}
file.close();
}我使用的命令是deno run --allow-net --allow-read --allow-write --allow-env --allow-run main.ts。所以我不认为我在这里放了正确的权限。
然而,我得到了错误
error: Uncaught (in promise) PermissionDenied: Permission denied (os error 13)
api_1 | const file = await Deno.open(`./pdfs/${uid}.pdf`, { create: true, write: true })
api_1 | ^
api_1 | at unwrapOpResult (deno:core/core.js:100:13)
api_1 | at async Object.open (deno:runtime/js/40_files.js:46:17)
api_1 | at async downloadPDF (file:///app/downloadPDF.ts:3:17)这里的问题可能是什么?我正在使用Dockerfile
FROM hayd/alpine-deno:1.9.0
EXPOSE 1993
WORKDIR /app
USER deno
COPY deps.ts .
RUN deno cache deps.ts
COPY . .
RUN deno cache main.ts
CMD [run --allow-net --allow-read --allow-write --allow-env --allow-run main.ts]来运行此应用程序。
发布于 2021-04-20 16:48:40
import { readerFromStreamReader } from "https://deno.land/std@0.93.0/io/mod.ts";
export default async function downloadPDF(uid: string): Promise<void> {
const res = await fetch(`https://example.com/pdfs/${uid}`);
if (res.status !== 200) {
console.error(`response status was ${res.status}, this is not handled.`);
return;
}
// we don't want to download existing files
if (await fileExists(`/html/${uid}.html`)) {
console.info(`${uid}.html already exists, skipping...`);
return;
}
const file = await Deno.open(`/pdfs/${uid}.pdf`, { create: true, write: true, read: true })
if (res?.body) {
const reader = readerFromStreamReader(res.body.getReader());
await Deno.copy(reader, file);
}
file.close();
}我在问题中发布的错误是由于我的Dockerfile中的deno用户的文件权限造成的。只需删除用户即可解决问题。但是,如果将其用于生产,则正确设置权限会更好。
https://stackoverflow.com/questions/67163132
复制相似问题