更新时间:2023-11-21 GMT+08:00
引入依赖库
支持的依赖库说明
FunctionGraph支持引入标准库及第三方依赖库。
- 标准库
- FunctionGraph支持的非标准库
FunctionGraph内置一些三方件,如表1、表2所示。像标准库一样,在编写代码时直接引入,使用其功能。
表1 Node.js Runtime集成的三方件 名称
功能
版本号
q
异步方法封装
1.5.1
co
异步流程控制
4.6.0
lodash
常用工具方法库
4.17.10
esdk-obs-nodejs
OBS sdk
2.1.5
express
极简web开发框架
4.16.4
fgs-express
在FunctionGraph和API Gateway之上使用现有的Node.js应用程序框架运行无服务器应用程序和REST API 。提供的示例允许您使用Express框架轻松构建无服务器Web应用程序/服务和RESTful API 。
1.0.1
request
简化http调用,支持HTTPS并默认遵循重定向
2.88.0
引入依赖库示例
处理图片的函数代码如下。
# -*- coding: utf-8 -*- from PIL import Image, ImageEnhance from com.obs.client.obs_client import ObsClient import sys import os current_file_path = os.path.dirname(os.path.realpath(__file__)) # append current path to search paths, so that we can import some third party libraries. sys.path.append(current_file_path) region = 'your region' obs_server = 'obs.xxxxxxcloud.com' def newObsClient(context): ak = context.getAccessKey() sk = context.getSecretKey() return ObsClient(access_key_id=ak, secret_access_key=sk, server=obs_server, path_style=True, region=region, ssl_verify=False, max_retry_count=5, timeout=20) def downloadFile(obsClient, bucket, objName, localFile): resp = obsClient.getObject(bucket, objName, localFile) if resp.status < 300: print 'download file', file, 'succeed' else: print('download failed, errorCode: %s, errorMessage: %s, requestId: %s' % resp.errorCode, resp.errorMessage, resp.requestId) def uploadFileToObs(client, bucket, objName, file): resp = client.putFile(bucket, objName, file) if resp.status < 300: print 'upload file', file, 'succeed' else: print('upload failed, errorCode: %s, errorMessage: %s, requestId: %s' % resp.errorCode, resp.errorMessage, resp.requestId) def getObjInfoFromObsEvent(event): s3 = event['Records'][0]['s3'] eventName = event['Records'][0]['eventName'] bucket = s3['bucket']['name'] objName = s3['object']['key'] print "*** obsEventName: %s, srcBucketName: %s, objName: %s", eventName, bucket, objName return bucket, objName def set_opacity(im, opacity): """设置透明度""" if im.mode != "RGBA": im = im.convert('RGBA') else: im = im.copy() alpha = im.split()[3] alpha = ImageEnhance.Brightness(alpha).enhance(opacity) im.putalpha(alpha) return im def watermark(im, mark, opacity=0.6): """添加水印""" try: if opacity < 1: mark = set_opacity(mark, opacity) if im.mode != 'RGBA': im = im.convert('RGBA') if im.size[0] < mark.size[0] or im.size[1] < mark.size[1]: print "The mark image size is larger size than original image file." return False x = (im.size[0] - mark.size[0]) / 2 y = (im.size[1] - mark.size[1]) / 2 layer = Image.new('RGBA', im.size, ) layer.paste(mark, (x, y)) return Image.composite(layer, im, layer) except Exception as e: print ">>>>>>>>>>> WaterMark EXCEPTION: " + str(e) return False def watermark_image(localFile, fileName): im = Image.open(localFile) watermark_image_path = os.path.join(current_file_path, "watermark.png") mark = Image.open(watermark_image_path) out = watermark(im, mark) print "**********finish water mark" name = fileName.split('.') outFileName = name[0] + '-watermark.' + name[1] outFilePath = "/tmp/" + outFileName if out: out = out.convert('RGB') out.save(outFilePath) else: print "Sorry, Save watermarked file Failed." return outFileName, outFilePath def handler(event, context): srcBucket, srcObjName = getObjInfoFromObsEvent(event) outputBucket = context.getUserData('obs_output_bucket') client = newObsClient(context) # download file uploaded by user from obs localFile = "/tmp/" + srcObjName downloadFile(client, srcBucket, srcObjName, localFile) outFileName, outFile = watermark_image(localFile, srcObjName) # 将转换后的文件上传到新的obs桶中 uploadFileToObs(client, outputBucket, outFileName, outFile) return 'OK'
对于标准库和FunctionGraph支持的非标准库,可以直接引入。
对于FunctionGraph暂没有内置的非标准三方库,通过以下步骤引入。
- 将依赖的库文件压缩成ZIP包,上传至OBS存储桶,获得依赖包的OBS存储链接。
- 登录FunctionGraph控制台,在左侧导航栏选择“函数 > 依赖包管理”,进入“依赖包管理”界面。
- 选择“创建依赖包”,弹出“创建依赖包”对话框。
- 输入依赖包名称、运行时语言和OBS存储链接,单击“确定”。
图1 设置依赖包
- 进入函数详情页面,在“代码”页签,单击“依赖代码包”所在行的“添加依赖包”,选择4中创建的依赖包,单击“确定”。
图2 添加依赖包
各个依赖包和代码包之间尽量不要有相同的目录或文件,比如依赖包depends.zip,里面有index.py这个文件,如果代码采用在线编辑方式,函数执行入口为index.handler,这样在函数执行的时候会产生一个代码文件index.py,跟依赖包里面的index.py文件同名,两个文件可能会因覆盖合并而出错。
父主题: 依赖包管理