在很多客户的对接中,都有增量数据从阿里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.ObjectCreated:*
角色创建方式:选择快捷创建
,创建的角色为AliyunOSSEventNotificationRole
参考:https://help.aliyun.com/document_detail/74765.html
注意:函数计算和管理的OSS Bucket必须在同一区域!
阿里函数计算的执行环境里,默认包含了OSS的SDK,这里以Python 2.7环境为示例,展示在阿里函数计算里导入COS Python SDK,并把监听到的增量对象上传到COS里。
1. 创建代码目录
创建一个用于生成函数计算所需python代码的目录。
~ mkdir oss-python-cos
2. 安装cos python sdk
在创建的目录里,使用pip在该目录安装cos-python-sdk-v5。
~ cd oss-python-cos
~ pip install -t . cos-python-sdk-v5
...
3. 编写同步对象代码
如下,创建index.py文件,编写同步对象的代码。
调用OSS的Object流式下载和COS的流式上传接口,无需先下载对象到本地!
注意:函数计算默认的代码文件为index.py,入口函数为:handler()
➜ 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'])
把本地临时目录里的所有文件打包为zip文件,上传代码包到函数计算中,或者通过文件夹直接上传里面的文件;
上传后的函数代码结构如下:
在阿里OSS的Bucket上,上传新的Object,然后去COS对应Bucket上确认是否Object复制过来。
附件:
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。