将文件从Firebase Cloud Functions上传到Cloud Storage [英] Uploading files from Firebase Cloud Functions to Cloud Storage
问题描述
文档对于我来说太复杂了.它显示了如何将文件从Cloud Storage下载到Cloud Functions,如何处理文件,然后将新文件上传到Cloud Storage.我只是想了解有关将文件从Cloud Functions上载到Cloud Storage的基本,最低限度的说明.为什么这样行不通:
The documentation is too complex for me to understand. It shows how to download a file from Cloud Storage to Cloud Functions, manipulate the file, and then upload the new file to Cloud Storage. I just want to see the basic, minimum instructions for uploading a file from Cloud Functions to Cloud Storage. Why doesn't this work:
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
吗?为什么这样行不通:
The error message is admin.storage(...).ref is not a function
. I'm guessing that firebase-admin
includes Firestore but not Storage? Instead of firebase-admin
should I use @google-cloud/storage
? Why doesn't this work:
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'
}
})
});
我什至无法部署此代码,错误消息是
I can't even deploy this code, the error message is
Error parsing triggers: Cannot find module './clone.js'
显然缺少npm模块依赖项?但是该模块不称为clone.js
吗?我试过要求child-process-promise
,path
,os
和fs
;没有一个可以解决丢失的clone.js
错误.
Apparently a npm module dependency is missing? But the module isn't called clone.js
? I tried requiring child-process-promise
, path
, os
, and fs
; none fixed the missing clone.js
error.
为什么admin.initializeApp();
缺少参数,而在我的index.html
文件中却有:
Why does admin.initializeApp();
lack parameters, when in my index.html
file I have:
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
出了点问题.我应该使用节点版本管理器还原到旧版本的节点吗?
In other words, there's something wrong with firebase-admin
, or with Node 6.11.2
. Should I use a Node Version Manager to revert to an older version of Node?
推荐答案
我通过Google Cloud Functions将文件从硬盘驱动器上传到Firebase Cloud Storage.首先,我找到了Google Cloud Functions的文档 bucket.upload .
I uploaded a file from my hard drive to Firebase Cloud Storage via Google Cloud Functions. First, I found the documentation for Google Cloud Functions bucket.upload.
const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp();
exports.Storage = functions.firestore.document('Storage_Value').onUpdate((change, context) => {
const {Storage} = require('@google-cloud/storage');
const storage = new Storage();
const bucket = storage.bucket('myapp.appspot.com');
const options = {
destination: 'Test_Folder/hello_world.dog'
};
bucket.upload('hello_world.ogg', options).then(function(data) {
const file = data[0];
});
return 0;
});
前三行是Cloud Functions样板.下一行
The first three lines are Cloud Functions boilerplate. The next line
exports.Storage = functions.firestore.document('Storage_Value').onUpdate((change, context) => {
创建云功能并设置触发器.接下来的三行是Google Cloud的样板.
creates the Cloud Function and sets the trigger. The next three lines are more Google Cloud boilerplate.
其余代码在我的项目目录的functions
文件夹中的计算机硬盘驱动器上找到文件hello_world.ogg
,并将其上载到目录Test_Folder
中,并将文件名更改为hello_world.dog
我的Firebase云存储.这将返回一个承诺,并且除非要对文件进行其他操作,否则下一行const file = data[0];
是不必要的.
The rest of the code locates the file hello_world.ogg
on my computer's hard drive in the functions
folder of my project directory and uploads it to the directory Test_Folder
and changes the name of the file to hello_world.dog
in my Firebase Cloud Storage. This returns a promise, and the next line const file = data[0];
is unnecessary unless you want to do something else with the file.
最后我们return 0;
.该行除了防止出现错误消息外没有任何作用
Lastly we return 0;
. This line does nothing except prevent the error message
Function returned undefined, expected Promise or Value
这篇关于将文件从Firebase Cloud Functions上传到Cloud Storage的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!