如果文件上传到Firebase存储,如何部署运行的Firebase功能?

时间:2018-02-02 11:36:14

标签: javascript firebase firebase-storage google-cloud-functions

我正在尝试部署Firebase函数,该函数在将图像上传到名为“upload”的存储桶时运行。但是,当我尝试部署该函数时,我收到此错误:

无法配置触发器GCS Bucket:上传

以下是一些其他信息:

我的存储桶是:gs://my-firestore-c379d.appspot.com/upload

以下是代码示例:

'use strict';

const functions = require('firebase-functions');
const gcs = require('@google-cloud/storage')();

functions.storage.bucket('upload').object().onChange(event => {

exports.photoInfo = functions.storage.object().onChange(event => {
const fileBucket = object.bucket; // The Storage bucket that contains the file.
const filePath = object.name; // File path in the bucket.
const contentType = object.contentType; // File content type.
const resourceState = object.resourceState; // The resourceState is 'exists' or 'not_exists' (for file/folder deletions).
const metageneration = object.metageneration; // Number of times metadata has been generated. New objects have a value of 1.
const metadata = object.metadata;
const guestId = object.metadata['guestId'];

console.log('object:' + object);
console.log('fileBucket: ' + fileBucket);
console.log('filePath: ' + filePath);
console.log('contentType: ' + contentType);
console.log('resourceState: ' + resourceState);
console.log('metageneration: ' + metageneration);
console.log('metadata: ' + metadata);
console.log('guestId: ' + guestId);

return 0;

});

这是我的package.json:

{
"name": "functions",
"description": "Cloud Functions for Firebase",
"scripts": {
  "lint": "./node_modules/.bin/eslint .",
  "serve": "firebase serve --only functions",
  "shell": "firebase experimental:functions:shell",
  "start": "npm run shell",
  "deploy": "firebase deploy --only functions",
  "logs": "firebase functions:log"
 },
"dependencies": {
  "firebase-admin": "~5.8.1",
  "firebase-functions": "^0.8.1",
  "@google-cloud/storage": "^1.5.2"
},
"devDependencies": {
  "eslint": "^4.12.0",
  "eslint-plugin-promise": "^3.6.0"
},
"private": true
}

我做了一些研究并找到了这两个链接,但我仍然无法删除该错误:

https://github.com/firebase/functions-samples/issues/66

https://github.com/firebase/friendlychat-web/issues/184#issuecomment-290914672

如何创建仅侦听“上传”存储桶中所做更改并删除该错误的函数?

感谢您的帮助。

2 个答案:

答案 0 :(得分:3)

这似乎是语法错误:

functions.storage.bucket('upload').object().onChange(event => {

exports.photoInfo = functions.storage.object().onChange(event => {

如果你看documentation for triggering Cloud Functions from Storage它说:

  

使用functions.storage创建处理云存储事件的功能。根据您是要将功能范围扩展到特定的云存储桶还是使用默认存储桶,使用以下其中一种(强调我的):

     
      
  • functions.storage.object()侦听默认存储分区上的对象更改。
  •   
  • functions.storage.bucket('bucketName').object()侦听特定存储桶上的对象更改。
  •   
exports.generateThumbnail = functions.storage.object().onChange(event => {
  // ...
});

这意味着您不应该在一个功能中使用这两个签名,而是选择符合您需求的签名。因为你试图在特定的存储桶上触发:

functions.storage.bucket('upload').object().onChange(event => {
  // ...
})

答案 1 :(得分:0)

......不知何故,这两个嵌套导出看起来也很奇怪。

FireStore也会有一个OnCreate事件触发器...... Cloud Storage Triggers处的示例也不显示任何嵌套导出/嵌套可安装触发器。也许在exports.generateThumbnail中记录桶名?

bucket ACL也可以阻止安装触发器。