Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/google-cloud-platform/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Google cloud platform 如何使用云功能下载谷歌云存储中文件夹中的文件?_Google Cloud Platform_Google Cloud Storage_Google Cloud Functions - Fatal编程技术网

Google cloud platform 如何使用云功能下载谷歌云存储中文件夹中的文件?

Google cloud platform 如何使用云功能下载谷歌云存储中文件夹中的文件?,google-cloud-platform,google-cloud-storage,google-cloud-functions,Google Cloud Platform,Google Cloud Storage,Google Cloud Functions,我正在使用一个https触发的Google云函数,该函数应该从Google云存储下载一个文件(然后将其与req.body中的数据结合起来)。虽然只要下载的文件在根目录中,它似乎就可以工作,但当将同一文件放在文件夹中时,我在访问该文件时遇到了问题。文件路径为documents/someTemplate.docx 'use strict'; const functions = require('firebase-functions'); const path = require('path'); c

我正在使用一个https触发的Google云函数,该函数应该从Google云存储下载一个文件(然后将其与req.body中的数据结合起来)。虽然只要下载的文件在根目录中,它似乎就可以工作,但当将同一文件放在文件夹中时,我在访问该文件时遇到了问题。文件路径为
documents/someTemplate.docx

'use strict';
const functions = require('firebase-functions');
const path = require('path');
const os = require("os");
const fs = require('fs');
const gcconfig = {
  projectId: "MYPROJECTNAME",
  keyFilename: "KEYNAME.json"
};
const Storage = require('@google-cloud/storage')(gcconfig)
const bucketPath = 'MYPROJECTNAME.appspot.com'
const bucket = Storage.bucket(bucketPath);

exports.getFileFromStorage = functions.https.onRequest((req, res) => {
      let fileName = 'documents/someTemplate.docx'
      let tempFilePath = path.join(os.tmpdir(), fileName);

      return bucket.file(fileName)
      .download({
          destination: tempFilePath,
        })
        .then(() => {
          console.log(fileName + ' downloaded locally to', tempFilePath);
          let content = fs.readFileSync(tempFilePath, 'binary');

          // do stuff with the file and data from req.body

          return
        })
        .catch(err => {
          res.status(500).json({
            error: err
          });
        });
})
我不明白的是,当我将文件移动到根目录并使用文件名
someTemplate.docx
时,代码就起作用了

声明

添加到文件夹的对象似乎位于GCP控制台中的文件夹中。实际上,所有对象都存在于bucket级别,只是在其名称中包含目录结构。例如,如果创建名为pets的文件夹并将文件cat.jpeg添加到该文件夹中,则GCP控制台会使该文件显示为存在于该文件夹中。实际上,没有单独的文件夹实体:文件只存在于bucket中,名称为pets/cat.jpeg


这似乎是正确的,因为在元数据中,文件名确实是
documents/someTemplate.docx
。因此,我不明白为什么上面的代码不起作用。

发布来自@James Poag的评论答案以获得可见性:


此外,临时文件夹位置上可能不存在该目录?也许可以尝试让tempFilePath=path.join(os.tmpdir(),'tempkjhgfhjnmbvgh.docx')–詹姆斯·波格8月21日17:10

到底什么不起作用?是否有错误消息?请使用
firebase Service--only functions
在本地运行,以便获取控制台错误(更快)。此外,临时文件夹位置上可能不存在该目录?也许可以尝试
让tempFilePath=path.join(os.tmpdir(),'tempkjhgfhjnmbvgh.docx')@JamesPoag谢谢,这就是问题所在,我没有意识到它不会自动创建文件夹。奇怪的是,尽管我一直在使用
firebase-service
,但从未收到错误消息。它会告诉我,例如
执行耗时16毫秒,用户功能成功完成
,但既不运行
然后
也不运行
捕获
部分。我认为很多人使用
tmp
节点模块:
npm安装tmp