【发布时间】:2016-08-04 08:51:58
【问题描述】:
我正在创建一个可用于上传文件的 Firebase 应用。如何获取用户在其文件夹 (users/{userId}/{allPaths=**}) 中使用的空间量?
【问题讨论】:
我正在创建一个可用于上传文件的 Firebase 应用。如何获取用户在其文件夹 (users/{userId}/{allPaths=**}) 中使用的空间量?
【问题讨论】:
很好的问题。简而言之,没有简单的方法可以做到这一点(即使对我们来说!),因为这实际上需要我们递归整个文件集并将它们全部汇总。这是一个相当大的 mapreduce,每次上传文件时运行效率不高。
不过,我们会在 metadata.size 属性中返回单个文件的大小,因此您可以在服务器上执行自己的 list 调用(查看 gcloud`),这将为您提供文件列表和“文件夹”。获取文件的大小并将它们相加,然后递归并对所有子文件夹执行相同的操作。总结它们,然后将它们编写为 Firebase 实时数据库之类的东西,您可以在其中轻松地从客户端获取文件夹大小。
【讨论】:
这是我编写的一个小脚本,用于计算每个“文件夹”使用的文件和字节数并输出到控制台。
function main(bucketName = 'YOUR_BUCKET_NAME') {
/**
* TODO(developer): Uncomment the following line before running the sample.
*/
// const bucketName = 'Name of a bucket, e.g. my-bucket';
// Imports the Google Cloud client library
const {Storage} = require('@google-cloud/storage');
// Creates a client
const storage = new Storage();
async function listFiles() {
// Lists files in the bucket
const [files] = await storage.bucket(bucketName).getFiles();
console.log('Files:');
let bucketList = {};
files.forEach(file => {
let folder = file.name.split('/')[0];
if (!bucketList[folder]) {
bucketList[folder] = {};
bucketList[folder]['bytes'] = 0;
bucketList[folder]['count'] = 0;
}
bucketList[folder]['bytes'] += Number(file.metadata.size);
bucketList[folder]['count'] +=1;
});
console.log(bucketList);
}
listFiles().catch(console.error);
// [END storage_list_files]
}
main(...process.argv.slice(2));
【讨论】:
基于@Mike 的回答的略微改进的版本,它还递归地输出所有子文件夹的大小,并以“人类可读”格式(即 MB、kB 等)打印大小。
它还将输出写入一个 json 文件,您可以使用一些 json 查看器(例如 https://jsoneditoronline.org/)来浏览该文件。
请注意,您还需要将 serviceaccount.json 作为凭据传递给 Storage
function main(bucketName) {
// Imports the Google Cloud client library
const {Storage} = require('@google-cloud/storage');
const fs = require('fs').promises;
// Creates a client
const storage = new Storage({
credentials: //your service account json key
});
async function listFiles() {
// Lists files in the bucket
const [files] = await storage.bucket(bucketName).getFiles();
console.log('Files:');
let bucketList = {};
files.forEach(file => {
let folders = file.name.split('/');
let curFolder = bucketList
folders.forEach(subFolder => {
if (!curFolder[subFolder]) {
curFolder[subFolder] = {};
curFolder[subFolder]['bytes'] = 0;
curFolder[subFolder]['count'] = 0;
}
curFolder[subFolder]['bytes'] += Number(file.metadata.size);
curFolder[subFolder]['count'] +=1;
curFolder[subFolder]['size'] = humanFileSize(curFolder[subFolder]['bytes'])
curFolder = curFolder[subFolder]
})
});
console.log(bucketList);
await fs.writeFile("sizes.json", JSON.stringify(bucketList))
}
function humanFileSize(bytes, si=true, dp=1) {
const thresh = si ? 1000 : 1024;
if (Math.abs(bytes) < thresh) {
return bytes + ' B';
}
const units = si
? ['kB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB']
: ['KiB', 'MiB', 'GiB', 'TiB', 'PiB', 'EiB', 'ZiB', 'YiB'];
let u = -1;
const r = 10**dp;
do {
bytes /= thresh;
++u;
} while (Math.round(Math.abs(bytes) * r) / r >= thresh && u < units.length - 1);
return bytes.toFixed(dp) + ' ' + units[u];
}
listFiles().catch(console.error);
// [END storage_list_files]
}
//TODO change the name of the bucket to yours
main("my-bucket-name");
【讨论】: