简体   繁体   English

每日从 Google Cloud Firebase 以.png 结尾的存储桶删除文件

[英]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.

相关问题 在来自 Google Cloud Storage 的 javascript 中未经授权列出存储桶中的文件 - List files in a bucket without authorisation in javascript from Google Cloud Storage 无法使用Firebase功能删除Google Cloud Storage文件 - Cannot delete Google Cloud Storage files using Firebase Functions 使用PHP将文件上传到Google Cloud Storage Bucket - Upload files to Google Cloud Storage Bucket using PHP Firebase 函数 - 从 Google 云存储桶中检索文件导致 RangeError:超出最大调用堆栈大小 - Firebase functions - retrieving files from Google cloud bucket results in RangeError: Maximum call stack size exceeded 从 Firebase Cloud Function 在 Google Cloud Bucket 中创建文件 - Create File in Google Cloud Bucket from Firebase Cloud Function 如何从 Firebase 云 Function 中删除 Firebase 存储文件夹? - How to delete a Firebase Storage folder from a Firebase Cloud Function? 如何将映像从Expressjs更新到Google Cloud Storage / Firebase Storage - How update an image from Expressjs to Google Cloud Storage / Firebase Storage Firebase Admin SDK可下载/检索Google Cloud Storage上的文件 - Firebase Admin SDK to download/retrieve files on Google Cloud Storage 从 Google firebase 存储文件中检索前缀 - Retrieve prefixes from Google firebase storage files Cloud Storage for Firebase SDK 6.1.0:无法使用listAll()列出存储桶中的文件 - Cloud Storage for Firebase SDK 6.1.0 : can't list files in a bucket using listAll()
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM