Amazon SQS Extended Client Library for Python

1.0.1 · active · verified Thu Apr 16

The Amazon SQS Extended Client Library for Python extends Amazon SQS to handle message payloads larger than the standard 256 KB limit, supporting sizes up to 2 GB. It achieves this by transparently storing the message payload in an Amazon S3 bucket and sending a reference to the S3 object in the SQS message. The library is currently at version 1.0.1 and maintains a release cadence tied to feature development and AWS SDK updates.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to initialize the `SQSExtendedClient` and use it to send and receive messages larger than the standard SQS limit. It requires an existing SQS queue and an S3 bucket configured in your AWS environment with appropriate permissions. The `always_through_s3` flag is set to `True` to ensure all messages, regardless of size, are handled via S3, which is often desired for consistency.

import boto3
from amazon_sqs_extended_client import SQSExtendedClient
import os

# Configure AWS credentials and region
# Ensure AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY, AWS_REGION are set as environment variables
aws_region = os.environ.get('AWS_REGION', 'us-east-1')
sqs_client = boto3.client('sqs', region_name=aws_region)
s3_client = boto3.client('s3', region_name=aws_region)

# SQS Queue URL and S3 Bucket Name must exist and be accessible
# Replace with your actual SQS Queue URL and S3 Bucket Name
SQS_QUEUE_URL = os.environ.get('SQS_QUEUE_URL', 'YOUR_SQS_QUEUE_URL')
S3_BUCKET_NAME = os.environ.get('S3_BUCKET_NAME', 'YOUR_S3_BUCKET_NAME')

# Initialize the SQS Extended Client
sqs_extended_client = SQSExtendedClient(
    sqs_client=sqs_client,
    s3_client=s3_client,
    bucket_name=S3_BUCKET_NAME,
    always_through_s3=True # Always store messages in S3, regardless of size
)

# Example: Send a large message
large_message_body = 'A' * 300000  # 300KB message, exceeding SQS limit
print(f"Sending a large message ({len(large_message_body) / 1024} KB)...")
send_response = sqs_extended_client.send_message(
    QueueUrl=SQS_QUEUE_URL,
    MessageBody=large_message_body
)
print(f"Message sent! Message ID: {send_response.get('MessageId')}")

# Example: Receive the message
print("\nReceiving messages...")
receive_response = sqs_extended_client.receive_message(
    QueueUrl=SQS_QUEUE_URL,
    MaxNumberOfMessages=1
)

messages = receive_response.get('Messages', [])
if messages:
    message = messages[0]
    received_body = message.get('Body')
    print(f"Received message body (first 100 chars): {received_body[:100]}...")
    print(f"Received message length: {len(received_body)} bytes")
    
    # Delete the message after processing
    sqs_extended_client.delete_message(
        QueueUrl=SQS_QUEUE_URL,
        ReceiptHandle=message['ReceiptHandle']
    )
    print("Message deleted.")
else:
    print("No messages received.")

view raw JSON →