簡體   English   中英

如何在 Python 中使用 AWS lambda 提供 YAML/文本文件作為響應?

[英]How to serve YAML/text files as response using AWS lambda in Python?

我無法獲得有關如何在 AWS Lambda 中將 static 文件作為響應提供的信息。 我有一個 YAML 文件內容,當用戶點擊 HTTP GET 請求時,該文件內容需要作為 YAML 文件(應在瀏覽器中作為文件下載)發送。 如何配置 lambda function 以及在創建請求時我們應該在 API 網關中進行哪些更改?

樣品 Lambda function:

import yaml
def lambda_handler(event, context):
    test_url = 'http://www.google.com'

    with open("test.yml") as f:
        list_doc = yaml.load(f)
    # updating a variable in yaml file
    list_doc[0]['vars']['url'] = test_url
    with open("/tmp//return_file.yml", "w") as f:
        yaml.dump(list_doc, f, default_flow_style=False)
    # how to send this return_file.yml as http response?
    # return {
    #     'statusCode': 200,
    #     'body': json.dumps('Hello from Lambda!')
    # }

首先,您需要為類型*/*application/octet-stream 啟用 REST API 的二進制支持

然后在你的lambda_function.py中,你需要像這樣返回

    f = yaml.dump(filename, encoding='utf-8')
    return {
        'statusCode': 200,
        'headers': { 'Content-Type': 'application/octet-stream; charset=utf-8'},
        'body': f.decode('utf-8')
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM