背景介绍

系统运维管理OOS

系统运维管理OOS(CloudOps Orchestration Service)提供了一个高度灵活和强大的解决方案,通过精巧地编排阿里云提供的OpenAPI,使得用户能够将分散的单个原子运维任务链接起来,形成复杂的运维场景和流程。这种方式不仅大幅提升了运维的效率,也极大地减少了人为错误的可能性。更进一步,OOS的编排能力不仅限于基础的云服务管理操作,它还扩展到了阿里云的其他核心服务如函数计算FC和对象存储OSS。

文件转存场景

对于http文件转存到对象存储的场景,经典的做法通常涉及一个繁琐的双步骤过程:首先,用户需要手动下载目标文件至本地存储;随后,通过使用命令行工具或编写特定脚本,再将文件上传到云端的对象存储服务。这个流程不仅效率较低,还需要用户依赖于本地硬件资源或者支付额外费用租用阿里云上的ECS实例。

然而,借助于阿里云OOS这一过程得到了极大简化和优化。用户无需依赖任何本地硬件或者额外的云服务实例,仅需在阿里云的函数计算服务上执行一段定制的Python脚本。利用了云计算的弹性和函数计算的无服务器(Serverless)特性,实现了从HTTP源直接将文件高效转存到对象存储的目的。这样不仅消除了对物理硬件或计算实例的需求,而且极大降低了操作成本,提升了数据处理的效率。此外,这一过程的自动化也意味着可以极大减少因手动操作引入的错误。

前提条件

实践步骤

  1. 登录 OOS 控制台并使用附录中示例模板创建自定义模板。您可以参考FC提供的Python开发指南自定义脚本和模板。
  2. 模板创建完成后,配置参数并创建执行。执行成功后,在目标OSS Bucket中可以看到已下载的文件。

附录

示例模板

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,详情请查看Python内置模块