[英]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:此外,它是一个内存文件系统,这意味着几件事:
/tmp
(you won't be able to download a file of 10Gb for example) /tmp
中的文件存储之间共享(例如,您将无法下载 10Gb 的文件)/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.