'Firebase Functions) Can I exclude certain files when importing them from the Storage bucket?

I'm going to delete old images using the schedule function. Before deleting these images, I have created thumbnail images, and I want to delete only the original images except for these thumbnail images.

The following is part of my code

scedule function

exports.scheduledDeleteFile = functions
  .region("asia-northeast3")
  .pubsub.schedule("every 5 minutes")
  .onRun(async (context) => {
    try {
      const bucket = firebase.storage().bucket();

      // get storage file
      const [filesArray] = await bucket.getFiles({
        prefix: "chat/files",
      });

      totalCount = filesArray.length;

      // variables with our settings to be reused below
      const now = Date.now();
      const time_ago = Date.now() - 180000; // 3min test
      const TIMESTAMP_AGO = new Date(time_ago); // change datetime
      const DELETE_OPTIONS = { ignoreNotFound: true }; // ??

      functions.logger.log(
        `Found ${totalCount} files that need to be checked.`
      );

      const deleteOldFileResults = await Promise.all(
        filesArray.map(async (file) => {
          let metadata;

          try {
            // 파일에 대한 메타데이터를 가져옴
            [metadata] = await file.getMetadata();

            // metadata of file
            const { temporaryHold, eventBasedHold, timeCreated } = metadata;

            const TIME_CREATED = new Date(timeCreated);

            const dispose = TIME_CREATED < TIMESTAMP_AGO;

            // delete
            if (dispose) {
              await file.delete(DELETE_OPTIONS);
              functions.logger.log("delete");
              disposedCount++;

              // ===================
              // firestore file chat 업데이트
              // 트리거 함수를 따로 만들어서 사용
            }

            return { file, metadata, disposed: dispose, skipped: activeHold };
          } catch (error) {}
        })
      );
    } catch (error) {}
  });

My thumbnail image is in the same path as the original file. Is there an option to exclude certain files when importing them? (For example, except for a file whose name precedes "thumb_")

await bucket.getFiles({
        prefix: "chat/files",
      });

The following is a create thumbnail function. I referred to the example provided by firebase. https://github.com/firebase/functions-samples/tree/main/2nd-gen/thumbnails

// thumb image name size
const THUMB_MAX_HEIGHT = 200;
const THUMB_MAX_WIDTH = 200;
// thumb image name
const THUMB_PREFIX = "thumb_";

exports.generateThumbnail = functions
  .region("asia-northeast3")
  .storage.object()
  .onFinalize(async (object) => {
    // custom metadata
    const userKey = object.metadata["userKey"];
    const chatroomKey = object.metadata["chatroomKey"];
    const type = object.metadata["type"];

    // File and directory paths.
    const filePath = object.name;
    const contentType = object.contentType; // This is the image MIME type
    const fileDir = path.dirname(filePath);
    const fileName = path.basename(filePath);
    const thumbFilePath = path.normalize(
      // ! if change path, error!
      path.join(fileDir, `${THUMB_PREFIX}${fileName}`)
    );

    const tempLocalFile = path.join(os.tmpdir(), filePath);
    const tempLocalDir = path.dirname(tempLocalFile);
    const tempLocalThumbFile = path.join(os.tmpdir(), thumbFilePath);

    if (!contentType.startsWith("image/")) {
      return functions.logger.log("This is not an image.");
    }

    if (fileName.startsWith(THUMB_PREFIX)) {
      return functions.logger.log("Already a Thumbnail.");
    }

    // Cloud Storage files.
    const bucket = admin.storage().bucket(object.bucket);
    const file = bucket.file(filePath);
    const thumbFile = bucket.file(thumbFilePath);
    const metadata = {
      contentType: contentType,
    };

    await mkdirp(tempLocalDir);
    // Download file from bucket.
    await file.download({ destination: tempLocalFile });
    functions.logger.log("The file has been downloaded to", tempLocalFile);

    // Generate a thumbnail using ImageMagick.
    await spawn(
      "convert",
      [
        tempLocalFile,
        "-thumbnail",
        `${THUMB_MAX_WIDTH}x${THUMB_MAX_HEIGHT}>`, 
        tempLocalThumbFile,
      ],
      { capture: ["stdout", "stderr"] }
    );

    functions.logger.log("Thumbnail created at", tempLocalThumbFile);

    // Uploading the Thumbnail.
    await bucket.upload(tempLocalThumbFile, {
      destination: thumbFilePath,
      metadata: metadata,
    });

    functions.logger.log("Thumbnail uploaded to Storage at", thumbFilePath);

    fs.unlinkSync(tempLocalFile);
    fs.unlinkSync(tempLocalThumbFile);

    const results = await Promise.all([
      thumbFile.getSignedUrl({
        action: "read",
        expires: "03-01-2500",
      }),
      file.getSignedUrl({
        action: "read",
        expires: "03-01-2500",
      }),
    ]);
    functions.logger.log("Got Signed URLs.");

    const thumbResult = results[0];
    const originalResult = results[1];
    const thumbFileUrl = thumbResult[0];
    const fileUrl = originalResult[0];

    await file.delete().then((value) => {
      functions.logger.log("원본 삭제 완료");
    });

    // Add the URLs to the Database
    await admin
      .database()
      .ref("images")
      .push({ path: fileUrl, thumbnail: thumbFileUrl });

    return functions.logger.log("Thumbnail URLs saved to database.");
  });


Solution 1:[1]

Is there an option to exclude certain files when importing them? (For example, except for a file whose name precedes "thumb_")

No. You can filter out the objects you don't want in the code that iterates the results.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Doug Stevenson