forked from mtusi2026/mtusi2026lab7
36 lines
1.1 KiB
Python
36 lines
1.1 KiB
Python
import os
|
|
import boto3
|
|
from botocore.client import Config
|
|
|
|
S3_ENDPOINT = os.getenv("S3_ENDPOINT", "https://s3:8333")
|
|
S3_BUCKET = os.getenv("S3_BUCKET", "messenger-attachments")
|
|
S3_ROOT_ACCESS_KEY = os.getenv("S3_ROOT_ACCESS_KEY", "minioadmin")
|
|
S3_ROOT_SECRET_KEY = os.getenv("S3_ROOT_SECRET_KEY", "minioadminpassword")
|
|
AWS_CA_BUNDLE = os.getenv("AWS_CA_BUNDLE")
|
|
|
|
client_kwargs = {
|
|
"service_name": "s3",
|
|
"endpoint_url": S3_ENDPOINT,
|
|
"aws_access_key_id": S3_ROOT_ACCESS_KEY,
|
|
"aws_secret_access_key": S3_ROOT_SECRET_KEY,
|
|
"config": Config(signature_version="s3v4"),
|
|
"region_name": "us-east-1",
|
|
}
|
|
if S3_ENDPOINT.startswith("https://") and AWS_CA_BUNDLE:
|
|
client_kwargs["verify"] = AWS_CA_BUNDLE
|
|
|
|
s3_client = boto3.client(**client_kwargs)
|
|
|
|
|
|
def init_bucket():
|
|
print(f"Checking/Creating bucket: {S3_BUCKET}")
|
|
try:
|
|
s3_client.head_bucket(Bucket=S3_BUCKET)
|
|
print("Bucket already exists.")
|
|
except Exception:
|
|
s3_client.create_bucket(Bucket=S3_BUCKET)
|
|
print("Bucket successfully created.")
|
|
|
|
|
|
if __name__ == "__main__":
|
|
init_bucket() |