首页 > 解决方案 > 将包含可变数据的文件上传到 Google Cloud Storage

问题描述

我想上传一个文本文件,其中包含我的服务器之前收到的一些变量。例如,当用户 1 发送一些数据时,我想创建并上传一个名为USER1.txt的文件,其中包含USER1的详细信息。同样,当 USER2 发送他的数据时,会创建一个包含USER2详细信息的新文件USER2.txt并将其上传到 Google Cloud Storage。

这是我尝试过的

   const filename = "./details.txt";
   const content = 'this contains some random variables declared previously';
   fs.writeFileSync(filename,content);
   await savetocloud('my-bucket',filename);
});

const savetocloud = async function(bucketName, filename) {
   async function uploadFile() {
      await storage.bucket(bucketName).upload(filename,{
         gzip:true,
         metadata:{
            cacheControl: 'public, max-age=31536000',
         }
      });
      console.log('${filename} uplaoded to ${bucketname}');
   }
   uploadFile().catch(console.error);

该程序运行良好,并且在 localhost 中执行时也会上传文件。但是,当我将此应用程序部署到应用程序引擎时,它不起作用。

编辑:这是我的 app.yaml 文件。

runtime: nodejs12
automatic_scaling:
target_cpu_utilization: 0.85

handlers:
- url: /javascript
static_dir: javascript
- url: /style
static_dir: style
- url: /coverphoto
static_dir: coverphoto
- url: /slidephoto
static_dir: slidephoto

标签: node.jsgoogle-cloud-platformgoogle-cloud-storagefscloud-storage

解决方案


推荐阅读