背景
通过调用函数的url,将参数传递给函数执行,函数里如何获取这个参数
过程
- 下一个简单的demo如下
参考这个链接https://support.huaweicloud.com/devg-functiongraph/functiongraph_02_0420.html写一个demo,这个是百度视频云获取token的过程,返回的是一个token。
python">import json
from hashlib import sha1
import hmac
import base64
import binascii
from optparse import OptionParserdef calculate_signature(app_key, app_id, ts, random_str, room_name, uid, expect_ts):data = "ACS{}{}{}{}{}{}".format(app_id, ts, random_str, room_name, uid, expect_ts)app_key = bytes(app_key, encoding='utf-8')data = bytes(data, encoding='utf-8')signature =hmac.new(app_key, data, sha1).digest()#.encode('base64').rstrip()signature = binascii.b2a_hex(signature)return signaturedef calculate_token(version, signature, ts, random_str, expect_ts):token ="{}{}{}{}{}".format(version, signature, ts, random_str, expect_ts)return tokendef handler (event, context):rig=calculate_signature("57krhkd8qfuf222","appqk8fdr57iskp335","1553144847","dabdd97c","cc345","1006255","1731992174")rig=rig.decode()print("结果",rig)res=dir(event)print(res)return {"statusCode": 200,"isBase64Encoded": False,"body": calculate_token("004",rig,"1553144847","dabdd97c","1578380254"),"body": event,"headers": {"Content-Type": "application/json"}}
- 在华为云中给这个函数配置一个触发器apig,配置后就有一个请求地址了,如
https://cccccccc.apig.cn-north-5.huaweicloudapis.com/my?my=6758
3.如何获取这个my的参数的值
其实这个参数就在even中,它是一个json格式数据,产品文档也有说明如下,
执行事件(event): 函数执行界面由用户输入的执行事件参数, 格式为JSON对象
因此,直接在函数中print这个even就可以了,安置好代码后,直接通过apig的地址请求访问 https://cccccccc.apig.cn-north-5.huaweicloudapis.com/my?my=6758,在返回结果的queryStringParameters字段中可以看到请求参数