forked from mtusi2026/mtusi2026lab4
43 lines
1.3 KiB
Python
43 lines
1.3 KiB
Python
import os
|
|
import time
|
|
|
|
import boto3
|
|
from botocore.config import Config
|
|
from botocore.exceptions import ClientError, EndpointConnectionError
|
|
|
|
from config import secret
|
|
|
|
|
|
def main():
|
|
bucket = os.environ["S3_BUCKET"]
|
|
s3 = boto3.client(
|
|
"s3", endpoint_url=os.environ["S3_ENDPOINT"], region_name=os.environ["S3_REGION"],
|
|
aws_access_key_id=secret("S3_ADMIN_ACCESS_KEY"),
|
|
aws_secret_access_key=secret("S3_ADMIN_SECRET_KEY"),
|
|
config=Config(connect_timeout=1, read_timeout=2, retries={"total_max_attempts": 1},
|
|
request_checksum_calculation="when_required", s3={"addressing_style": "path"}),
|
|
)
|
|
deadline = time.monotonic() + 45
|
|
while True:
|
|
try:
|
|
s3.head_bucket(Bucket=bucket)
|
|
break
|
|
except ClientError as error:
|
|
if error.response["ResponseMetadata"]["HTTPStatusCode"] != 404:
|
|
raise
|
|
try:
|
|
s3.create_bucket(Bucket=bucket)
|
|
except ClientError as conflict:
|
|
if conflict.response["Error"]["Code"] != "BucketAlreadyOwnedByYou":
|
|
raise
|
|
break
|
|
except EndpointConnectionError:
|
|
if time.monotonic() >= deadline:
|
|
raise
|
|
time.sleep(0.5)
|
|
print("Private S3 bucket is ready")
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|