【发布时间】:2019-04-08 04:43:49
【问题描述】:
documentation 太复杂了,我无法理解。它展示了如何将文件从 Cloud Storage 下载到 Cloud Functions、操作文件,然后将新文件上传到 Cloud Storage。我只想查看将文件从 Cloud Functions 上传到 Cloud Storage 的基本、最低限度的说明。为什么这不起作用:
const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp();
exports.storage = functions.firestore.document('Test_Value').onUpdate((change, context) => {
var metadata = {
contentType: 'text',
};
admin.storage().ref().put( {'test': 'test'}, metadata)
.then(function() {
console.log("Document written.");
})
.catch(function(error) {
console.error(error);
})
});
错误消息是admin.storage(...).ref is not a function。我猜firebase-admin 包括 Firestore 但不包括 Storage?我应该使用@google-cloud/storage 而不是firebase-admin?为什么这不起作用:
const functions = require('firebase-functions');
const admin = require('firebase-admin');
const {Storage} = require('@google-cloud/storage')();
const storage = new Storage();
admin.initializeApp();
exports.storage = functions.firestore.document('Test_Value').onUpdate((change, context) => {
storage.bucket().upload( {'test': 'test'} , {
metadata: {
contentType: 'text'
}
})
});
我什至无法部署此代码,错误消息是
Error parsing triggers: Cannot find module './clone.js'
显然缺少一个 npm 模块依赖项?但是模块不叫clone.js?我尝试要求child-process-promise、path、os 和fs; none 修复了缺少的 clone.js 错误。
为什么admin.initializeApp(); 缺少参数,而我的index.html 文件中有:
firebase.initializeApp({
apiKey: 'swordfish',
authDomain: 'myapp.firebaseapp.com',
databaseURL: "https://myapp.firebaseio.com",
projectId: 'myapp',
storageBucket: "myapp.appspot.com"
});
我看到的另一个问题:
npm list -g --depth=0
/Users/TDK/.nvm/versions/node/v6.11.2/lib
├── child_process@1.0.2
├── UNMET PEER DEPENDENCY error: ENOENT: no such file or directory, open '/Users/TDK/.nvm/versions/node/v6.11.2/lib/node_modules/firebase-admin/package.json
├── firebase-functions@2.1.0
├── firebase-tools@6.0.1
├── firestore-backup-restore@1.3.1
├── fs@0.0.2
├── npm@6.4.1
├── npm-check@5.9.0
├── protractor@5.4.1
├── request@2.88.0
└── watson-developer-cloud@3.13.0
换句话说,firebase-admin 或Node 6.11.2 有问题。我应该使用 Node 版本管理器恢复到旧版本的 Node 吗?
【问题讨论】:
-
同样的痛苦,我猜我们应该使用这个 API 参考。googleapis.dev/nodejs/storage/latest/File.html#save
标签: firebase google-cloud-functions firebase-storage