vips_realpath和vips__file_open_write:无法形成文件名 UNIX 错误 Firebase



我正在尝试使用sharp将base_64图像上传到Firebase云函数。 我有以下代码:

// Create image of question
export async function drawQuestionImage(question: any): Promise<any> {
const canvas = createCanvas(200, 200)
const ctx = canvas.getContext('2d')
// Write "Awesome!"
ctx.font = '30px Impact'
ctx.rotate(0.1)
ctx.fillText('Awesome!', 50, 100)
// Draw line under text
const text = ctx.measureText('Awesome!')
ctx.strokeStyle = 'rgba(0,0,0,0.5)'
ctx.beginPath()
ctx.lineTo(50, 102)
ctx.lineTo(50 + text.width, 102)
ctx.stroke()
// Draw cat with lime helmet
await loadImage(`${process.cwd()}/backgrounds/bg.jpg`).then(async (image) => {
ctx.drawImage(image, 50, 0, 70, 70)
//console.log('<img src="' + canvas.toDataURL() + '" />')
const base64 = canvas.toDataURL().replace(/^data:image/w+;base64,/, "");;
if (!base64 || !question) {
return null;
}
const filePath = `questions/${question.id}`;
const filename = `${question.id}.png`;
// Resize source image
await sharp(Buffer.from(base64.toString(), 'base64'))
//.resize(size, size)
.toFile(filePath);
// Upload to GCS
return bucket.upload(filePath, {
destination: join(dirname(filePath), filename)
});
});
}

触发函数后,我不断收到错误消息:

错误:vips_realpath:无法形成文件名 unix 错误:没有这样的文件 或目录vips__file_open_write:无法打开文件 "questions/zv872JxW"用于写入 unix 错误:没有这样的文件或 目录

我做错了什么?

在将文件上传到 Cloud Storage 之前,夏普不会创建临时目录来容纳文件。

filePath也缺少文件名/基名(带有扩展名(。

所以:

...
import { tmpdir } from 'os';
import { ensureDir, remove } from 'fs-extra';

export async function drawQuestionImage(question: any): Promise<any> {
// Create a temporary directory to house files, and ensure it exists.
const workingDir = join(tmpdir(), 'thumbs');
await ensureDir(workingDir);
...
...
...
const fileName = `${question.id}.png`,
filePath = join(workingDir, fileName), // Tmp path for resized image
bucketDir = `questions/${question.id}/`, // Dir in bucket
destination = join(bucketDir, fileName); // Ultimate image name in bucket
// Resize source image
await sharp(Buffer.from(base64.toString(), 'base64'))
.resize(size, size)
.toFile(filePath);
// Upload to GCS
await bucket.upload(filePath, { destination });
return remove(workingDir); // Clear up by deleting entire tmp directory.
};

请注意三个新导入。你也可以在这里使用 mkdirp。我们创建临时目录,并将调整大小的图像嵌套在其中(filePath(。然后我们调用文件上传,传递一个完整的路径(destination(作为桶内的绝对位置。最后,我们把房子炸毁了。

最新更新