如何使用Firebase Cloud Functions Storage创建文件夹

14

如何创建名为“posts”的文件夹?

存储桶 --> gs://app.appspot.com/posts

代码

exports.generateThumbnail = functions.storage.object()
  .onChange(event => {
    const object = event.data
    const filePath = object.name
    const fileName = filePath.split('/').pop()
    const fileBucket = object.bucket;
    const bucket = gcs.bucket(fileBucket)
    const tempFilePath = `/posts/${fileName}`

    return bucket.file(filePath).download({
      destination: tempFilePath
    })
    .then(() => {
      console.log('image downloaded locally to', tempFilePath)
      return spawn('convert', [tempFilePath, '-thumbnail', '400x400>', tempFilePath])
    })
    .then(() => {
      console.log('thumbnail created')
      // match end of string that contains a slash followed by 0 or more characters that are not a slash:
      const thumbFilePath = filePath.replace(/(\/)?([^\/]*)*/, '$1thumb_$2')
          console.log('afoter=thumbFilePath=='+thumbFilePath);
      return bucket.upload(tempFilePath, {
        destination: thumbFilePath
      })
    })

  });

错误

错误:只读文件系统,打开 '/posts/image.jpg'?

2个回答

24

除了/tmp目录外,Cloud Functions的文件系统是只读的。你需要将/posts目录更改为类似于/tmp/posts,才能创建目录或进行写操作。你还可以使用类似node-tmp这样的工具来更轻松地管理临时文件/目录的创建。


哇,我一直在云函数中不停地尝试将数据写入数据库,真是让我烦恼。感谢您的澄清,我甚至不确定文档中是否有提到这点。 - AlxVallejo
2
此外,为了确保您的代码可移植性,请不要硬编码“tmp”,而是使用os.tmpdir()获取本地临时目录,并使用path.join()获取子路径。这可以确保您的节点模块在Windows上运行正常,当有人想使用本地仿真器测试您的代码时,这非常方便。 - Thomas Bouldin
一整天都在不停地摆弄,结果最终来到这里。谢谢! - Satheesh
1
在什么情况下会清空 /tmp 目录?长时间不活动,新部署等。谢谢。 - rraallvv
3
不要假设 /tmp 目录会在当前处理事件之后继续存在。 - Michael Bleigh
@ThomasBouldin 谢谢。os.tempdir()是解决这个问题的绝佳方法。 - seveneights

6
如下所示的可工作代码:
//add 3 package
const path = require('path');
const os = require('os');
const fs = require('fs');


exports.generateThumbnail = functions.storage.object()
  .onChange(event => {

  const object = event.data; 
  const fileBucket = object.bucket; 
  const filePath = object.name; 
  const contentType = object.contentType; 
  const resourceState = object.resourceState; 
  const metageneration = object.metageneration; 

  const bucket = gcs.bucket(fileBucket);
  const tempFilePath = path.join(os.tmpdir(), fileName);
  return bucket.file(filePath).download({
    destination: tempFilePath
  }).then(() => {
    console.log('Image downloaded locally to', tempFilePath);
    // Generate a thumbnail using ImageMagick.
    return spawn('convert', [tempFilePath, '-thumbnail', '400x400>', tempFilePath]);
  }).then(() => {
    console.log('Thumbnail created at', tempFilePath);
    // We add a 'thumb_' prefix to thumbnails file name. That's where we'll upload the thumbnail.
    const thumbFileName = `thumb_${fileName}`;
    const thumbFilePath = path.join(path.dirname(filePath), thumbFileName);
    // Uploading the thumbnail.
    return bucket.upload(tempFilePath, {destination: thumbFilePath});
  // Once the thumbnail has been uploaded delete the local file to free up disk space.
  }).then(() => fs.unlinkSync(tempFilePath));

  });

成功 -> /tmp/posts -> gs://app.appspot.com/posts

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接