AirJD 焦点
AirJD

没有录音文件
00:00/00:00
加收藏

基于AWS的Python程序开发 by 费良宏

发布者 pyconf   简介 PyChina 大会
发布于 1457572303641  浏览 7692 关键词 Python 
分享到

第1页

基于AWS的Python程序开发

费良宏, AWS Evangelist

©2015,
 Amazon
 Web
 Services,
 Inc.
 or
 its
 affiliates.
 All
 rights
 reserved



第2页

Python 是什么?

§ 一种通用的、高级程序设计语言 § 一个荷兰人(Guido van Rossum)的设计/1991年 § 一个榜单上(TIBOE)上排名第5的语言/2015年9月 § 一大批公司、开发者在使用它

Google, Youtube, Instagram, Pinterest, Bing, Reddit, Etsy,  Dropbox, Quora, Yelp, Friendfeed, Trulia, Hunch, Blogger  and Rdio... 

§ 一些知名的软件由它来开发

Autodesk Maya, GIMP, BitTorrent, Blender 3D, Cinema 4D,  Dropbox, OpenStack, YUM, OpenERP, Civilization IV,  Matplotlib, FreeCAD, MySQL Workbench, Nuke... 



第3页

为什么是Python ?

在一些人眼中它像一把”瑞士军刀”,无所不能

“Python allows us to produce maintainable features in record times, with  a minimum of developers.”

Cuong Do, Software Architect, Youtube

一些人觉得它充满争议,并不完美

Python 2 vs. Python3, Significant white-­space, Performance, Global  Interpreter Lock (GIL)  …



第4页

Python 之禅

美胜于丑 规则胜于特例 显胜于隐 实用胜于单纯 简胜于繁 告错胜于沉默 繁胜于杂 沉默胜于吵闹 平胜于迭 拒绝胜于猜测 疏胜于密 唯一胜于显然 最好只有一种 不要自认作者 现在胜于永不 永不胜于匆猝 值得说则必说 命名空间很赞

- Tim Peters (PEP20 / import this)



第5页

爱上Python 的十大理由

让我保持专注 我猜的总是对的 键入不多 喜欢的人毋需多说 不假设如何发现错误 不期待完美语言的出现 不把我当作傻瓜 效率之上再谈性能 不会有向后兼容的痛苦 减少混乱

- Bruce Eckle, "Why I Love Python" 



第6页

Boto – 适用于Python的AWS 开发工具包

Boto 版本 –

Boto3.__version__   ‘1.1.3’

兼容性 -

Py2.6 、Py2.7 ,Py3.3+

功能 -

支持 40+ AWS 服务(S3, EC2, EMR,  Machine Learning…)



第7页

Boto3 概述

• 设计之初已支持 Python2 & Python 3  • 从底层支持的数据驱动 • 针对AWS 新的服务的支持 • 一致性的使用界面 现代风格的面向对象的 API  • 可在现有的 Boto2 的程序中使用 • 与AWS CLI 共享 Botocore • 基于Apache许可的开源项目



第8页

Boto3 的变化

• 资源(Resource) -­ 高级的面向对象的接口 • 集合(Collection) -­ 迭代操作资源组的工具 • 客户端(Clients) -­ 低层次服务连接 • 分页器(Paginators) -­ 响应自动分页 • 阻塞(Waiters) -­ 一种阻塞的方法,直到特定的状态满足 • Botocore  -­ 提供低级别的客户端、会话、凭据以及配

置数据



第9页

Boto3  的架构



Boto 3 Session



Config



Resources



Clients



Botocore Session

Credentials



Clients HTTPS



Authentication Serialization



第10页

Boto3 的安装与配置

安装 - $ pip install boto3

配置文件(~/.aws/credentials) -

[default]  aws_access_key_id = YOUR_ACCESS_KEY  aws_secret_access_key = YOUR_SECRET_KEY region=us-­east-­1



第11页

Boto3 的样例代码

import boto3

s3 = boto3.resource('s3') bucket = s3.Bucket('my-­‐bucket') for obj in bucket.objects.all():

print(obj.key, obj.last_modified)



第12页

Boto3 应用演示



第13页

演示概述

• 低级别的客户端(Client)调用 • 客户端对象列表分页(Pagination) • 访问资源(Resource) • 创建一个新资源(Amazon S3对象) • 集合(Collection)遍历 • 资源信息输出



第14页

通过AWS Transcoder 实现视频转码



第15页

通过AWS Transcoder 实现视频转码



