我正在尝试将一个简单的测试文件从我的工作目录上传到 firebase 存储。为此,我创建了以下代码:
const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp();
exports.Storage = functions.https.onCall((data, 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('./tst.txt', options).then(function(data) {
const file = data[0];
if (file) {
return file;
}else{
throw new Error("Irgendwas geht ned")
}
}).catch(e);
return 200;
});
不幸的是,firebase 总是说:
错误:ENOENT:没有这样的文件或目录,stat '/tst.txt'
我的文件位于我的项目中的./tst.txt 我的目标是在我的云函数中生成一个文本文件并将其上传到 firebase 存储来存储它。当前存储器中没有存储任何文件。现在我希望能够上传已经创建的文件。 这就是我的文件的组织方式:
Firebase CLI 将部署函数文件夹中的所有文件(node_modules 除外)。您的 tst.txt 文件不在那里 - 它是上一级文件夹。所以它甚至没有被部署。您必须将其移至函数中,以便在运行时可供函数使用。
对于最终遇到
requirements.txt
文件未上传问题(python)的其他人。这是因为 .gcloudignore
中有 .git
和 .gitignore
,但我的 *.txt
文件中有一个 .gitignore
。我从 *.txt
中删除了 .gitignore
然后它就起作用了。