简体   繁体   中英

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 .

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. It seems the * is not picked up by the JavaScript API. With gsutil, this wildcard does work. When I enter the full name of an object, it is deleted successfully according to the scheduled time.

You can't do this with most cloud storage APIs. 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 .

So, use getFiles() and do the pattern matching in your code to create a list of matching files.

Alright, so with the information from @jarmod, I updated the code and got it working as follows:

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

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