文档

使用自定义模板编排FC并运行脚本

更新时间:

本文介绍如何用OOS自定义模板编排函数计算, 通过执行Python脚本来下载文件并将其存储到OSS

功能背景

阿里云函数计算(Function Compute,FC)是一项核心服务。它是基于事件驱动的平台,允许用户在没有服务器的情况下运行代码。利用FC的高效编程能力,开发者只需部署代码,FC就能够响应事件触发立即执行。此外,FC与阿里云的对象存储服务(Object Storage Service,OSS)紧密集成,使得数据的存储和批量处理变得更简单,开发者无需管理服务器或存储,即可轻松处理数据,并且可以将处理结果安全地存储在OSS中。同时,OOS能够自动执行多种复杂的运维任务,让开发者通过自定义模板来集成和调用阿里云提供的各类资源和服务。在本文中,我们将介绍如何用OOS编排函数计算,通过执行Python脚本来下载文件并将其存储到OSS。

使用说明

  1. 使用此功能必须开通函数计算服务

  2. 创建执行前需要为FC创建RAM角色并授予访问OSS的权限

操作步骤

  1. 登录 OOS 控制台并使用以下示例模板创建自定义模板。您可以参考FC提供的Python开发指南自定义脚本和模板。

    FormatVersion: OOS-2019-06-01
    Description:
      en: FC runs script, To use this template, you must first <a href='https://help.aliyun.com/zh/functioncompute/getting-started/quickly-create-a-function#p-t79-y7o-68z' target="_blank">activate the function computing service< /a>
      zh-cn: FC运行脚本,使用此功能必须<a href='https://help.aliyun.com/zh/functioncompute/getting-started/quickly-create-a-function#p-t79-y7o-68z' target="_blank">开通函数计算服务</a>
      name-en: FC-RunScript
      name-zh-cn: FC运行脚本
    Parameters:
      FileUrl:
        Label:
          en: FileUrl
          zh-cn: 文件存储URL
        Type: String
      OSSRegionId:
        Label:
          en: OSSRegionId
          zh-cn: OSS bucket所在地域ID
        Type: String
        AssociationProperty: RegionId
      OSSBucketName:
        Label:
          en: OSSBucketName
          zh-cn: OSS Bucket 名称
        Type: String
        AssociationProperty: ALIYUN::OSS::Bucket::BucketName
        AssociationPropertyMetadata:
          RegionId: ${OSSRegionId}
        Default: ''
      OSSDirectory:
        Type: String
        Label:
          en: OSSDirectory
          zh-cn: OSS目录
        Description:
          en: The directory where files are stored in the OSS Bucket. / is used to split the path and quickly create subdirectories. However, do not start with / and do not appear consecutive / s.
          zh-cn: 文件存储在 OSS Bucket 中的目录,/ 用于分割路径,可快速创建子目录,但不要以 / 开头,不要出现连续的 / 。
        Default: Download/Demo/
      FCAssumeRole:
        Label:
          en: FCAssumeRole
          zh-cn: FC扮演的RAM角色
        Description:
          en: The Function Compute platform will use this RAM role to generate a temporary key for accessing your Alibaba Cloud resources and pass it to your code. For details, please see <a href="https://help.aliyun.com/zh/functioncompute/user-guide/grant-function-compute-permissions-to-access-other-alibaba-cloud-services" target="_blank ">Grant Function Compute permissions to access other cloud services</a>
          zh-cn: 函数计算平台会使用这个 RAM 角色(Role)来生成访问您的阿里云资源的临时密钥,并传递给您的代码。详情请查看<a href="https://help.aliyun.com/zh/functioncompute/user-guide/grant-function-compute-permissions-to-access-other-alibaba-cloud-services" target="_blank">授予函数计算访问其他云服务的权限</a>
        Type: String
        AssociationProperty: ALIYUN::RAM::Service::Role
        AssociationPropertyMetadata:
          Service: fc.aliyuncs.com
        Default: ''
      OOSAssumeRole:
        Label:
          en: OOSAssumeRole
          zh-cn: OOS扮演的RAM角色
        Type: String
        Default: ''
    RamRole: '{{ OOSAssumeRole }}'
    Tasks:
      - Name: ExecuteScript
        Action: ACS::FC::ExecuteScript
        Description:
          en: Run the python script
          zh-cn: 运行Python脚本
        Properties:
          runtime: 'python3.10'
          role: '{{ FCAssumeRole }}'
          script: |-
            import oss2
            import requests
            
            def handler(event, context):
              # 获取FC角色credential
              auth = oss2.StsAuth(context.credentials.access_key_id, context.credentials.access_key_secret, context.credentials.security_token)
              endpoint = 'https://oss-{{OSSRegionId}}.aliyuncs.com'
              bucket = oss2.Bucket(auth, endpoint, '{{OSSBucketName}}')
              file_url = '{{FileUrl}}'
              # 下载文件
              file_content = requests.get(file_url)
              file_name = file_url.split('/')[-1]
              # 将文件上传到指定OSS
              bucket.put_object(f'{{OSSDirectory}}{file_name}', content)

    示例脚本说明:

    1. 运行环境默认 python3.10

    2. 函数名称默认 index.handler

    3. 使用模块oss2和requests,详情请查看环境说明

  2. 模板创建完成后,配置参数并创建执行。执行成功后,在目标OSS Bucket中可以看到已下载的文件。image

附录

进阶操作请查看定时备份redis实例并转储到OSS