【问题标题】:How to update file when hosting in Google App Engine?在 Google App Engine 中托管时如何更新文件?
【发布时间】:2020-06-26 21:31:57
【问题描述】:

我在 Google Cloud App Engine 上运行节点 js 服务器服务。 我在项目的资产文件夹中有 JSON 文件,需要按流程进行更新。 我能够读取文件中的文件和配置。但是在添加文件时从 GAE 获取只读服务错误。

有没有一种方法可以在不使用云存储选项的情况下将信息写入文件?

这是一个非常小的文件,使用云存储的东西将使用一个非常大的钻机来拧内六角扳手

谢谢

【问题讨论】:

    标签: node.js google-app-engine gcloud gae-module


    【解决方案1】:

    不,在 App Engine Standard 中没有这样的文件系统。在docs中,提到了以下内容:

    运行时包括一个完整的文件系统。文件系统是只读的,但位置 /tmp 是一个在 App Engine 实例的 RAM 中存储数据的虚拟磁盘。

    因此,考虑到这一点,您可以写信给 /tmp,但我建议使用 Cloud Storage,因为如果缩放关闭所有实例,数据将会丢失。

    您还可以考虑 App Engine Flex,它提供 HDD(因为它的后端是 VM),但最小大小为 10GB,因此它比使用存储更糟糕。

    【讨论】:

    • 是的。修改云中的文件时要小心——我建议使用 firestore 或 storage 转储您的 json。您可以在每次编辑时加载最新/保存到新版本(除非编辑次数变得荒谬)。
    • 感谢您的解决方案。我想我会为此使用云存储,因为不会有太多数据或荒谬的编辑。
    【解决方案2】:

    曾经感谢您引导我不要浪费时间寻找解决问题的黑客解决方案。

    无论如何,没有明确的代码如何使用 /tmp 目录并使用应用引擎托管的 node.js 应用程序下载/上传文件。 如果有人需要,这里是代码

    const {
        Storage
    } = require('@google-cloud/storage');
    const path = require('path');
    
    class gStorage {
        constructor() {
            this.storage = new Storage({
                keyFile: 'Please add path to your key file'
            });
            this.bucket = this.storage.bucket(yourbucketname);
            this.filePath = path.join('..', '/tmp/YourFileDetails');
            // I am using the same file path and same file to download and upload
        }
    
        async uploadFile() {
            try {
                await this.bucket.upload(this.filePath, {
                    contentType: "application/json"
                });
            } catch (error) {
                throw new Error(`Error when saving the config. Message :  ${error.message}`);
            }
        }
    
        async downloadFile() {
            try {
                await this.bucket.file(filename).download({
                    destination: this.filePath
                });
            } catch (error) {
                throw new Error(`Error when saving the config. Message :  ${error.message}`);
            }
        }
    }

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2010-10-23
      • 1970-01-01
      • 2016-02-15
      • 1970-01-01
      • 2010-10-19
      • 2020-09-05
      • 1970-01-01
      • 2021-02-25
      相关资源
      最近更新 更多