翼度科技»论坛 编程开发 python 查看内容

Serverless 使用阿里云OOS将http文件转存到对象存储

6

主题

6

帖子

18

积分

新手上路

Rank: 1

积分
18
背景介绍

系统运维管理OOS

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

对于http文件转存到对象存储的场景,经典的做法通常涉及一个繁琐的双步骤过程:首先,用户需要手动下载目标文件至本地存储;随后,通过使用命令行工具或编写特定脚本,再将文件上传到云端的对象存储服务。这个流程不仅效率较低,还需要用户依赖于本地硬件资源或者支付额外费用租用阿里云上的ECS实例。
然而,借助于阿里云OOS这一过程得到了极大简化和优化。用户无需依赖任何本地硬件或者额外的云服务实例,仅需在阿里云的函数计算服务上执行一段定制的Python脚本。利用了云计算的弹性和函数计算的无服务器(Serverless)特性,实现了从HTTP源直接将文件高效转存到对象存储的目的。这样不仅消除了对物理硬件或计算实例的需求,而且极大降低了操作成本,提升了数据处理的效率。此外,这一过程的自动化也意味着可以极大减少因手动操作引入的错误。
前提条件

实践步骤


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

附录

示例模板
  1. FormatVersion: OOS-2019-06-01
  2. Description:
  3.   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>
  4.   zh-cn: FC运行脚本,使用此功能必须<a href='https://help.aliyun.com/zh/functioncompute/getting-started/quickly-create-a-function#p-t79-y7o-68z' target="_blank">开通函数计算服务</a>
  5.   name-en: FC-RunScript
  6.   name-zh-cn: FC运行脚本
  7. Parameters:
  8.   FileUrl:
  9.     Label:
  10.       en: FileUrl
  11.       zh-cn: 文件存储URL
  12.     Type: String
  13.   OSSRegionId:
  14.     Label:
  15.       en: OSSRegionId
  16.       zh-cn: OSS bucket所在地域ID
  17.     Type: String
  18.     AssociationProperty: RegionId
  19.   OSSBucketName:
  20.     Label:
  21.       en: OSSBucketName
  22.       zh-cn: OSS Bucket 名称
  23.     Type: String
  24.     AssociationProperty: ALIYUN::OSS::Bucket::BucketName
  25.     AssociationPropertyMetadata:
  26.       RegionId: ${OSSRegionId}
  27.     Default: ''
  28.   OSSDirectory:
  29.     Type: String
  30.     Label:
  31.       en: OSSDirectory
  32.       zh-cn: OSS目录
  33.     Description:
  34.       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.
  35.       zh-cn: 文件存储在 OSS Bucket 中的目录,/ 用于分割路径,可快速创建子目录,但不要以 / 开头,不要出现连续的 / 。
  36.     Default: Download/Demo/
  37.   FCAssumeRole:
  38.     Label:
  39.       en: FCAssumeRole
  40.       zh-cn: FC扮演的RAM角色
  41.     Description:
  42.       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>
  43.       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>
  44.     Type: String
  45.     AssociationProperty: ALIYUN::RAM::Service::Role
  46.     AssociationPropertyMetadata:
  47.       Service: fc.aliyuncs.com
  48.     Default: ''
  49.   OOSAssumeRole:
  50.     Label:
  51.       en: OOSAssumeRole
  52.       zh-cn: OOS扮演的RAM角色
  53.     Type: String
  54.     Default: ''
  55. RamRole: '{{ OOSAssumeRole }}'
  56. Tasks:
  57.   - Name: ExecuteScript
  58.     Action: ACS::FC::ExecuteScript
  59.     Description:
  60.       en: Run the python script
  61.       zh-cn: 运行Python脚本
  62.     Properties:
  63.       runtime: 'python3.10'
  64.       role: '{{ FCAssumeRole }}'
  65.       script: |-
  66.         import oss2
  67.         import requests
  68.         
  69.         def handler(event, context):
  70.           # 获取FC角色credential
  71.           auth = oss2.StsAuth(context.credentials.access_key_id, context.credentials.access_key_secret, context.credentials.security_token)
  72.           endpoint = 'https://oss-{{OSSRegionId}}.aliyuncs.com'
  73.           bucket = oss2.Bucket(auth, endpoint, '{{OSSBucketName}}')
  74.           file_url = '{{FileUrl}}'
  75.           # 下载文件
  76.           file_content = requests.get(file_url)
  77.           file_name = file_url.split('/')[-1]
  78.           # 将文件上传到指定OSS
  79.           bucket.put_object(f'{{OSSDirectory}}{file_name}', content)
复制代码
示例脚本说明:

  • 运行环境默认 python3.10
  • 函数名称默认 index.handler
  • 使用模块oss2和requests,详情请查看Python内置模块

来源:https://www.cnblogs.com/alicloudros/p/18237640
免责声明:由于采集信息均来自互联网,如果侵犯了您的权益,请联系我们【E-Mail:cb@itdo.tech】 我们会及时删除侵权内容,谢谢合作!

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x

举报 回复 使用道具