In this post we’ll go over a few common commands for interacting with AWS S3 and SQS using Python Boto. These were pulled from the pages of my Jupiter notebook and have been valuable in jump stating new projects.
Lets start with an example for listing all S3 buckets:
import boto3
s3 = boto3.resource('s3')
# print out bucket names
for bucket in s3.buckets.all():
print (bucket.name)
In this example we’ll again list the buckets, but using the client API:
import boto3
s3 = boto3.client('s3')
response = s3.list_buckets()
# print the bucket names
for bucket in response['buckets']:
print(f' {bucket["Name"]}')
So much for bucket list examples, now lets upload a file to S3:
import boto3
s3 = boto.resource('s3')
data = open('Duff_Beer.png', 'rb')
s3.Bucket('your-aws-bucket').put_object(Key='Duffer.png', Body=data)
Using the examples above as a pattern you can easily extend to include the other CRUD operations.
We turn now to some basic SQS operations:
import boto3
boto3.setup_default_session(profile_name='sqs')
sqs = boto3.resource('sqs')
# list and print SQS names
for queue in sqs.queues.all():
print(queue.url)
Here’s how you would go about creating a Queue instance:
import boto3
boto3.setup_default_session(profile_name='sqs')
sqs = boto3.resource('sqs')
# create queue
queue = sqs.create_queue(QueueName='test', Attributes={'DelaySeconds': '5'})
# send a message
response = queue.send_messages(Entries=[
{
'Id': '1',
'MessageBody': 'Howdy from your pal Boto3'
},
{
'Id': '2',
'MessageBody': 'Boto3 would like to know if you have plans for this eve',
'MessageAttributes': {
'Author': {
'StringValue': 'A. Piker',
'DataType': 'String'
}
}
}
])
Reading the SQS message:
import boto3
from datetime import datetime
boto3.setup_default_session(profile_name='sqs')
sqs = boto3.resource('sqs')
queue = sqs.get_queue_by_name(QueueName='test')
queue.load()
messages = queue.receive_messages(
QueueUrl=queue.url,
AttributeName=[
'SentTimeStamp', 'Author', 'Id'
],
MaxNumberOfMessages=9,
MessageAttributeNames=[
'All'
],
VisibilityTimeout=300,
WaitTimeSeconds=0
)
# print messages
for message in messages:
print(f'SQS Message: \n\t {message.body'})
message.delete()
# some other handy debug and cleanup helpers
print(queue)
resp = clt.get_queue_attributes(QueueUrl=queue.url,
AttributeNames=['ApproximateNumberOfMessages', 'LastModifiedTimestamp'])
print(resp)
print(clt.purge_queue(QueueUrl=queue.url)
print(clt.delete_queue(QueueUrl=queue.url)
I hope these examples serve to spark your curiosity and help guide you to realize the ease and simplicity of AWS SDK integration using Boto.