第16页

通过AWS Transcoder 实现视频转码



第17页

通过AWS Transcoder 实现视频转码



第18页

通过AWS Transcoder 实现视频转码



第19页

通过AWS Transcoder 实现视频转码



第20页

通过AWS Transcoder 实现视频转码



第21页

通过AWS Transcoder 实现视频转码



第22页

通过AWS Transcoder 实现视频转码

Amazon  SQS



第23页

通过AWS Transcoder 实现视频转码

Amazon  SQS



第24页

通过AWS Transcoder 实现视频转码



第25页

演示



第26页

转码程序的设计

• Elastic Transcoder 需要的配置 • 用于输入的S3 bucket • 用于输出的S3 bucket • 用于通知的Amazon SNS topic • 用于资源存取的IAM角色 • 转码管道(pipeline) • 转码任务(job)



第27页

转码程序的基础:会话和连接

• 建立缺省的会话(session)

– 从文件或者环境变量中加载 Config 和 credentials

• 创建客户端 (clients) • 创建资源 (resources)



第28页

转码程序的基础:会话和连接

import boto3

# Creating a client by name client = boto3.client('s3')

# Creating a resource by name resource = boto3.resource('s3')



第29页

Amazon S3 上传

• 建立新的存储桶(bucket) • 获取存储桶(bucket) 的名字 • 上传文件



第30页

Amazon 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')



第31页

Amazon S3 上传

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)



第32页

Amazon S3 下载

• 找到存储桶(buckets) • 找到存储对象(objects) • 下载对象(object) 内容



第33页

Amazon S3 下载

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)



第34页

Amazon S3 下载

import boto3

# Download a file bucket = s3.Bucket('Boto3') obj = bucket.Object('output.mp4') data = obj.get()['Body'].read()



第35页

Amazon SNS Topic & Amazon SQS Queue

• 建立新的 Amazon SNS topic • 获取已存在的 Amazon SNS topic • 建立新的 Amazon SQS queue • 获取已存在的Amazon SQS queue • 发送通知到 Amazon SQS queue • 检查新的消息



第36页

Amazon SNS Topic & Amazon SQS Queue

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>')



第37页

Amazon SNS Topic & Amazon SQS Queue

# 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')



第38页

Amazon SNS Topic & Amazon SQS Queue

# Subscribe  an SQS queue to the topic topic.subscribe(

Protocol='sqs', Endpoint=queue.attributes['QueueArn'])



第39页

IAM 角色

• 建立新的 IAM 角色(role) • 设置许可(permissions)

– Amazon S3 – Amazon SQS – Amazon Elastic Transcoder



第40页

IAM 角色

import boto3

# Create IAM role iam = boto3.resource('iam') role = iam.create_role(

RoleName='role-­‐name', AssumeRolePolicyDocument='...')



第41页

IAM 角色

# Set role policy policy = role.RolePolicy('transcoder') policy.put(PolicyDocument={

'Version': '2012-­‐10-­‐17', 'Statement':[

{...} ] })



第42页

转码的 Pipeline & Jobs

• 建立新的 pipeline • 可选项

– Input / output S3 bucket – IAM role – Amazon SNS topic config

• 建立新的 job



第43页

转码的 Pipeline & Jobs

# Create a new pipeline transcoder = boto3.client('elastictranscoder') response = transcoder.create_pipeline(

Name='Boto3', InputBucket='Boto3-­‐input', OutputBucket='Boto3-­‐output', Role='<ROLE ARN>', ... )



第44页

转码的 Pipeline & Jobs

# Create a new transcoding job job = transcoder.create_job(

PipelineId=response['Pipeline']['Id'], Input={

'Key': 'input.mov', ... }, Outputs={...} )



第45页

增加一点胶水代码

• 目录监控 • 主要功能

– Upload / start job / wait / download / delete

• Amazon S3 清理 • Amazon SQS 清理



第46页

资源

• 演示源代码 - https://github.com/boto/boto3-­ sample

• AWS SDK for Python -­ https://aws.amazon.com/cn/sdk-­for-­python/

• Boto3 文档 - http://boto.readthedocs.org/en/latest/ • Boto3 @ github -­ https://github.com/boto/boto



第47页

谢谢!

©2015,
 Amazon
 Web
 Services,
 Inc.
 or
 its
 affiliates.
 All
 rights
 reserved



支持文件格式:*.pdf
上传最后阶段需要进行在线转换,可能需要1~2分钟,请耐心等待。