首页 > 解决方案 > 使用 aws lambda (Python) 读取和写入 aws s3 存储桶中的 html 文件

问题描述

我是 AWS 的新手,我正在尝试从 amazon s3 存储桶中检索一个 Html 文件并使用 aws lambda python 写入它。我尝试使用 urllib 来检索 html 文件,但我无法写入/更新它。目前 html 文件包含基本的 hello world 文本,我想更新它以发布服务器的当前时间。任何建议我如何写入该文件?

非常感谢,

import boto3
import urllib


def lambda_handler(event, context):

    s3 = boto3.client('s3')
    response = s3.list_buckets()
    buckets = [bucket['Name'] for bucket in response['Buckets']]
    print("Bucket Name: %s" % buckets)

    bucket_url = urllib.request.urlretrieve("https://s3.us-east-2.amazonaws.com/testbucket/index.html","index.html")
    #print(index.html)

    #sock = urllib.request.urlopen("https://s3.us-east-2.amazonaws.com/testbucket/index.html") 
    #htmlsource = sock.read()

    #print(type(htmlsource))
    #print(htmlsource)



    from datetime import datetime   
    # TODO implement

    print(datetime.now().time())


    return 'Hello from Lambda'

标签: htmlpython-3.xamazon-s3aws-lambda

解决方案



推荐阅读