#!/bin/sh set -eu : "${MINIO_ROOT_USER:?}" : "${MINIO_ROOT_PASSWORD:?}" : "${S3_BUCKET:?}" : "${S3_ACCESS_KEY:?}" : "${S3_SECRET_KEY:?}" if [ "$S3_ACCESS_KEY" = "$MINIO_ROOT_USER" ]; then echo "API must use a separate MinIO user" >&2 exit 1 fi if [ "$S3_SECRET_KEY" = "$MINIO_ROOT_PASSWORD" ]; then echo "API and administrator passwords must differ" >&2 exit 1 fi case "$S3_BUCKET" in ""|*[!a-z0-9.-]*) echo "Invalid bucket name" >&2 exit 1 ;; esac mc alias set storage https://minio:9000 \ "$MINIO_ROOT_USER" "$MINIO_ROOT_PASSWORD" >/dev/null mc mb --ignore-existing "storage/$S3_BUCKET" mc anonymous set none "storage/$S3_BUCKET" for role in api worker; do user="messenger_$role" if [ "$role" = api ]; then password="$S3_SECRET_KEY" write_prefix=staging else password="$WORKER_S3_PASSWORD" write_prefix=originals fi read_actions='"s3:GetObject"' if [ "$role" = api ]; then read_actions='"s3:GetObject","s3:DeleteObject"'; fi extra="" if [ "$role" = worker ]; then extra=',"arn:aws:s3:::'"$S3_BUCKET"'/variants/*"' fi cat > /tmp/policy.json </dev/null mc admin user add storage "$user" "$password" >/dev/null if ! mc admin user info --json storage "$user" | grep -Eq '"policyName"[[:space:]]*:[[:space:]]*"'"$policy"'"'; then mc admin policy attach storage "$policy" --user "$user" >/dev/null fi done