95 lines
2.9 KiB
Python

import mysql
def generate_new_s3_key(old_key, prefix):
"""
Generates a new S3 key by prepending the destination prefix to the old key,
thereby preserving the original path structure.
"""
if not isinstance(old_key, str) or not old_key:
return None
# Ensure the prefix ends with a slash if it's not empty and doesn't already end with one.
prefixed_path = prefix
if prefix and not prefix.endswith('/'):
prefixed_path += '/'
# Combine the prefix and the old key. If the old key starts with a slash, strip it.
final_key = old_key.lstrip('/')
return f"{prefixed_path}{final_key}"
def move_s3_object(s3_client, source_bucket, source_key, dest_bucket, dest_key):
"""
Performs the S3 'move' operation (Copy + Delete).
Returns True on success, False otherwise.
"""
copy_source = {
'Bucket': source_bucket,
'Key': source_key
}
try:
print(f" -> Copying '{source_key}' to '{dest_key}' in '{dest_bucket}'...")
# 1. Copy the object
s3_client.copy_object(
CopySource=copy_source,
Bucket=dest_bucket,
Key=dest_key
)
print(" -> Copy object successful.")
# print(f" -> Deleting original object from '{source_bucket}/{source_key}'...")
# # 2. Delete the original object
# s3_client.delete_object(
# Bucket=source_bucket,
# Key=source_key
# )
# print(" -> Delete object successful.")
return True
except Exception as e:
print(f" -> S3 Move FAILED for {source_key}: {e}")
return False
def update_document_key(db_config_full, doc_id, new_s3_key):
"""
Updates the S3Key for a specific document ID in the database.
NOTE: db_config_full includes the 'table' key, which must be filtered out for the connection.
"""
conn = None
cursor = None
# Separate connection parameters from the table name
table_name = db_config_full['table']
connection_config = {k: v for k, v in db_config_full.items() if k != 'table'}
try:
# Connect using only valid connection parameters
conn = mysql.connector.connect(**connection_config)
cursor = conn.cursor()
# Prepare the UPDATE query
update_query = (
f"UPDATE {table_name} SET S3Key = %s WHERE ID = %s"
)
cursor.execute(update_query, (new_s3_key, doc_id))
# Commit the transaction to apply the changes
conn.commit()
print(f" -> DB Update SUCCESS for ID {doc_id}.")
return True
except mysql.connector.Error as err:
print(f" -> DB Update FAILED for ID {doc_id}: {err}. Rolling back.")
if conn:
conn.rollback()
return False
finally:
if cursor:
cursor.close()
if conn and conn.is_connected():
conn.close()