137 lines
No EOL
3.8 KiB
Bash
137 lines
No EOL
3.8 KiB
Bash
#!/bin/sh
|
|
# ============================================================================
|
|
# MinIO Bucket Initialization Script
|
|
# Automatically creates all required buckets and policies on startup
|
|
# ============================================================================
|
|
|
|
set -e
|
|
|
|
echo "============================================"
|
|
echo "Starting MinIO Bucket Initialization"
|
|
echo "============================================"
|
|
|
|
# Wait for MinIO to be ready
|
|
echo "→ Waiting for MinIO to be ready..."
|
|
sleep 5
|
|
|
|
# Configure MinIO client with credentials from environment
|
|
echo "→ Configuring MinIO client..."
|
|
mc alias set local http://${MINIO_HOST}:${MINIO_PORT} ${MINIO_ROOT_USER} ${MINIO_ROOT_PASSWORD}
|
|
|
|
# Create all required buckets
|
|
echo "→ Creating buckets..."
|
|
mc mb local/text-files --ignore-existing
|
|
mc mb local/image-files --ignore-existing
|
|
mc mb local/audio-files --ignore-existing
|
|
mc mb local/video-files --ignore-existing
|
|
mc mb local/document-files --ignore-existing
|
|
mc mb local/pipeline-artifacts --ignore-existing
|
|
mc mb local/uploads --ignore-existing
|
|
mc mb local/backups --ignore-existing
|
|
mc mb local/didi-prod --ignore-existing # single-bucket mode (MINIO_BUCKET=didi-prod) — media upload/download
|
|
|
|
echo "✓ All buckets created"
|
|
|
|
# Enable versioning for important buckets
|
|
echo "→ Enabling versioning..."
|
|
mc version enable local/pipeline-artifacts
|
|
mc version enable local/backups
|
|
echo "✓ Versioning enabled for pipeline-artifacts and backups"
|
|
|
|
# Set lifecycle policies for temporary files
|
|
echo "→ Setting lifecycle policies..."
|
|
cat > /tmp/lifecycle-30days.json <<EOF
|
|
{
|
|
"Rules": [
|
|
{
|
|
"ID": "expire-30days",
|
|
"Status": "Enabled",
|
|
"Expiration": {
|
|
"Days": 30
|
|
}
|
|
}
|
|
]
|
|
}
|
|
EOF
|
|
|
|
cat > /tmp/lifecycle-60days.json <<EOF
|
|
{
|
|
"Rules": [
|
|
{
|
|
"ID": "expire-60days",
|
|
"Status": "Enabled",
|
|
"Expiration": {
|
|
"Days": 60
|
|
}
|
|
}
|
|
]
|
|
}
|
|
EOF
|
|
|
|
# Apply lifecycle policies
|
|
mc ilm import local/text-files < /tmp/lifecycle-30days.json
|
|
mc ilm import local/audio-files < /tmp/lifecycle-30days.json
|
|
mc ilm import local/video-files < /tmp/lifecycle-30days.json
|
|
mc ilm import local/image-files < /tmp/lifecycle-60days.json
|
|
mc ilm import local/uploads < /tmp/lifecycle-30days.json
|
|
|
|
echo "✓ Lifecycle policies configured"
|
|
|
|
# Create anonymous read policy for public buckets (optional)
|
|
# Uncomment if you want public read access to certain buckets
|
|
# echo "→ Setting public access policies..."
|
|
# mc anonymous set download local/image-files
|
|
# mc anonymous set download local/video-files
|
|
# echo "✓ Public read access configured"
|
|
|
|
# Create service accounts for microservices (optional)
|
|
# This creates restricted access for each service
|
|
echo "→ Creating service access policies..."
|
|
|
|
# Policy for text analysis service
|
|
cat > /tmp/text-service-policy.json <<EOF
|
|
{
|
|
"Version": "2012-10-17",
|
|
"Statement": [
|
|
{
|
|
"Effect": "Allow",
|
|
"Action": ["s3:*"],
|
|
"Resource": ["arn:aws:s3:::text-files/*"]
|
|
}
|
|
]
|
|
}
|
|
EOF
|
|
|
|
# Policy for image analysis service
|
|
cat > /tmp/image-service-policy.json <<EOF
|
|
{
|
|
"Version": "2012-10-17",
|
|
"Statement": [
|
|
{
|
|
"Effect": "Allow",
|
|
"Action": ["s3:*"],
|
|
"Resource": ["arn:aws:s3:::image-files/*"]
|
|
}
|
|
]
|
|
}
|
|
EOF
|
|
|
|
# Apply policies (these can be used to create service accounts later)
|
|
mc admin policy create local text-service-policy /tmp/text-service-policy.json || true
|
|
mc admin policy create local image-service-policy /tmp/image-service-policy.json || true
|
|
|
|
echo "✓ Service policies created"
|
|
|
|
# List all buckets to confirm
|
|
echo ""
|
|
echo "============================================"
|
|
echo "Initialization Complete!"
|
|
echo "============================================"
|
|
echo "Buckets created:"
|
|
mc ls local/
|
|
echo "============================================"
|
|
|
|
# Clean up temp files
|
|
rm -f /tmp/lifecycle-*.json /tmp/*-policy.json
|
|
|
|
echo "MinIO is ready for use!" |