无法在 firebase 存储桶中创建文件夹 - firebase 管理员
unable to make folders in firebase storage bucket - firebase admin
在我的例子中,我有一个 Url 的列表,我想从这些网址下载每个文件并将其组织在 firebase 存储桶中,我的问题是我无法在 firebase 中创建文件夹通过 nodejs javascript/typescript.
存储桶
well firebase 存储提供 ref() 和子方法来上传子文件夹中的文件(参见 this) but firebase only offers those method for firebase client libraries, it is not that we can not use client library in nodejs but they have made some namespaces hidden when you connect firebase client library in nodejs and storage is one of them (see this)。
我很高兴他们分别考虑了前端和后端,因为前端和后端在安全和用例方面有完全不同的场景,所以他们真正编写用于 nodejs 的是 firebase admin,我不能请参阅官方文档中的 ref 和 child 方法,他们说的是 this 不是任何其他方式来命名我正在上传的文件,也不是任何使文件夹成为子目录的方法,当我从我的计算机上传文件时它得到保存在存储桶根目录中,名称与它在我的计算机中的文件名相同,即使我可以从 firebase 控制台手动创建文件夹,但它不能满足我的要求,确保必须有任何方法以编程方式创建文件夹。
我也试过使用google云存储库const {Storage} = require('@google-cloud/storage');
,但结果是firebase admin和gogole云库共享同一个文档,至少在上传文件部分有相同的界面。
好吧,我已经度过了我的一天(好吧,因为它是4:46am,所以晚上也是)尝试不同的库并深入研究他们的文档,我也发现这些文档几乎没有组织和缺乏代码示例。
如有任何帮助,我们将不胜感激,到目前为止,我的代码片段来自他们的文档并正确上传文件:
import "firebase/firestore"
admin.initializeApp({
credential: admin.credential.cert("./../path-to-service account-cert.json"),
databaseURL: 'gs://bilal-assistant-xxxxx.appspot.com'
});
const quran_bucket = admin.storage().bucket("quran-bucket");
quran_bucket.upload("./my_computer_path/fatiha.mp3", {
gzip: true,
metadata: {
cacheControl: 'public, max-age=31536000',
}
}).then(uploadResponse => {
console.log(` uploaded complete.`);
}).catch((reason: any) => {
console.log("reason: ", reason);
})
我只想将音频文件保存在文件夹存储桶中,而不是存储桶根目录中
根据 API 文档,upload() takes an UploadOptions 对象作为第二个参数。您将希望使用该对象的记录 destination
属性 来指定存储中文件的名称:
quran_bucket.upload("./my_computer_path/fatiha.mp3", {
destination: 'audio/juz30/fatiha.mp3',
gzip: true,
metadata: {
cacheControl: 'public, max-age=31536000',
}
})
您可能不想打扰 gzip 和 mp3,因为它已经压缩并且不会进一步压缩。
在我的例子中,我有一个 Url 的列表,我想从这些网址下载每个文件并将其组织在 firebase 存储桶中,我的问题是我无法在 firebase 中创建文件夹通过 nodejs javascript/typescript.
存储桶well firebase 存储提供 ref() 和子方法来上传子文件夹中的文件(参见 this) but firebase only offers those method for firebase client libraries, it is not that we can not use client library in nodejs but they have made some namespaces hidden when you connect firebase client library in nodejs and storage is one of them (see this)。
我很高兴他们分别考虑了前端和后端,因为前端和后端在安全和用例方面有完全不同的场景,所以他们真正编写用于 nodejs 的是 firebase admin,我不能请参阅官方文档中的 ref 和 child 方法,他们说的是 this 不是任何其他方式来命名我正在上传的文件,也不是任何使文件夹成为子目录的方法,当我从我的计算机上传文件时它得到保存在存储桶根目录中,名称与它在我的计算机中的文件名相同,即使我可以从 firebase 控制台手动创建文件夹,但它不能满足我的要求,确保必须有任何方法以编程方式创建文件夹。
我也试过使用google云存储库const {Storage} = require('@google-cloud/storage');
,但结果是firebase admin和gogole云库共享同一个文档,至少在上传文件部分有相同的界面。
好吧,我已经度过了我的一天(好吧,因为它是4:46am,所以晚上也是)尝试不同的库并深入研究他们的文档,我也发现这些文档几乎没有组织和缺乏代码示例。
如有任何帮助,我们将不胜感激,到目前为止,我的代码片段来自他们的文档并正确上传文件:
import "firebase/firestore"
admin.initializeApp({
credential: admin.credential.cert("./../path-to-service account-cert.json"),
databaseURL: 'gs://bilal-assistant-xxxxx.appspot.com'
});
const quran_bucket = admin.storage().bucket("quran-bucket");
quran_bucket.upload("./my_computer_path/fatiha.mp3", {
gzip: true,
metadata: {
cacheControl: 'public, max-age=31536000',
}
}).then(uploadResponse => {
console.log(` uploaded complete.`);
}).catch((reason: any) => {
console.log("reason: ", reason);
})
我只想将音频文件保存在文件夹存储桶中,而不是存储桶根目录中
根据 API 文档,upload() takes an UploadOptions 对象作为第二个参数。您将希望使用该对象的记录 destination
属性 来指定存储中文件的名称:
quran_bucket.upload("./my_computer_path/fatiha.mp3", {
destination: 'audio/juz30/fatiha.mp3',
gzip: true,
metadata: {
cacheControl: 'public, max-age=31536000',
}
})
您可能不想打扰 gzip 和 mp3,因为它已经压缩并且不会进一步压缩。