#!/bin/bash
# S3 log file download script usage example (optimized version, supports massive file scanning)

# Configure AWS credentials (choose one of the following methods)

# Method 1: Use AWS config file (recommended)
# export AWS_PROFILE=your-profile-name

# Method 2: Use environment variables
# export AWS_ACCESS_KEY_ID=your-access-key
# export AWS_SECRET_ACCESS_KEY=your-secret-key
# export AWS_DEFAULT_REGION=us-east-1

# Download parameters
BUCKET="o11y-prod-shared-us-east-1"
PREFIX="0/1372813089209061633/1372813089454538353/1379661944646416073/logs/statementlog"
START_TIME="1760065208"
END_TIME="1760067008"
TRUE_START_TIME="1760058008"
TRUE_END_TIME="1760065208"
OUTPUT_DIR="./statement_logs"
export AWS_ACCESS_KEY_ID="<REDACTED>"
export AWS_SECRET_ACCESS_KEY="<REDACTED>"
export AWS_SESSION_TOKEN="<REDACTED>"
python3 -m venv .venv
source .venv/bin/activate
pip install -r requirements.txt
echo "Starting to download statement log files..."
echo "Time range: $(date -d @$START_TIME) to $(date -d @$END_TIME)"
echo "S3 path: s3://$BUCKET/$PREFIX"
echo "Local directory: $OUTPUT_DIR"
echo ""

# Create output directory
mkdir -p "$OUTPUT_DIR"

# Run Python download script (with increased memory and timeout settings to handle massive files)
echo "Note: Since there are tens of millions of files in the directory, scanning may take some time..."
python s3_downloader.py \
    --bucket "$BUCKET" \
    --prefix "$PREFIX" \
    --start-time "$START_TIME" \
    --end-time "$END_TIME" \
    --output-dir "$OUTPUT_DIR" \
    --region "us-east-1"

echo ""
echo "Download completed!"
