# noqa: D100 from src.connectors.ack_storage import AckStorage import config import boto3 import os TMP_ACKS_DIR = '/tmp/acks/' class S3(AckStorage): # noqa: D101 def __init__(self): # noqa: D107 self.client = boto3.client( 's3', aws_access_key_id=config.TIKTOK_AWS_ACCESS_KEY_ID, aws_secret_access_key=config.TIKTOK_AWS_SECRET_ACCESS_KEY, region_name=config.DEFAULT_AWS_REGION ) def acks(self, unacked_deliveries): # noqa: D102 if not os.path.exists(TMP_ACKS_DIR): os.makedirs(TMP_ACKS_DIR) acks = [] for unacked_delivery in unacked_deliveries: ack_found = False response = None for S3_ACK_PREFIX in config.S3_ACK_PREFIXES: response = self.client.list_objects_v2( Bucket=config.S3_ACK_BUCKET, Prefix=f"{S3_ACK_PREFIX}/{unacked_delivery['sfn_execution_id'].strip()}/" ) count = response.get('KeyCount', 0) if count > 0: break if not response or response.get('KeyCount', 0) == 0: acks.append({'ack_content': None, 'unacked_delivery': unacked_delivery}) continue for item in response.get('Contents', []): key = item['Key'] if 'ACK_' in key: ack_name = key.split('/').pop() if '.xml' not in ack_name: continue self.client.download_file(config.S3_ACK_BUCKET, key, TMP_ACKS_DIR + ack_name) try: with open(TMP_ACKS_DIR + ack_name, 'r') as ack_file: content = ack_file.read() ack_found = True acks.append({'ack_content': content, 'unacked_delivery': unacked_delivery}) finally: os.remove(TMP_ACKS_DIR + ack_name) if not ack_found: acks.append({'ack_content': None, 'unacked_delivery': unacked_delivery}) return acks