如何从 Cloud Storage for Firebase 获取在特定时间创建的文件?

问题描述

enter image description here

我需要删除云存储中 eventPoster 文件夹中最近一次修改元数据的时间超过 6 个月的图片

所以我想使用 Cloud Function 做一个 cron 作业,我需要“查询”6 个多月前最后修改文件

如何使用 Admin SDK 做到这一点?

解决方法

我想我找到了解决办法

根据this answer,无法通过元数据查询

所以,现在我使用下面的代码来获取过时的文件

import * as admin from "firebase-admin";
import * as moment from "moment";

const app = admin.initializeApp(); // I am using Google Cloud Function,much more simple to initialize the app
const bucket = app.storage().bucket();

const response = await bucket.getFiles({prefix: "eventPoster/"}); // set your folder name in prefix here
const files = response[0];

const sixMonthAgo = moment().subtract(6,"months").toDate();
const outOfDateFiles = files.filter((file) => {
    const createdTime = new Date(file.metadata.timeCreated);
    return moment(createdTime).isBefore(sixMonthAgo);
});

console.log(`number of outdated files: ${outOfDateFiles.length}`);

阅读documentation in here以进一步阅读

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...