2022-11-17 17:08:20 +00:00
|
|
|
import uuid
|
|
|
|
|
|
|
|
from helpers.cmd import execute_cmd
|
|
|
|
|
|
|
|
|
|
|
|
def create_bucket(endpoint, versioning, location):
|
|
|
|
if location:
|
|
|
|
location = f"--create-bucket-configuration 'LocationConstraint={location}'"
|
|
|
|
bucket_name = str(uuid.uuid4())
|
|
|
|
|
|
|
|
cmd_line = f"aws --no-verify-ssl s3api create-bucket --bucket {bucket_name} " \
|
2023-05-11 12:08:16 +00:00
|
|
|
f"--endpoint http://{endpoint} {location}"
|
2022-11-17 17:08:20 +00:00
|
|
|
cmd_line_ver = f"aws --no-verify-ssl s3api put-bucket-versioning --bucket {bucket_name} " \
|
2023-05-11 12:08:16 +00:00
|
|
|
f"--versioning-configuration Status=Enabled --endpoint http://{endpoint} "
|
2022-11-17 17:08:20 +00:00
|
|
|
|
|
|
|
out, success = execute_cmd(cmd_line)
|
|
|
|
|
2023-05-23 16:04:38 +00:00
|
|
|
if not success and "succeeded and you already own it" not in out:
|
|
|
|
print(f" > Bucket {bucket_name} has not been created:\n{out}")
|
|
|
|
return False
|
|
|
|
|
|
|
|
print(f"cmd: {cmd_line}")
|
2022-11-17 17:08:20 +00:00
|
|
|
|
2023-05-23 16:04:38 +00:00
|
|
|
if versioning == "True":
|
2022-11-17 17:08:20 +00:00
|
|
|
out, success = execute_cmd(cmd_line_ver)
|
|
|
|
if not success:
|
2023-03-31 10:30:33 +00:00
|
|
|
print(f" > Bucket versioning has not been applied for bucket {bucket_name}:\n{out}")
|
2022-11-17 17:08:20 +00:00
|
|
|
else:
|
|
|
|
print(f" > Bucket versioning has been applied.")
|
2023-06-28 17:21:43 +00:00
|
|
|
|
|
|
|
print(f"Created bucket: {bucket_name} via endpoint {endpoint}")
|
2022-11-17 17:08:20 +00:00
|
|
|
return bucket_name
|
|
|
|
|
|
|
|
|
|
|
|
def upload_object(bucket, payload_filepath, endpoint):
|
|
|
|
object_name = str(uuid.uuid4())
|
|
|
|
|
2023-03-31 10:30:33 +00:00
|
|
|
cmd_line = f"aws --no-verify-ssl s3api put-object --bucket {bucket} --key {object_name} " \
|
2023-05-11 12:08:16 +00:00
|
|
|
f"--body {payload_filepath} --endpoint http://{endpoint}"
|
2022-11-17 17:08:20 +00:00
|
|
|
out, success = execute_cmd(cmd_line)
|
|
|
|
|
|
|
|
if not success:
|
|
|
|
print(f" > Object {object_name} has not been uploaded.")
|
|
|
|
return False
|
2023-06-28 17:21:43 +00:00
|
|
|
|
|
|
|
return bucket, endpoint, object_name
|