import argparse import boto3 import json from pathlib import Path from botocore.exceptions import ClientError def get_queue_url(sqs_client, queue_name: str) -> str: """Get the URL of an SQS queue by its name.""" response = sqs_client.get_queue_url(QueueName=queue_name) return response['QueueUrl'] def load_message_file(filename: str) -> dict: """Load message data from a JSON file exported by sqs-download.py.""" file_path = Path(filename) if not file_path.exists(): raise FileNotFoundError(f'File not found: {filename}') with open(file_path, 'r', encoding='utf-8') as f: return json.load(f) def convert_message_attributes(attrs: dict) -> dict: """Convert exported SQS into the format expected by send_message.""" result = {} for key, value in attrs.items(): attr = {'DataType': value['DataType']} if 'StringValue' in value: attr['StringValue'] = value['StringValue'] if 'BinaryValue' in value: attr['BinaryValue'] = value['BinaryValue'] if 'StringListValues' in value: attr['StringListValues'] = value['StringListValues'] if 'BinaryListValues' in value: attr['BinaryListValues'] = value['BinaryListValues'] result[key] = attr return result def send_message_from_file(sqs_client, queue_url: str, message_data: dict) -> dict: """Send a message to SQS using the data loaded from a JSON file.""" body = message_data['Body'] message_attributes = convert_message_attributes( message_data.get('MessageAttributes', {}) ) print(f'sending message to "{queue_url}" with Body:') send_kwargs = { 'QueueUrl': queue_url, 'MessageBody': body, } if message_attributes: send_kwargs['MessageAttributes'] = message_attributes print(body) return sqs_client.send_message(**send_kwargs) def main(): """Command-line main.""" parser = argparse.ArgumentParser( description='Read a message from a JSON file and send it to an SQS queue.' ) parser.add_argument( '--filename', required=True, help='Path to exported message JSON file', ) parser.add_argument( '--sqs-name', default='dev-amazon-data-availability-queue', help='Destination SQS queue name', ) parser.add_argument( '--profile', default='default', help='AWS profile name (optional)', ) parser.add_argument( '--region', default='us-east-1', help='AWS region name (optional)', ) args = parser.parse_args() session_kwargs = {} if args.profile: session_kwargs['profile_name'] = args.profile if args.region: session_kwargs['region_name'] = args.region session = boto3.Session(**session_kwargs) sqs = session.client('sqs') try: queue_url = get_queue_url(sqs, args.sqs_name) print(f'Resolved queue URL: {queue_url}') message_data = load_message_file(args.filename) print(f'Message loaded from file: {args.filename}') response = send_message_from_file(sqs, queue_url, message_data) print('Message sent successfully') print(f'New MessageId: {response["MessageId"]}') print(f'MD5OfMessageBody: {response["MD5OfMessageBody"]}') except ClientError as e: print(f'AWS error: {e}') raise SystemExit(1) except Exception as e: print(f'Unexpected error: {e}') raise SystemExit(1) if __name__ == '__main__': main()