前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >使用阿里函数计算同步OSS增量对象到COS

使用阿里函数计算同步OSS增量对象到COS

原创
作者头像
ictfox
修改2021-07-15 14:54:33
4.3K1
修改2021-07-15 14:54:33
举报
文章被收录于专栏:对象存储COS对象存储COS

在很多客户的对接中,都有增量数据从阿里OSS同步到COS的需求,这里就可以利用到阿里的函数计算来完成。本文以Python 2.7代码为例,给出了阿里函数计算来实现OSS增量数据同步到COS的方法。

一、阿里函数计算

阿里函数计算与腾讯云的SCF类似,都是无服务的执行环境,它支持配置OSS的触发器,借助该功能我们可以把阿里OSS的增量数据同步到COS上。

参考:https://help.aliyun.com/product/50980.html

- 创建服务

在创建函数前,必须先创建一个服务,选定区域,指定服务名称;

参考:https://help.aliyun.com/document_detail/73337.html

- 创建函数

函数是系统调度和运行的单位。函数必须从属于服务,同一个服务下的所有函数共享一些相同的设置,例如服务授权、日志配置。

参考:https://help.aliyun.com/document_detail/73338.html

二、创建并配置OSS触发器

创建OSS的对象存储触发器,同时配置如下参数:

  • Bucket列表:选择对应的OSS Bucket
  • 触发事件:选择OSS相关的触发事件,比如所有create事件:oss.ObjectCreated:*
  • 触发规则:填写触发规则的前缀和后缀

角色创建方式:选择快捷创建,创建的角色为AliyunOSSEventNotificationRole

参考:https://help.aliyun.com/document_detail/74765.html

注意:函数计算和管理的OSS Bucket必须在同一区域!

三、创建代码

阿里函数计算的执行环境里,默认包含了OSS的SDK,这里以Python 2.7环境为示例,展示在阿里函数计算里导入COS Python SDK,并把监听到的增量对象上传到COS里。

1. 创建代码目录

创建一个用于生成函数计算所需python代码的目录。

代码语言:javascript
复制
~ mkdir oss-python-cos

2. 安装cos python sdk

在创建的目录里,使用pip在该目录安装cos-python-sdk-v5。

代码语言:javascript
复制
~ cd oss-python-cos
~ pip install -t . cos-python-sdk-v5
...

3. 编写同步对象代码

如下,创建index.py文件,编写同步对象的代码。

调用OSS的Object流式下载和COS的流式上传接口,无需先下载对象到本地!

注意:函数计算默认的代码文件为index.py,入口函数为:handler()

代码语言:javascript
复制
➜  oss-python-cos cat index.py
# -*- coding: utf-8 -*-
​
import oss2
import json
from qcloud_cos import CosConfig
from qcloud_cos import CosS3Client
import sys
import logging
​
def handler(event, context):
   """
  Replicate the object from OSS to tecent COS.
  event:   The OSS event json string. Including oss object uri and other information.
  context: The function context, including credential and runtime info.
            For detail info, please refer to https://help.aliyun.com/document_detail/56316.html#using-context
  """
   logging.basicConfig(level=logging.INFO, stream=sys.stdout)
​
   ### Get the OSS object
   # Configure with OSS value
   endpoint = 'http://oss-cn-shanghai.aliyuncs.com'
   auth = oss2.Auth('xxx', 'xxx')
​
   # Parse the event to get the source object info
   evt_list = json.loads(event)
   evt = evt_list['events'][0]
   bucket_name = evt['oss']['bucket']['name']
   object_name = evt['oss']['object']['key']
   #print "bucket %s, object %s" % (bucket_name, object_name)
   oss_bucket = oss2.Bucket(auth, endpoint, bucket_name)
​
   # Get the oss object output stream
   try:
       output_stream = oss_bucket.get_object(object_name)
   except oss2.exceptions.NoSuchKey as e:
       print('{0} not found: http_status={1}, request_id={2}'.format(key, e.status, e.request_id))
​
   ### Replicate the OSS object to Tencent COS
   # Config with COS value
   secret_id = 'xxx'
   secret_key = 'xxx'
   region = 'ap-shanghai'
   cos_bucket = "bruins-1253766168"
​
   # Create COS Client
   cos_config = CosConfig(Region=region, SecretId=secret_id, SecretKey=secret_key)
   cos_client = CosS3Client(cos_config)
​
   # Upload COS object with:
   # put_object() - simple upload interface, max object size is 5GB.
   # upload_file_from_buffer() - upload file with simple upload or multipart upload automatically.
   response = cos_client.put_object(Bucket=cos_bucket, Body=output_stream, Key=object_name)
   #response = client.upload_file_from_buffer(Bucket=cos_bucket, Body=output_stream, Key=object_name)
   print(response['ETag'])

4. 上传代码

把本地临时目录里的所有文件打包为zip文件,上传代码包到函数计算中,或者通过文件夹直接上传里面的文件;

upload-code-1
upload-code-1

上传后的函数代码结构如下:

upload-code-2
upload-code-2

5. 测试代码

在阿里OSS的Bucket上,上传新的Object,然后去COS对应Bucket上确认是否Object复制过来。

附件:

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • - 创建服务
  • - 创建函数
  • 二、创建并配置OSS触发器
  • 三、创建代码
    • 4. 上传代码
      • 5. 测试代码
      相关产品与服务
      对象存储
      对象存储(Cloud Object Storage,COS)是由腾讯云推出的无目录层次结构、无数据格式限制,可容纳海量数据且支持 HTTP/HTTPS 协议访问的分布式存储服务。腾讯云 COS 的存储桶空间无容量上限,无需分区管理,适用于 CDN 数据分发、数据万象处理或大数据计算与分析的数据湖等多种场景。
      领券
      问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档