(DEV307) Introduction to Version 3 of the AWS SDK for Python (Boto) | AWS re:Invent 2014
-
Upload
amazon-web-services -
Category
Technology
-
view
402 -
download
0
description
Transcript of (DEV307) Introduction to Version 3 of the AWS SDK for Python (Boto) | AWS re:Invent 2014
Boto
Project
Overview
Boto 3
Features
Project
Example
Project
Started
Community
Contributions
Amazon
Service
Updates
Code
Generation
Python 3
Support
import boto
conn = boto.connect_s3()
bucket = conn.get_bucket('my-bucket')
for obj in bucket:
print(obj.key, obj.last_modified)
Boto
Project
Overview
Boto 3
Features
Project
Example
Boto 3
Botocore
Session Resources ClientsConfig
Session
Credentials
ClientsAuthentication
SerializationHTTPS
import boto3
s3 = boto3.resource('s3')
bucket = s3.Bucket('my-bucket')
for obj in bucket.objects.all():
print(obj.key, obj.last_modified)
import boto3
sqs = boto3.client('sqs')
response = sqs.list_queues(QueueNamePrefix='test')
print(response['QueueUrls'][0])
"QueueUrls"
"https://queue.amazonaws.com/12345678/products"
"https://queue.amazonaws.com/12345678/news"
"https://queue.amazonaws.com/12345678/test"
"https://queue.amazonaws.com/12345678/jobs"
s3 = boto3.client('s3')
# Get a paginator and iterate through each page
paginator = s3.get_paginator('list_objects')
for page in paginator.paginate(Bucket='my-bucket'):
for obj in page['Contents']:
print(obj['Key'])
Starting Running Terminated
ec2 = boto3.client('ec2')
# Get a waiter and wait for instance to be readywaiter = ec2.get_waiter('instance_running')waiter.wait(InstanceIds=['i-abc123'])
print('Instance is ready!')
instance = Instance(id)Amazon EC2
bucket = Bucket(name)Amazon S3
queue = Queue(url)Amazon SQS
instance.idAmazon EC2
object.bucket_name, object.keyAmazon S3
queue.urlAmazon SQS
instance.instance_typeAmazon EC2
object.last_modifiedAmazon S3
queue.attributes[‘DelaySeconds’]Amazon SQS
instance.create_image(…)Amazon EC2
object.put(Body=‘hello’)Amazon S3
queue.receive_messages()Amazon SQS
instance.subnetAmazon EC2
-Amazon S3
-Amazon SQS
ec2.Instance(id)Amazon EC2
bucket.Object(key)Amazon S3
sqs.Queue(url)Amazon SQS
ec2.instancesAmazon EC2
bucket.objectsAmazon S3
sqs.queuesAmazon SQS
import boto3
s3 = boto3.resource('s3')
obj = s3.Object('my-bucket', 'my-key.jpg')
print(obj.content_type)
print(obj.last_modified)
for obj in s3.Bucket(name='boto3').objects.all():
print(obj.key)
for queue in sqs.queues.filter(QueueNamePrefix='a'):
print(queue.url)
# Batch actions coming soon!
bucket.objects.delete()
Boto
Project
Overview
Boto 3
Features
Project
Example
Amazon
SQS
Amazon
SQS
import boto3
# Creating a client by name
client = boto3.client('s3')
# Creating a resource by name
resource = boto3.resource('s3')
import boto3
# Create a new bucket
s3 = boto3.resource('s3')
bucket = s3.create_bucket(Name='Boto3')
# Get a bucket by name
bucket = s3.Bucket('Boto3')
import boto3
# Get a bucket by name
bucket = s3.Bucket('Boto3')
# Upload a new file
with open('file.mov', 'rb') as data:
bucket.Object('file.mov').put(Body=data)
import boto3
# List all objects in all buckets
s3 = boto3.resource('s3')
for bucket in s3.buckets.all():
for obj in bucket.objects.all():
print(bucket.name, obj.key)
import boto3
# Download a file
bucket = s3.Bucket('Boto3')
obj = bucket.Object('output.mp4')
data = obj.get()['Body'].read()
import boto3
# Create SNS topic (idempotent)
sns = boto3.resource('sns')
topic = sns.create_topic(Name='Boto3')
# Get an SNS topic
topic = sns.Topic('<TOPIC ARN>')
# 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')
# Subscribe an SQS queue to the topic
topic.subscribe(
Protocol='sqs',
Endpoint=queue.attributes['QueueArn'])
import boto3
# Create IAM role
iam = boto3.resource('iam')
role = iam.create_role(
RoleName='role-name',
AssumeRolePolicyDocument='...')
# Set role policy
policy = role.RolePolicy('transcoder')
policy.put(PolicyDocument={
'Version': '2012-10-17',
'Statement':[
{...}
]
})
# Create a new pipeline
transcoder = boto3.client('elastictranscoder')
response = transcoder.create_pipeline(
Name='Boto3',
InputBucket='Boto3-input',
OutputBucket='Boto3-output',
Role='<ROLE ARN>',
...
)
# Create a new transcoding job
job = transcoder.create_job(
PipelineId=response['Pipeline']['Id'],
Input={
'Key': 'input.mov',
...
},
Outputs={...}
)
https://github.com/boto/boto3-sample
Boto
Project
Overview
Boto 3
Features
Project
Example
https://github.com/boto/boto3
https://github.com/boto/boto3-sample
Please give us your feedback on this session.
Complete session evaluations and earn re:Invent swag.
http://bit.ly/awsevals