函数工作流 FUNCTIONGRAPH-构建程序:创建程序包

时间:2024-11-16 13:21:45

创建程序包

本例使用Python语言实现为图片打水印的功能,有关函数开发的过程请参考Python函数开发。本例不再介绍业务功能实现的代码,样例代码目录如图1所示。

图1 样例代码目录

其中index.py为函数执行的入口文件,index.py中入口函数的代码片段如下,参数“obs_output_bucket”为打水印后的图片存储地址,需要在创建函数时配置自定义参数。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
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'

support.huaweicloud.com/bestpractice-functiongraph/functiongraph_05_0402.html