这份文档对我来说太过复杂。它展示了如何从云存储下载文件到云函数,操纵文件,然后上传新文件到云存储。我只想要看到将文件从云函数上传到云存储的基本最小指令。为什么这样不起作用:
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不是一个函数
。我猜测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
,但都没有解决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 版本管理器来回退到旧版本吗?