"""Create the application bucket and public object-read policy on RustFS.""" import json import os import boto3 from botocore.config import Config from botocore.exceptions import ClientError BUCKET = os.environ.get("STORAGE_BUCKET", "mineseeker") s3 = boto3.client( "s3", endpoint_url=os.environ["TARGET_ENDPOINT"], aws_access_key_id=os.environ["STORAGE_ACCESS_KEY"], aws_secret_access_key=os.environ["STORAGE_SECRET_KEY"], region_name="us-east-1", config=Config(signature_version="s3v4", s3={"addressing_style": "path"}), ) try: s3.head_bucket(Bucket=BUCKET) except ClientError as error: status = error.response.get("ResponseMetadata", {}).get("HTTPStatusCode") code = error.response.get("Error", {}).get("Code", "") if status != 404 and code not in {"NoSuchBucket", "404", "NotFound"}: raise s3.create_bucket(Bucket=BUCKET) policy = { "Version": "2012-10-17", "Statement": [ { "Effect": "Allow", "Principal": "*", "Action": ["s3:GetObject"], "Resource": [ f"arn:aws:s3:::{BUCKET}/media/*", f"arn:aws:s3:::{BUCKET}/cache/*", ], } ], } s3.put_bucket_policy(Bucket=BUCKET, Policy=json.dumps(policy)) print(f"RustFS bucket {BUCKET!r} is initialized", flush=True)