【问题标题】:How to access a specific file and its contents from within Google Cloud Storage如何从 Google Cloud Storage 中访问特定文件及其内容
【发布时间】:2019-09-16 18:30:35
【问题描述】:

我需要访问存储在 Google Cloud Storage 上的文件,这不是上传的文件,而是由云功能创建的。我不知道如何使用 Node 访问这个文件。

我已经尝试了很多 Stackoverflow 上推荐的东西。

同样,我查看了示例 Google 项目,但它只使用读取流,并且因为我没有上传文件,所以我不知道如何使用它。

我得到的最接近的是修改第一个 link 来得到这个

var {Storage} = require('@google-cloud/storage');
var gcs = new Storage({
  keyFilename: path.join("_file.json"),
  projectId: 'ProjectId'
});

const chosenBucket = gcs.bucket("bucket_name");
var file = chosenBucket('file.json');

这会导致 TypeError: TypeError: bucket is not a function

如何访问和读取文件中的 json?

【问题讨论】:

  • 你知道文件名吗?您想在写入文件时启动此过程吗?你的 Node 版本是多少?
  • 是的,我知道文件名。我根本不需要写入文件,只需从中读取,但它是一个将为每个实例新创建的文件。它是节点的第 12 版。
  • 另外,文件的权限是公开的。

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


【解决方案1】:
const chosenBucket = gcs.bucket("bucket_name");
var file = chosenBucket('file.json');

chosenBucket 不是一个函数。这是一个Bucket 对象。做这样的事情:

const chosenBucket = gcs.bucket("bucket_name");
var file = chosenBucket.file('file.json');

const download_options = {
  // The path to which the file should be downloaded, e.g. "./file.txt"
  destination: destFilename,
};
await file.download(download_options)

查看示例:https://github.com/googleapis/nodejs-storage/blob/master/samples/files.js

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-21
    • 1970-01-01
    相关资源
    最近更新 更多