firebase / google云存储getFiles工作正常但.upload或.file却没有

时间:2018-04-10 11:08:31

标签: firebase google-cloud-storage firebase-storage actions-on-google

我的firebase应用程序包含谷歌和谷歌云存储的操作。 当我执行bucket.getFiles()时,它列出了我的文件,这很好用。 但是当我执行.upload或.file时,我收到以下错误:

ApiError: Error during request.
        at Object.parseHttpRespBody (/user_code/node_modules/google-cloud/node_modules/@google-cloud/storage/node_modules/@google-cloud/common/src/util.js:193:32)
        at Object.handleResp (/user_code/node_modules/google-cloud/node_modules/@google-cloud/storage/node_modules/@google-cloud/common/src/util.js:137:18)
        at /user_code/node_modules/google-cloud/node_modules/@google-cloud/storage/node_modules/@google-cloud/common/src/util.js:502:12
        at Request.onResponse [as _callback] (/user_code/node_modules/google-cloud/node_modules/retry-request/index.js:195:7)
        at Request.self.callback (/user_code/node_modules/google-cloud/node_modules/request/request.js:186:22)
        at emitTwo (events.js:106:13)
        at Request.emit (events.js:191:7)
        at Request.<anonymous> (/user_code/node_modules/google-cloud/node_modules/request/request.js:1163:10)
        at emitOne (events.js:96:13)
        at Request.emit (events.js:188:7)
      code: undefined,
      errors: undefined,
      response: undefined,
      message: 'Error during request.' 

这是我的代码:

'use strict';

const App = require('actions-on-google').DialogflowApp;
const functions = require('firebase-functions');
const firebase = require('firebase');

const pdfkit = require('pdfkit');
var fs = require('fs');

var config = {
...
  };

firebase.initializeApp(config);

var gcloud = require('google-cloud');

// Enable Storage
var gcs = gcloud.storage({
  projectId: ...,
  keyFilename: ...
});

// Reference bucket
var bucket = gcs.bucket("gs://.../");

//action / intent
const NAME_ACTION = 'make_name';

exports.sillyNameMaker = functions.https.onRequest((request, response) => {
  const app = new App({request, response});

  //makename function
  function makeName (app) {

      //this does not work
      var doc = new pdfkit;
      const filename = 'output.pdf';
      const file = bucket.file(filename);
      const stream = file.createWriteStream({resumable: false});

      doc.pipe(stream);
      doc.fontSize(25).text('Some text with an embedded font!', 100, 100);
      doc.end();

      //this works
      bucket.getFiles()
        .then(results => {
          const files = results[0];
          console.log('Files:');
          files.forEach(file => {
            console.log(file.name);
          })
          return results
        })
        .catch((err) => console.log("error in get files: ",err));

      //this works not
      bucket.upload('./test.txt')
        .catch((err) => console.log("Error in upload file: ",err));
  }


  //map intent names to functions
  let actionMap = new Map();
  actionMap.set(NAME_ACTION, makeName);

  app.handleRequest(actionMap);
});

我对权限的请求有区别吗?或者我是否需要在Google操作中启用某些内容?

1 个答案:

答案 0 :(得分:0)

您用于初始化存储客户端的密钥文件链接到项目的服务帐户。该服务帐户可能确实具有足够的权限来列出存储桶中的文件但不能上载文件。有关与云存储相关的IAM权限的详细信息,请参阅文档here

您使用的服务帐户可能具有storage.objectViewer角色,该角色允许列出存储桶中的对象,但不能创建/上传它们。尝试授予它更广泛的角色,例如storage.admin,以验证它是否有效。