简体   繁体   English

直接从谷歌存储读取 JSON 文件(使用 Cloud Functions)

[英]Read JSON file directly from google storage (using Cloud Functions)

I created a function that extracts a specific attribute from a JSON file, but this file was together with the function in Cloud Functions.我创建了一个 function 从 JSON 文件中提取特定属性,但该文件与 Cloud Functions 中的 function 一起。 In this case, I was simply attaching the file and was able to refer to a specific attribute:在这种情况下,我只是附加文件并能够引用特定属性:

const jsonData = require('./data.json');
const result = jsonData.responses[0].fullTextAnnotation.text;

return result;

Ultimately, I want to read this file directly from cloud storage and here I have tried several solutions, but without success.最终,我想直接从云存储中读取这个文件,在这里我尝试了几种解决方案,但都没有成功。 How can I read a JSON file directly from google storage so that, as in the first case, I can read its attributes correctly?如何直接从谷歌存储中读取 JSON 文件,以便与第一种情况一样,我可以正确读取其属性?

As mentioned in the comment the Cloud Storage API allows you to do many things through API.正如评论中提到的,云存储 API 允许您通过 API 做很多事情。 Here's an example from documentation on how to download a file from Cloud Storage for your reference.以下是有关如何从 Cloud Storage 下载文件以供参考的文档示例。

/**
 * TODO(developer): Uncomment the following lines before running the sample.
 */
// The ID of your GCS bucket
// const bucketName = 'your-unique-bucket-name';

// The ID of your GCS file
// const fileName = 'your-file-name';

// The path to which the file should be downloaded
// const destFileName = '/local/path/to/file.txt';

// Imports the Google Cloud client library
const {Storage} = require('@google-cloud/storage');

// Creates a client
const storage = new Storage();

async function downloadFile() {
  const options = {
    destination: destFileName,
  };

  // Downloads the file
  await storage.bucket(bucketName).file(fileName).download(options);

  console.log(
    `gs://${bucketName}/${fileName} downloaded to ${destFileName}.`
  );
}

downloadFile().catch(console.error);

To clearly answer the question: you can't!明确回答问题:你不能!

You need to download the file locally first, and then process it.您需要先将文件下载到本地,然后再进行处理。 You can't read it directly from GCS.您不能直接从 GCS 读取它。

With Cloud Functions you can only store file in the /tmp directory, it's the only one writable.使用 Cloud Functions,您只能将文件存储在/tmp目录中,这是唯一可写的。 In addition, it's an in-memory file system, that means several things:此外,它是一个内存文件系统,这意味着几件事:

  • The size is limited by the memory set up to the Cloud Function.大小受云 Function 设置的 memory 限制。 The memory space is shared between your app memory footprint and your file storage in /tmp (you won't be able to download a file of 10Gb for example) memory 空间在您的应用程序 memory 占用空间和/tmp中的文件存储之间共享(例如,您将无法下载 10Gb 的文件)
  • The memory is lost when the instance goes down and memory 在实例宕机时丢失,并且
  • All the Cloud Functions instances have their own memory space.所有 Cloud Functions 实例都有自己的 memory 空间。 You can't share the files between all the Cloud Functions您无法在所有 Cloud Functions 之间共享文件
  • The /tmp directory isn't cleaned between 2 functions invocation (on the same instance). /tmp目录在 2 个函数调用之间没有被清理(在同一个实例上)。 Think to cleanup yourselves this directory.想想自己清理这个目录。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从 Google Cloud Storage 读取 Excel 文件 - Read Excel file from Google Cloud Storage 将文件从谷歌云存储直接下载到nodejs中的客户端 - Download file from google cloud storage directly to client in nodejs 如何使用node js读取上传到谷歌云存储的JSON文件的内容 - How to read content of JSON file uploaded to google cloud storage using node js 如何使用 Cloud Functions 中的“onFinalize”将 300Mb JSON 文件从 Firebase 存储导入数据库? - How to import 300Mb JSON file from Firebase Storage to Database using 'onFinalize' in Cloud Functions? Firebase:云函数+存储读取文件 - Firebase: Cloud Functions + Storage Read File 如何从@google-cloud/storage 读取文件? - How to read file from @google-cloud/storage? 从云功能中删除云存储中的文件 - Delete file in cloud storage from cloud functions 使用谷歌云存储 JSON Api(React-Native 和云函数) - Using Google Cloud Storage JSON Api (React-Native and Cloud Functions) 使用下载 url 和 Cloud Functions 从 Firebase 存储中删除文件 - Delete a file from firebase storage using download url with Cloud Functions 使用 Cloud Functions 将图像上传到 Google Storage - Uploading an image to Google Storage using Cloud Functions
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM