2022-11-17 17:08:20 +00:00
|
|
|
import uuid
|
|
|
|
|
2023-08-08 15:57:06 +00:00
|
|
|
from helpers.cmd import execute_cmd, log
|
2022-11-17 17:08:20 +00:00
|
|
|
|
|
|
|
|
2024-02-05 15:56:36 +00:00
|
|
|
def create_bucket(endpoint, versioning, location, acl, no_verify_ssl):
|
2024-07-02 17:45:31 +00:00
|
|
|
configuration = ""
|
2022-11-17 17:08:20 +00:00
|
|
|
if location:
|
2024-07-02 17:45:31 +00:00
|
|
|
configuration = f"--create-bucket-configuration 'LocationConstraint={location}'"
|
2024-02-05 15:56:36 +00:00
|
|
|
if acl:
|
|
|
|
acl = f"--acl {acl}"
|
|
|
|
|
2022-11-17 17:08:20 +00:00
|
|
|
bucket_name = str(uuid.uuid4())
|
2023-07-13 12:50:03 +00:00
|
|
|
no_verify_ssl_str = "--no-verify-ssl" if no_verify_ssl else ""
|
|
|
|
cmd_line = f"aws {no_verify_ssl_str} s3api create-bucket --bucket {bucket_name} " \
|
2024-07-02 17:45:31 +00:00
|
|
|
f"--endpoint {endpoint} {configuration} {acl} "
|
2023-07-13 12:50:03 +00:00
|
|
|
cmd_line_ver = f"aws {no_verify_ssl_str} s3api put-bucket-versioning --bucket {bucket_name} " \
|
2024-11-06 13:47:56 +00:00
|
|
|
f"--versioning-configuration Status=Enabled --endpoint {endpoint}"
|
2022-11-17 17:08:20 +00:00
|
|
|
|
2023-08-08 15:57:06 +00:00
|
|
|
output, success = execute_cmd(cmd_line)
|
2022-11-17 17:08:20 +00:00
|
|
|
|
2023-08-08 15:57:06 +00:00
|
|
|
if not success and "succeeded and you already own it" not in output:
|
|
|
|
log(f"{cmd_line}\n"
|
|
|
|
f"Bucket {bucket_name} has not been created:\n"
|
|
|
|
f"Error: {output}", endpoint)
|
2023-05-23 16:04:38 +00:00
|
|
|
return False
|
2022-11-17 17:08:20 +00:00
|
|
|
|
2024-11-06 13:47:56 +00:00
|
|
|
if versioning:
|
2023-08-08 15:57:06 +00:00
|
|
|
output, success = execute_cmd(cmd_line_ver)
|
2022-11-17 17:08:20 +00:00
|
|
|
if not success:
|
2023-08-08 15:57:06 +00:00
|
|
|
log(f"{cmd_line_ver}\n"
|
|
|
|
f"Bucket versioning has not been applied for bucket {bucket_name}\n"
|
|
|
|
f"Error: {output}", endpoint)
|
2022-11-17 17:08:20 +00:00
|
|
|
else:
|
2023-08-08 15:57:06 +00:00
|
|
|
log(f"Bucket versioning has been applied for bucket {bucket_name}", endpoint)
|
2023-06-28 17:21:43 +00:00
|
|
|
|
2024-07-02 17:45:31 +00:00
|
|
|
log(f"Created bucket: {bucket_name} ({location})", endpoint)
|
2022-11-17 17:08:20 +00:00
|
|
|
return bucket_name
|
|
|
|
|
|
|
|
|
2023-07-13 12:50:03 +00:00
|
|
|
def upload_object(bucket, payload_filepath, endpoint, no_verify_ssl):
|
2022-11-17 17:08:20 +00:00
|
|
|
object_name = str(uuid.uuid4())
|
2023-07-13 12:50:03 +00:00
|
|
|
no_verify_ssl_str = "--no-verify-ssl" if no_verify_ssl else ""
|
|
|
|
cmd_line = f"aws {no_verify_ssl_str} s3api put-object --bucket {bucket} --key {object_name} " \
|
|
|
|
f"--body {payload_filepath} --endpoint {endpoint}"
|
2023-08-08 15:57:06 +00:00
|
|
|
output, success = execute_cmd(cmd_line)
|
2022-11-17 17:08:20 +00:00
|
|
|
|
|
|
|
if not success:
|
2023-08-08 15:57:06 +00:00
|
|
|
log(f"{cmd_line}\n"
|
|
|
|
f"Object {object_name} has not been uploaded\n"
|
|
|
|
f"Error: {output}", endpoint)
|
2022-11-17 17:08:20 +00:00
|
|
|
return False
|
2023-06-28 17:21:43 +00:00
|
|
|
|
|
|
|
return bucket, endpoint, object_name
|