【问题标题】:Google Cloud Function - Error: ENOENT: no such file or directory谷歌云功能 - 错误:ENOENT:没有这样的文件或目录
【发布时间】:2019-04-16 19:29:09
【问题描述】:

我正在尝试做一个简单的函数来调整存储中新上传的图像的大小。我使用以下方法来帮助我做到这一点:

import { tmpdir } from 'os';
import { join, dirname } from 'path';
import * as sharp from 'sharp';
import * as fs from 'fs-extra';

这段代码执行时:

await bucket.file(filePath).download({
    destination: tmpFilePath
});

我在 Google Cloud Function 日志中收到以下错误:

错误:ENOENT:没有这样的文件或目录,打开 '/tmp/images/1542144115815_Emperor_penguins.jpg' at Error (native)

这里是完整的代码[segment]:

const gcs = admin.storage();
const db = admin.firestore();

import { tmpdir } from 'os';
import { join, dirname } from 'path';

import * as sharp from 'sharp';
import * as fs from 'fs-extra';

export const imageResize = functions.storage
    .object()
    .onFinalize(async object => {
        console.log('> > > > > > > 1.3 < < < < < < <');
        const bucket = gcs.bucket(object.bucket);
        console.log(object.name);
        const filePath = object.name;
        const fileName = filePath.split('/').pop();
        const tmpFilePath = join(tmpdir(), object.name);

        const thumbFileName = 'thumb_' + fileName;
        const tmpThumbPath = join(tmpdir(), thumbFileName);

        console.log('step 1');
        // Resizing image
        if (fileName.includes('thumb_')) {
            console.log('exiting function');
            return false;
        }
        console.log('step 2');
        console.log(`filePath: ${filePath}`);
        console.log(`tmpFilePath: ${tmpFilePath}`);
        await bucket.file(filePath).download({
            destination: tmpFilePath
        });
        console.log('step 3');
        await sharp(tmpFilePath)
            .resize(200, 200)
            .toFile(tmpThumbPath);

        await bucket.upload(tmpThumbPath, {
            destination: join(dirname(filePath), thumbFileName)
        });

更新 1:添加 await fs.ensureDir(tmpFilePath); 以确保文件路径存在。现在收到一个新错误:

错误:EINVAL:无效参数,在错误时打开“/tmp/images/1542146603970_mouse.png”(本机)

更新 2 已解决:在下面添加了一个解决方案作为答案。

【问题讨论】:

    标签: node.js google-cloud-firestore google-cloud-storage google-cloud-functions


    【解决方案1】:

    我改了下面的代码

    来自

    const bucket = gcs.bucket(object.bucket);
    const filePath = object.name;
    const fileName = filePath.split('/').pop();
    const tmpFilePath = join(tmpdir(), object.name);
    
    const thumbFileName = 'thumb_' + fileName;
    const tmpThumbPath = join(tmpdir(), thumbFileName);
    

    收件人

    const bucket = gcs.bucket(object.bucket);
    const filePath = object.name;
    const fileName = filePath.split('/').pop();
    const thumbFileName = 'thumb_' + fileName;
    
    const workingDir = join(tmpdir(), `${object.name.split('/')[0]}/`);//new
    const tmpFilePath = join(workingDir, fileName);
    const tmpThumbPath = join(workingDir, thumbFileName);
    
    await fs.ensureDir(workingDir);
    

    如您所见,我创建了一个workingDir,它将在路径之间共享,然后运行await fs.ensureDir(workingDir); 来创建路径。这解决了我的问题。

    【讨论】:

      【解决方案2】:

      我怀疑您会看到该消息,因为您尝试写入此路径:

      /tmp/images/1542144115815_Emperor_penguins.jpg
      

      无需先创建父目录:

      /tmp/images
      

      您无法将文件写入不存在的本地文件系统文件夹,而且 Cloud Storage SDK 似乎不会为您创建它。

      【讨论】:

      • 但这不是在做什么,const tmpFilePath = join(tmpdir(), object.name); 创建目录吗? (其中对象名称为 images/1542144115815_Emperor_penguins.jpg)
      • 不,那只是构建一个字符串。它与文件系统无关。
      • 这和这个版本的做法很相似,代码稍微老了一点:angularfirebase.com/lessons/…
      • 如果你在这个函数中只处理单个文件,为什么不给它一个静态名称/路径,而不是从存储路径派生的名称?
      • 该示例中缺少的关键是对await fs.ensureDir(workingDir); 的调用。这将创建目录。
      猜你喜欢
      • 1970-01-01
      • 2017-10-07
      • 1970-01-01
      • 2019-03-09
      • 2017-12-28
      • 2022-01-16
      • 2021-03-30
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多