Files
mtusi2026lab7/app/init_storage.py
T
2026-11-20 20:37:41 +03:00

32 lines
1.1 KiB
Python

"""Bounded, authenticated S3 bucket bootstrap, without MinIO client images."""
import time
from botocore.exceptions import ClientError
from app.common import s3, bucket
def main():
for attempt in range(60):
try:
client = s3()
try:
client.head_bucket(Bucket=bucket())
except ClientError as exc:
if exc.response['Error']['Code'] not in ('404', 'NoSuchBucket', 'NotFound'):
raise
client.create_bucket(Bucket=bucket())
print('S3 bucket ready')
return
except Exception as exc:
if attempt == 59:
detail = type(exc).__name__
if isinstance(exc, ClientError):
status = exc.response.get('ResponseMetadata', {}).get('HTTPStatusCode')
if isinstance(status, int):
detail += f', HTTP {status}'
raise SystemExit(f'S3 bootstrap failed ({detail})') from None
time.sleep(2)
if __name__ == '__main__':
main()