问题描述
我需要删除云存储中 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以进一步阅读