在Firehose中编写用于PUT的AWS Lambda函数

时间:2019-07-08 14:10:56

标签: python-3.x

我是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.'

1 个答案:

答案 0 :(得分:0)

这是从AWS到示例code的链接,以写入S3和 documentation for Amazon Web Services(AWS)SDK for Python。

此外,您可以签出Amazon Kinesis Data Firehose api reference文档