Skip to content

Commit

Permalink
fix examples (#792)
Browse files Browse the repository at this point in the history
  • Loading branch information
thehesiod committed Apr 7, 2020
1 parent 875f939 commit 017cb59
Show file tree
Hide file tree
Showing 7 changed files with 158 additions and 167 deletions.
6 changes: 3 additions & 3 deletions docs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,12 @@ Basic Example
bucket = 'dataintake'
filename = 'dummy.bin'
folder = 'aiobotocore'
key = '{}/{}'.format(folder, filename)
key = f'{folder}/{filename}'
session = aiobotocore.get_session()
async with session.create_client('s3', region_name='us-west-2',
aws_secret_access_key=AWS_SECRET_ACCESS_KEY,
aws_access_key_id=AWS_ACCESS_KEY_ID) as client:
aws_secret_access_key=AWS_SECRET_ACCESS_KEY,
aws_access_key_id=AWS_ACCESS_KEY_ID) as client:
# upload object to amazon s3
data = b'\x01'*1024
resp = await client.put_object(Bucket=bucket,
Expand Down
102 changes: 50 additions & 52 deletions examples/dynamodb_batch_write.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,59 +44,57 @@ def create_batch_write_structure(table_name, start_num, num_items):

async def go():
session = aiobotocore.get_session()
client = session.create_client('dynamodb', region_name='us-west-2')
table_name = 'test'

print('Writing to dynamo')
start = 0
while True:
# Loop adding 25 items to dynamo at a time
request_items = create_batch_write_structure(table_name, start, 25)
response = await client.batch_write_item(
RequestItems=request_items
)
if len(response['UnprocessedItems']) == 0:
print('Writted 25 items to dynamo')
else:
# Hit the provisioned write limit
print('Hit write limit, backing off then retrying')
await asyncio.sleep(5)

# Items left over that haven't been inserted
unprocessed_items = response['UnprocessedItems']
print('Resubmitting items')
# Loop until unprocessed items are written
while len(unprocessed_items) > 0:
response = await client.batch_write_item(
RequestItems=unprocessed_items
)
# If any items are still left over, add them to the
# list to be written
async with session.create_client('dynamodb', region_name='us-west-2') as client:
table_name = 'test'

print('Writing to dynamo')
start = 0
while True:
# Loop adding 25 items to dynamo at a time
request_items = create_batch_write_structure(table_name, start, 25)
response = await client.batch_write_item(
RequestItems=request_items
)
if len(response['UnprocessedItems']) == 0:
print('Wrote 25 items to dynamo')
else:
# Hit the provisioned write limit
print('Hit write limit, backing off then retrying')
await asyncio.sleep(5)

# Items left over that haven't been inserted
unprocessed_items = response['UnprocessedItems']

# If there are items left over, we could do with
# sleeping some more
if len(unprocessed_items) > 0:
print('Backing off for 5 seconds')
await asyncio.sleep(5)

# Inserted all the unprocessed items, exit loop
print('Unprocessed items successfully inserted')
break

start += 25

# See if DynamoDB has the last item we inserted
final_item = 'item' + str(start + 24)
print('Item "{0}" should exist'.format(final_item))

response = await client.get_item(
TableName=table_name,
Key={'pk': {'S': final_item}}
)
print('Response: ' + str(response['Item']))

await client.close()
print('Resubmitting items')
# Loop until unprocessed items are written
while len(unprocessed_items) > 0:
response = await client.batch_write_item(
RequestItems=unprocessed_items
)
# If any items are still left over, add them to the
# list to be written
unprocessed_items = response['UnprocessedItems']

# If there are items left over, we could do with
# sleeping some more
if len(unprocessed_items) > 0:
print('Backing off for 5 seconds')
await asyncio.sleep(5)

# Inserted all the unprocessed items, exit loop
print('Unprocessed items successfully inserted')
break

start += 25

# See if DynamoDB has the last item we inserted
final_item = 'item' + str(start + 24)
print(f'Item "{final_item}" should exist')

response = await client.get_item(
TableName=table_name,
Key={'pk': {'S': final_item}}
)
print(f'Response: {response["Item"]}')


def main():
Expand Down
60 changes: 29 additions & 31 deletions examples/dynamodb_create_table.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,37 +7,35 @@

async def go():
session = aiobotocore.get_session()
client = session.create_client('dynamodb', region_name='us-west-2')
# Create random table name
table_name = 'aiobotocore-' + str(uuid.uuid4())

print('Requesting table creation...')
await client.create_table(
TableName=table_name,
AttributeDefinitions=[
{
'AttributeName': 'testKey',
'AttributeType': 'S'
},
],
KeySchema=[
{
'AttributeName': 'testKey',
'KeyType': 'HASH'
},
],
ProvisionedThroughput={
'ReadCapacityUnits': 10,
'WriteCapacityUnits': 10
}
)

print("Waiting for table to be created...")
waiter = client.get_waiter('table_exists')
await waiter.wait(TableName=table_name)
print("Table {0} created".format(table_name))

await client.close()
async with session.create_client('dynamodb', region_name='us-west-2') as client:
# Create random table name
table_name = f'aiobotocore-{uuid.uuid4()}'

print('Requesting table creation...')
await client.create_table(
TableName=table_name,
AttributeDefinitions=[
{
'AttributeName': 'testKey',
'AttributeType': 'S'
},
],
KeySchema=[
{
'AttributeName': 'testKey',
'KeyType': 'HASH'
},
],
ProvisionedThroughput={
'ReadCapacityUnits': 10,
'WriteCapacityUnits': 10
}
)

print("Waiting for table to be created...")
waiter = client.get_waiter('table_exists')
await waiter.wait(TableName=table_name)
print(f"Table {table_name} created")


def main():
Expand Down
2 changes: 1 addition & 1 deletion examples/simple.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ async def go():
bucket = 'dataintake'
filename = 'dummy.bin'
folder = 'aiobotocore'
key = '{}/{}'.format(folder, filename)
key = f'{folder}/{filename}'

session = aiobotocore.get_session()
async with session.create_client(
Expand Down
68 changes: 33 additions & 35 deletions examples/sqs_queue_consumer.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,46 +14,44 @@
async def go():
# Boto should get credentials from ~/.aws/credentials or the environment
session = aiobotocore.get_session()
client = session.create_client('sqs', region_name='us-west-2')
try:
response = await client.get_queue_url(QueueName=QUEUE_NAME)
except botocore.exceptions.ClientError as err:
if err.response['Error']['Code'] == \
'AWS.SimpleQueueService.NonExistentQueue':
print("Queue {0} does not exist".format(QUEUE_NAME))
await client.close()
sys.exit(1)
else:
raise
async with session.create_client('sqs', region_name='us-west-2') as client:
try:
response = await client.get_queue_url(QueueName=QUEUE_NAME)
except botocore.exceptions.ClientError as err:
if err.response['Error']['Code'] == \
'AWS.SimpleQueueService.NonExistentQueue':
print("Queue {0} does not exist".format(QUEUE_NAME))
sys.exit(1)
else:
raise

queue_url = response['QueueUrl']
queue_url = response['QueueUrl']

print('Pulling messages off the queue')
print('Pulling messages off the queue')

while True:
try:
# This loop wont spin really fast as there is
# essentially a sleep in the receieve_message call
response = await client.receive_message(
QueueUrl=queue_url,
WaitTimeSeconds=2,
)
while True:
try:
# This loop wont spin really fast as there is
# essentially a sleep in the receive_message call
response = await client.receive_message(
QueueUrl=queue_url,
WaitTimeSeconds=2,
)

if 'Messages' in response:
for msg in response['Messages']:
print('Got msg "{0}"'.format(msg['Body']))
# Need to remove msg from queue or else it'll reappear
await client.delete_message(
QueueUrl=queue_url,
ReceiptHandle=msg['ReceiptHandle']
)
else:
print('No messages in queue')
except KeyboardInterrupt:
break
if 'Messages' in response:
for msg in response['Messages']:
print(f'Got msg "{msg["Body"]}"')
# Need to remove msg from queue or else it'll reappear
await client.delete_message(
QueueUrl=queue_url,
ReceiptHandle=msg['ReceiptHandle']
)
else:
print('No messages in queue')
except KeyboardInterrupt:
break

print('Finished')
await client.close()
print('Finished')


def main():
Expand Down
23 changes: 11 additions & 12 deletions examples/sqs_queue_create.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,22 @@

async def go():
session = aiobotocore.get_session()
client = session.create_client('sqs', region_name='us-west-2')
async with session.create_client('sqs', region_name='us-west-2') as client:

print('Creating test_queue1')
response = await client.create_queue(QueueName='test_queue1')
queue_url = response['QueueUrl']
print('Creating test_queue1')
response = await client.create_queue(QueueName='test_queue1')
queue_url = response['QueueUrl']

response = await client.list_queues()
response = await client.list_queues()

print('Queue URLs:')
for queue_name in response.get('QueueUrls', []):
print(' ' + queue_name)
print('Queue URLs:')
for queue_name in response.get('QueueUrls', []):
print(f' {queue_name}')

print('Deleting queue {0}'.format(queue_url))
await client.delete_queue(QueueUrl=queue_url)
print(f'Deleting queue {queue_url}')
await client.delete_queue(QueueUrl=queue_url)

print('Done')
await client.close()
print('Done')


def main():
Expand Down
64 changes: 31 additions & 33 deletions examples/sqs_queue_producer.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,40 +15,38 @@
async def go():
# Boto should get credentials from ~/.aws/credentials or the environment
session = aiobotocore.get_session()
client = session.create_client('sqs', region_name='us-west-2')
try:
response = await client.get_queue_url(QueueName=QUEUE_NAME)
except botocore.exceptions.ClientError as err:
if err.response['Error']['Code'] == \
'AWS.SimpleQueueService.NonExistentQueue':
print("Queue {0} does not exist".format(QUEUE_NAME))
await client.close()
sys.exit(1)
else:
raise

queue_url = response['QueueUrl']

print('Putting messages on the queue')

msg_no = 1
while True:
async with session.create_client('sqs', region_name='us-west-2') as client:
try:
msg_body = 'Message #{0}'.format(msg_no)
await client.send_message(
QueueUrl=queue_url,
MessageBody=msg_body
)
msg_no += 1

print('Pushed "{0}" to queue'.format(msg_body))

await asyncio.sleep(random.randint(1, 4))
except KeyboardInterrupt:
break

print('Finished')
await client.close()
response = await client.get_queue_url(QueueName=QUEUE_NAME)
except botocore.exceptions.ClientError as err:
if err.response['Error']['Code'] == \
'AWS.SimpleQueueService.NonExistentQueue':
print(f"Queue {QUEUE_NAME} does not exist")
sys.exit(1)
else:
raise

queue_url = response['QueueUrl']

print('Putting messages on the queue')

msg_no = 1
while True:
try:
msg_body = f'Message #{msg_no}'
await client.send_message(
QueueUrl=queue_url,
MessageBody=msg_body
)
msg_no += 1

print(f'Pushed "{msg_body}" to queue')

await asyncio.sleep(random.randint(1, 4))
except KeyboardInterrupt:
break

print('Finished')


def main():
Expand Down

0 comments on commit 017cb59

Please sign in to comment.