阿里云函数计算使用体验

image

环境安装

  • 在此案例中,使用阿里云服务器。(Linux)html

  • 使用官方安装脚本自动安装python

curl -fsSL https://get.docker.com | bash -s docker --mirror Aliyun

复制代码
  • 安装fun
npm install @alicloud/fun -g

复制代码
  • 配置fun,按提示输入信息
fun config

复制代码

基础:建立Helloworld文件

1\. 建立一个文件夹,如image_crawler
2\. 建立一个文件template.yml,内容
ROSTemplateFormatVersion: '2015-09-01'
Transform: 'Aliyun::Serverless-2018-04-03'
Resources:
  localdemo:
    Type: 'Aliyun::Serverless::Service'
    Properties:
      Description: 'local invoke demo'
    image-crawler:
      Type: 'Aliyun::Serverless::Function'
      Properties:
        Handler: index.handler
        CodeUri: code/
        Description: 'Hello world with python2.7!'
        Runtime: python2.7
3\. 建立一个code文件夹,在文件夹中编写helloworld代码
def handler(event, context):
    return 'hello world!'
4\. 运行函数
fun local invoke image-crawler

复制代码

image

进阶:设置oss转存任务,如一个image_crawler

  • 获取网页源代码,按上述语句运行image-crawler,可看到变化。
import logging
import json
import urllib

logger = logging.getLogger()

def handler(event, context):
    logger.info("event: " + event)
    evt = json.loads(event)
    url = evt['url']

    html = get_html(url)

    logger.info("html content length: " + str(len(html)))
    return 'Done!'

def get_html(url):
    page = urllib.urlopen(url)
    html = page.read()
    return html

复制代码
  • 运行如下语句可看到变化
echo '{"url":"http://huaban.com/search/?q=%e5%a3%81%e7%ba%b8"}' | fun local invoke image-crawler

复制代码
  • 添加函数,从源代码中抽取图片路径
def get_img(html):
    reg = r'https:\/\/[^\s,"]*\.jpg'
    imgre = re.compile(reg)
    return re.findall(imgre, html)

复制代码
  • 调整替换原先的handler函数,使之从“获取网页源代码”功能转变为“获取图片路径功能”。
def handler(event, context):
    logger.info("event: " + event)
    evt = json.loads(event)
    url = evt['url']

    html = get_html(url)

    img_list = get_img(html)
    logger.info(img_list)

    return 'Done!'

复制代码
  • 将图片上传到OSS中,在 template 中配置环境变量(需提早建立好 oss bucket):
EnvironmentVariables:
    OSSEndpoint: oss-cn-hangzhou.aliyuncs.com
    BucketName: fun-local-test

复制代码
  • 而后就能够直接在函数中获取到这两个环境变量了:
endpoint = os.environ['OSSEndpoint']
bucket_name = os.environ['BucketName']

复制代码
  • 设置OSS的STS权限控制,参考以下代码
creds = context.credentials

if (local):
    auth = oss2.Auth(creds.access_key_id,
                     creds.access_key_secret)
else:
    auth = oss2.StsAuth(creds.access_key_id,
                        creds.access_key_secret,
                        creds.security_token)

bucket = oss2.Bucket(auth, endpoint, bucket_name)

复制代码
  • 遍历全部图片文件,而且转存到OSS中
count = 0
for item in img_list:
    count += 1
    logging.info(item)
    # Get each picture
    pic = urllib.urlopen(item)
    # Store all the pictures in oss bucket, keyed by timestamp in microsecond unit
    bucket.put_object(str(datetime.datetime.now().microsecond) + '.png', pic)  

复制代码
  • 完整的代码
import logging,datetime
import json,re,oss2
import requests,urllib,os

logger = logging.getLogger()
endpoint = os.environ['OSSEndpoint']
bucket_name = os.environ['BucketName']
local = 1

def handler(event, context):
    logger.info("event: " + event)
    evt = json.loads(event)
    url = evt['url']
    print(url)
    creds = context.credentials

    if (local):
        auth = oss2.Auth(creds.access_key_id,creds.access_key_secret)
    else:
        auth = oss2.StsAuth(creds.access_key_id,creds.access_key_secret,creds.security_token)

    bucket = oss2.Bucket(auth, endpoint, bucket_name)

    html = get_html(url)
    img_list = get_img(html)
    logger.info(img_list)
    iii(img_list,bucket)
    return 'Done!'

def iii(img_list,bucket):
    count = 0
    for item in img_list:
        count += 1
        logging.info(item)
        pic = urllib.urlopen(item)
        if pic != None:
            bucket.put_object(str(datetime.datetime.now().microsecond) + '.jpg', pic)

def get_img(html):
    reg = r'http:\/\/[^,"]*\.jpg'
    imgre =re.compile(reg)
    return re.findall(imgre,html)

def get_html(url):
    page = requests.get(url)
    html = page.content
    return html

复制代码
  • 此时运行以下命令可看到成功效果。git

    echo '{"url":"http://huaban.com/search/?q=%e5%a3%81%e7%ba%b8"}' | fun local invoke image-crawler
    
    复制代码

部署

  • 添加OSS权限到yaml中,这次是添加了policies:AliyunOSSFullAccess。注意localdemo这个函数计算项目及image-crawler这个函数,fun-local-test这个OSS项目bucket须要事先在阿里云官网后台配置过。
ROSTemplateFormatVersion: '2015-09-01'
Transform: 'Aliyun::Serverless-2018-04-03'
Resources:
  localdemo:
    Type: 'Aliyun::Serverless::Service'
    Properties:
      Description: 'local invoke demo'
      Policies: AliyunOSSFullAccess
    image-crawler:
      Type: 'Aliyun::Serverless::Function'
      Properties:
        Handler: index.handler
        CodeUri: code/
        Description: 'Hello world with python2.7!'
        Runtime: python2.7
        EnvironmentVariables:
          OSSEndpoint: oss-cn-hangzhou.aliyuncs.com
          BucketName: fun-local-test

复制代码
  • 使用fun deploy 即看到运行成功。

image

  • 登陆台阿里云函数计算后台,能够看到这个项目代码,能够线上修改配置。

思考:函数计算适合作什么用途?

  • 定时任务,如爬虫类。(图片转存、文字转存等)github

  • pipeline类别,如接受A信息,转存到B信息及格式。根据if,else等来分类的任务。web

  • 微信,钉钉等公众号的后端(Flask),免去搭建服务器。docker

  • 能够一个函数就解决的任务与项目,特别简单的项目。npm

  • 阿里云官方函数计算后台提供Flask-web类型的函数计算模块,能够快速搭建外网api可访问的接口与页面。json

参考资料

相关文章
相关标签/搜索