简体   繁体   中英

how to get files from Cloud Storage for Firebase that created at a certain time?

在此处输入图像描述

I need to delete images at eventPoster folder in my cloud storage that has last modified metadata more than 6 months ago.

so I want to make a cron job using Cloud Function, and I need to 'query' that files that has last modified more than 6 months ago

how to do that using Admin SDK?

I think I find the solution

according to this answer , There is no way to query by metadata

so, as for now I use the code below to get out of date files

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}`);

read the documentation in here for further reading

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM