In this session, we introduce Boto 3, the next major version of the AWS SDK for Python. You will learn about the new features in the SDK, such as the high-level resource APIs that simplify working with AWS collections and objects, and the eventing model that enables customizing your calls to AWS services. We use a sample application to demonstrate these features, and show how to integrate them with your existing projects.
14. s3 =boto3.client('s3')
# Get a paginatorand iterate through each page
paginator=s3.get_paginator('list_objects')
forpage inpaginator.paginate(Bucket='my-bucket'):
forobjinpage['Contents']:
print(obj['Key'])
16. ec2 =boto3.client('ec2')
# Get a waiter and wait for instance to be ready
waiter =ec2.get_waiter('instance_running')
waiter.wait(InstanceIds=['i-abc123'])
print('Instance is ready!')
44. importboto3
# Creating a client by name
client =boto3.client('s3')
# Creating a resource by name
resource =boto3.resource('s3')
45.
46. importboto3
# Create a new bucket
s3 =boto3.resource('s3')
bucket =s3.create_bucket(Name='Boto3')
# Get a bucket by name
bucket =s3.Bucket('Boto3')
47. importboto3
# Get a bucket by name
bucket =s3.Bucket('Boto3')
# Upload a new file
withopen('file.mov', 'rb') asdata:
bucket.Object('file.mov').put(Body=data)
48.
49. importboto3
# List all objects in all buckets
s3 =boto3.resource('s3')
forbucket ins3.buckets.all():
forobjinbucket.objects.all():
print(bucket.name, obj.key)
50. importboto3
# Download a file
bucket =s3.Bucket('Boto3')
obj=bucket.Object('output.mp4')
data =obj.get()['Body'].read()
51.
52. importboto3
# Create SNS topic (idempotent)
sns=boto3.resource('sns')
topic =sns.create_topic(Name='Boto3')
# Get an SNS topic
topic =sns.Topic('<TOPIC ARN>')
53. # Create an SQS queue (idempotent)
sqs=boto3.resource('sqs')
queue =sqs.create_queue(QueueName='Boto3')
# Get an existing queue
queue =sqs.get_queue_by_name(QueueName='Boto3')
54. # Subscribe an SQS queue to the topic
topic.subscribe(
Protocol='sqs',
Endpoint=queue.attributes['QueueArn'])
55.
56. importboto3
# Create IAM role
iam=boto3.resource('iam')
role =iam.create_role(
RoleName='role-name',
AssumeRolePolicyDocument='...')
57. # Set role policy
policy =role.RolePolicy('transcoder')
policy.put(PolicyDocument={
'Version': '2012-10-17',
'Statement':[
{...}
]
})
58.
59. # Create a new pipeline
transcoder =boto3.client('elastictranscoder')
response =transcoder.create_pipeline(
Name='Boto3',
InputBucket='Boto3-input',
OutputBucket='Boto3-output',
Role='<ROLE ARN>',
...
)
60. # Create a new transcoding job
job =transcoder.create_job(
PipelineId=response['Pipeline']['Id'],
Input={
'Key': 'input.mov',
...
},
Outputs={...}
)