首页 > 解决方案 > 在 Firehose 中为 PUT 编写 AWS Lambda 函数

问题描述

我是 Lambda 新手,正在尝试模拟一个简单的函数来将 PUT 放入 Kinesis Fireshose。

我尝试通过 AWS 文档查找,但找不到任何确切的参考来编写简单的 python 脚本来从 API 执行 GET 并通过 Firehose 将 JSON 发送到 S3。下面是我试图发布到 Lambda 的代码,但不是文件系统,而是我想按计划将其发送到 Firehose。

# Get weather from OWM and use args for correct type.
def get_weather(gtype, lat, lon, key):
    if gtype == 'current':
        apitype = "weather?"
    elif gtype == 'forecast':
        apitype = "forecast?"
    else:
        print("Undefined GET type: use 'current' or 'forecast'.")
    try:
        api = "http://api.openweathermap.org/data/2.5/" + apitype
        PARAMS = {'lat': lat, 'lon': lon, 'appid': key}
    except:
        return 'Invalid GET request'
    with requests.session() as s:
        rc = s.get(url=api, params=PARAMS)
    data = rc.json()
    return data

# Write data to json files.
def write_to_current(location, gtype, lat, lon, key):
    with open(location + '/current.json', 'w') as outfile:  
        json.dump(get_weather(gtype, lat, lon, key), outfile)
    return 'Current write complete.'

标签: python-3.xamazon-web-servicesamazon-kinesis

解决方案



推荐阅读