博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
利用函数计算对oss压缩文件做自动解压处理
阅读量:7197 次
发布时间:2019-06-29

本文共 3366 字,大约阅读时间需要 11 分钟。

前言

一个应用可能已经使用对象存储(Object Storage Service,简称OSS)来存放用户上传的图片,用户可以实现一个函数去下载图片进行处理,并将结果存入OSS或者其他服务。如果OSS能够帮助我们关注新上传的图片,并且自动的去调用执行相应的处理函数,用户就不需要再去自己调用函数了,从而简化了开发和使用流程。

函数计算提供了一种事件驱动的计算模型。函数的执行是由事件驱动的,函数计算触发器描述了一组规则,当某个事件满足这些规则,事件源就会触发相应的函数。

示例内容

在本教程示例中,一个被存入OSS的文件,如果它是以source/为前缀,并且后缀是.gz .tar .zip的文件,那么OSS就会自动的调用相应的函数进行解压处理。

# -*- coding: utf-8 -*-import oss2, jsonimport gzipimport tarfile import zipfileimport osimport cStringIO# This template code can decompress the following three types of compression files.#.gz .tar  .zip # if you want uncompress rar or for more info# please refer to https://yq.aliyun.com/articles/468050?spm=a2c4e.11153959.teamhomeleft.1.1534d3c20HYMx5def handler(event, context):    """    The object from OSS will be decompressed automatically .    param: event:   The OSS event json string. Including oss object uri and other information.        param: 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    """    evt_lst = json.loads(event)    creds = context.credentials    # Required by OSS sdk    auth=oss2.StsAuth(        creds.access_key_id,        creds.access_key_secret,        creds.security_token)    evt = evt_lst['events'][0]    bucket_name = evt['oss']['bucket']['name']    endpoint = 'oss-' +  evt['region'] + '.aliyuncs.com'    bucket = oss2.Bucket(auth, endpoint, bucket_name)    object_name = evt['oss']['object']['key']    """    When a source/ prefix object is placed in an OSS, it is hoped that the object will be decompressed and then stored in the OSS as processed/ prefixed.    For example, source/a.zip will be processed as processed/a/...     "Source /", "processed/" can be changed according to the user's requirements.    """    newKey = object_name.replace("source/", "processed/")    remote_stream = bucket.get_object(object_name)    if not remote_stream:        raise RuntimeError('failed to get oss object. bucket: %s. object: %s' % (bucket_name, object_name))    print 'download object from oss success: {}'.format(object_name)    file_type = os.path.splitext(object_name)[1]    if file_type == ".gz":        data = cStringIO.StringIO(remote_stream.read())        newKey = newKey.strip()[:-3]        with gzip.GzipFile(mode = 'rb', fileobj = data) as f:            r_data = f.read()              bucket.put_object(newKey, r_data)                elif file_type == ".tar":        data = cStringIO.StringIO(remote_stream.read())        with tarfile.TarFile(mode = 'r', fileobj = data) as tar:            newKey.replace(".tar", "")            names = tar.getnames()            for name in names:                r = tar.extractfile(name)                if r: # filter folder                    bucket.put_object(newKey +  name, r.read())                    r.close()                        elif file_type == ".zip":        data = cStringIO.StringIO(remote_stream.read())        with zipfile.ZipFile(data,"r") as zip_file:            newKey.replace(".zip", "")            for name in zip_file.namelist():                file = zip_file.open(name)                r_data = file.read()                if r_data: # filter folder                    bucket.put_object(newKey +  name, r_data)                file.close()

FAQ

如果是文件是后缀是.rar ,怎么解?

rar 这种格式如果用python,需要依赖第三方module和二进制文件,附件提供代码下载

转载地址:http://gsxkm.baihongyu.com/

你可能感兴趣的文章
职场新手如何长大
查看>>
拇指接龙游戏升级记录02(Cocos2d-x 3.8.1集成ShareSDK遇到的问题)
查看>>
Python list(列表) 详细总结
查看>>
[JavaScript] 匿名函数
查看>>
tomcat用普通用户启动
查看>>
CCNA 交换机安全 实验
查看>>
2012 VDI环境下,实现WIN7/ThinPC客户端自动配置RemoteAPP和桌面连接
查看>>
如何构建Keepalived+HAProxy实现高可用,负载均衡,动静分离。
查看>>
推荐一款小巧的SQL Server运维工具SqlOps
查看>>
IT人的感慨----时间都去哪了?
查看>>
图数据可视化工具:Gephi
查看>>
rsync生产排错思路细节15
查看>>
web安全检测的两大利器与对比应用
查看>>
使用组策略管理域用户IE浏览器设置
查看>>
Windows Server 2012 NIC Teaming配置实战
查看>>
使用Google API Tool:Infographics生成二维码
查看>>
Lync 小技巧-21-通过Google浏览器加入微软Lync 2013会议
查看>>
DPM2012系列之十八:如何保护工作组计算机
查看>>
我是个年轻人,我心情不太好
查看>>
微软MCITP系列课程(六)NTFS磁盘的安全与管理
查看>>