【问题标题】:How to write google cloud storage node getFiles to file?如何将谷歌云存储节点getFiles写入文件?
【发布时间】:2018-10-18 04:57:26
【问题描述】:

下面的代码生成一个内容为 Object 的文件。你能指出我在这里缺少什么吗?

gcs.bucket(xxx).getFiles()
        .then(results => {
            const files = results[0];
            files.forEach(file => {
                if(file.name === 'xxx'){
                    fs.writeFile("xxx", file, function(err) {
                        if(err) {
                            return console.log(err);
                        }

                        console.log("The file was saved!");
                    });
                    res.end()
                }
            });
        })
        .catch(err => {
            console.error('ERROR:', err);
        });

【问题讨论】:

  • 查看cloud.google.com/nodejs/docs/reference/storage/1.7.x/File 上的下载文件 示例...如果您无法理解基于event 的架构并且无法让它为您工作然后告诉我。我会将事件转换为基本承诺……这样您就可以获得流媒体的好处,但仍然是处理所有文件的简单方法……除非您不在乎它们何时完成。
  • 谢谢科迪。我正在尝试下载 api,结果是一个空文件。 gcs.bucket.file('icons.json').download({ 目的地: 'icons.json' },err => {)});这个 getFiles 方法给了我一个大对象,它与我要获取的文件的内容无关。我希望其中有一个隐藏的方法可以提供文件内容。
  • 如果您将回调用作承诺,请不要使用回调,gcs.bucket.file('icons.json').download({ destination: 'icons.json' }).then(downloadResponse=>{});
  • 它应该返回一个https://cloud.google.com/nodejs/docs/reference/storage/1.7.x/global#DownloadResponse,它应该只是文件的内容......
  • 谢谢科迪!你刚刚结束了两天的痛苦!

标签: node.js express google-cloud-storage


【解决方案1】:

如果您将回调用作承诺,请不要使用它,

gcs.bucket.file('icons.json').download({ destination: 'icons.json' }).then(downloadResponse=>{});

【讨论】:

  • 我得到一个不是我的文件的 json 文件。 {"domain":null,"_events":{},"_eventsCount":0,"metadata":{"kind":"storage#object","id":"....我希望有像 file.content 这样的方法,它给出了实际的文件内容。
猜你喜欢
  • 2019-12-26
  • 1970-01-01
  • 1970-01-01
  • 2014-08-17
  • 2014-06-21
  • 2013-06-26
  • 2019-05-08
  • 1970-01-01
  • 2022-01-03
相关资源
最近更新 更多