ЛР5: асинхронная обработка вложений (RabbitMQ + worker), transactional outbox, варианты, трек 5.1
This commit is contained in:
@@ -0,0 +1,57 @@
|
||||
"""Idempotent S3 bucket bootstrap (one-shot compose service).
|
||||
|
||||
Creates the private attachments bucket with the root credentials before any API
|
||||
replica starts. Re-running is safe; no credentials are printed.
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
import os
|
||||
import time
|
||||
|
||||
import boto3
|
||||
from botocore.client import Config
|
||||
from botocore.exceptions import BotoCoreError, ClientError
|
||||
|
||||
|
||||
def _read(env_key: str) -> str:
|
||||
with open(os.environ[env_key], encoding="utf-8") as handle:
|
||||
return handle.read().strip()
|
||||
|
||||
|
||||
def main() -> None:
|
||||
endpoint = os.environ["S3_ENDPOINT"]
|
||||
region = os.environ.get("S3_REGION", "us-east-1")
|
||||
bucket = os.environ["S3_BUCKET"]
|
||||
client = boto3.client(
|
||||
"s3",
|
||||
endpoint_url=endpoint,
|
||||
region_name=region,
|
||||
aws_access_key_id=_read("S3_ROOT_ACCESS_KEY_ID_FILE"),
|
||||
aws_secret_access_key=_read("S3_ROOT_SECRET_ACCESS_KEY_FILE"),
|
||||
config=Config(
|
||||
connect_timeout=2,
|
||||
read_timeout=5,
|
||||
retries={"max_attempts": 1},
|
||||
s3={"addressing_style": "path"},
|
||||
),
|
||||
)
|
||||
|
||||
last: Exception | None = None
|
||||
for attempt in range(1, 31):
|
||||
try:
|
||||
try:
|
||||
client.head_bucket(Bucket=bucket)
|
||||
print(f"bucket {bucket} already present")
|
||||
except ClientError:
|
||||
client.create_bucket(Bucket=bucket)
|
||||
print(f"bucket {bucket} created")
|
||||
return
|
||||
except (ClientError, BotoCoreError) as exc:
|
||||
last = exc
|
||||
print(f"bucket bootstrap not ready (attempt {attempt}/30)")
|
||||
time.sleep(2)
|
||||
raise SystemExit(f"bucket bootstrap failed: {last}")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
Reference in New Issue
Block a user