[英]Daily delete files from Google Cloud Firebase Storage bucket that end with .png
I want to run a Firebase Cloud Function scheduler that once per day deletes all files in a specific bucket that end with .png
.我想运行一个 Firebase Cloud Function 调度程序,每天一次删除特定存储桶中以.png
结尾的所有文件。
This is what I have tried:这是我尝试过的:
// The Cloud Functions for Firebase SDK to create Cloud Functions and setup triggers.
const functions = require('firebase-functions');
// The Firebase Admin SDK to access Cloud Firestore.
const admin = require('firebase-admin');
admin.initializeApp();
exports.scheduledFunctionCrontab = functions.pubsub.schedule('0 0 * * *')
.timeZone('Europe/Amsterdam')
.onRun((context) => {
console.log('This will be run every day at midnight in NL!');
const bucketName = '<myBucketName>'; //where I replaced this with my actual bucketname
const filename = '*.png';
// Imports the Google Cloud client library
const {Storage} = require('@google-cloud/storage');
// Creates a client
const storage = new Storage();
async function deleteFile() {
// Deletes the file from the bucket
await storage.bucket(bucketName).file(filename).delete();
console.log(`gs://${bucketName}/${filename} deleted.`);
}
deleteFile().catch(console.error);
return null;
});
But I get a message: 'No such object: <myBucketName>/*.png'
error.但我收到一条message: 'No such object: <myBucketName>/*.png'
错误。 It seems the * is not picked up by the JavaScript API.似乎 * 没有被 JavaScript API 拾取。 With gsutil, this wildcard does work.使用 gsutil,此通配符确实有效。 When I enter the full name of an object, it is deleted successfully according to the scheduled time.当我输入一个object的全名时,按照预定时间删除成功。
You can't do this with most cloud storage APIs.大多数云存储 API 无法做到这一点。 They generally don't support wildcards.它们通常不支持通配符。 The gsutil
CLI is doing the file matching for you but under the covers it's using the API and matching the results to *.png
. gsutil
CLI 正在为您进行文件匹配,但在幕后它使用 API 并将结果与*.png
匹配。
So, use getFiles() and do the pattern matching in your code to create a list of matching files.因此,使用getFiles()并在代码中进行模式匹配以创建匹配文件列表。
Alright, so with the information from @jarmod, I updated the code and got it working as follows:好的,所以使用来自@jarmod 的信息,我更新了代码并使其工作如下:
// The Cloud Functions for Firebase SDK to create Cloud Functions and setup triggers.
const functions = require('firebase-functions');
// The Firebase Admin SDK to access Cloud Firestore.
const admin = require('firebase-admin');
admin.initializeApp();
exports.scheduledFunctionCrontab = functions.pubsub.schedule('0 0 * * *')
.timeZone('Europe/Amsterdam')
.onRun((context) => {
console.log('This will be run every day at midnight in NL!');
/**
* TODO(developer): Uncomment the following lines before running the sample.
*/
const bucketName = '<myBucketName>'; //where I replaced this with my actual bucketname
// Imports the Google Cloud client library
const {Storage} = require('@google-cloud/storage');
// Creates a client
const storage = new Storage();
async function deleteFile(filename) {
// Deletes the file from the bucket
await storage.bucket(bucketName).file(filename).delete();
console.log(`gs://${bucketName}/${filename} deleted.`);
}
async function listAndDeleteFiles() {
// Lists files in the bucket
const [files] = await storage.bucket(bucketName).getFiles();
console.log('Files:');
files.forEach(file => {
console.log(file.name);
if (file.name.endsWith(".png")) {
deleteFile(file.name);
}
});
}
listAndDeleteFiles().catch(console.error);
return null;
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.