You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

能否通过Linux Shell脚本从Cloud Storage获取文件?流程实现咨询

Can a Shell Script Handle Firebase Cloud Storage Monitoring, Downloading, Processing, and Deleting?

Absolutely! Shell scripts are fully capable of executing this entire workflow for Firebase Cloud Storage. I’ve helped folks set up similar automation before, so let me walk you through how to make it work.

Prerequisites First

First, you’ll need the Firebase CLI installed on your home computer—it’s the bridge between your shell and Firebase Cloud Storage. Install it with:

curl -sL https://firebase.tools | bash

Since this script will run in the background (no user interaction), log in with a CI token to avoid needing manual authentication every time:

firebase login:ci

Save the generated token somewhere safe, and set it as an environment variable in your script so the CLI can use it:

export FIREBASE_TOKEN="your-ci-token-here"

Core Workflow Breakdown

Here’s how to structure your script to handle each step:

1. Monitor for New Files

The simplest approach for a shell script is polling (checking the bucket at regular intervals). You can track which files you’ve already processed using a log file to avoid reprocessing.

2. Download New Files

Use the Firebase CLI’s storage:download command to pull files from your bucket to your local machine.

3. Process Files

Shell scripts can call any command-line tools you need for processing—image resizing, video transcoding, document conversion, you name it. Just install the relevant tools (like imagemagick for images, ffmpeg for videos, or pandoc for documents) first.

4. Delete Processed Files from Cloud Storage

Once processing is done, use storage:delete to remove the original file from Firebase Cloud Storage.

Example Script

Here’s a ready-to-adapt script that ties all these steps together:

#!/bin/bash

# Configure your settings here
BUCKET="gs://your-firebase-storage-bucket-name"
LOCAL_DOWNLOAD_DIR="./firebase_uploads"
PROCESSED_LOG="./processed_files.log"
CHECK_INTERVAL=30  # Seconds between checks

# Set up necessary directories and files
mkdir -p "$LOCAL_DOWNLOAD_DIR"
touch "$PROCESSED_LOG"

# Main loop to monitor the bucket
while true; do
    # Get a list of all files in the bucket (recursive)
    current_files=$(firebase storage:list "$BUCKET" --recursive | grep -v "Listing objects" | grep -v "Total objects")

    # Check each file to see if it's new
    echo "$current_files" | while read -r file_path; do
        # Skip empty lines
        [[ -z "$file_path" ]] && continue

        if ! grep -q "^$file_path$" "$PROCESSED_LOG"; then
            echo "Found new file: $file_path"

            # Download the file to local directory
            firebase storage:download "$BUCKET/$file_path" "$LOCAL_DOWNLOAD_DIR/$file_path"
            
            # --- Add your processing logic here ---
            # Example 1: Resize images (requires imagemagick)
            if [[ "$file_path" =~ \.(jpg|jpeg|png)$ ]]; then
                convert "$LOCAL_DOWNLOAD_DIR/$file_path" -resize 50% "$LOCAL_DOWNLOAD_DIR/resized_$file_path"
                echo "Resized image: $file_path"
            fi
            # Example 2: Convert video to webm (requires ffmpeg)
            # if [[ "$file_path" =~ \.mp4$ ]]; then
            #     ffmpeg -i "$LOCAL_DOWNLOAD_DIR/$file_path" "$LOCAL_DOWNLOAD_DIR/converted_${file_path%.mp4}.webm"
            #     echo "Converted video: $file_path"
            # fi
            
            # Delete the original file from Cloud Storage
            firebase storage:delete "$BUCKET/$file_path"
            echo "Deleted cloud file: $file_path"

            # Mark the file as processed to avoid rechecking
            echo "$file_path" >> "$PROCESSED_LOG"
        fi
    done

    # Wait before checking again
    sleep "$CHECK_INTERVAL"
done

Pro Tips for Reliability

  • Run in the background: Use nohup ./your-script.sh & to keep the script running even if you log out of your computer. For better reliability, set it up as a systemd service so it starts automatically on boot.
  • Handle errors: Add basic error checking (e.g., check if firebase storage:download succeeded before processing/deleting) to avoid losing files if something goes wrong.
  • Manage disk space: Add a cleanup step to delete local processed files after a certain period to prevent your drive from filling up.

内容的提问来源于stack exchange,提问作者Jurgen

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.27 03:44:34