html - 使用 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'
解决方案
只需使用 Python AWS 开发工具包:https ://boto3.readthedocs.io/en/latest/reference/services/s3.html
https://boto3.readthedocs.io/en/latest/guide/s3-example-download-file.html有一个关于如何读取文件的示例。
推荐阅读
- python - Pythonpipbuilder:解决依赖关系 - 列表索引超出范围
- ios - 取消隐藏 iPad 上的导航视图
- spring-boot - 六边形架构中的层
- python - Pandas 或 python,遍历数据框中列中的 SQL 查询,将结果附加到数据框中
- amazon-cognito - 是否可以创建一个信任,允许来自 cognito 的联合用户根据自定义属性担任角色?
- flutter - 带有特殊字符 FLUTTER 的 HIVE openBox 问题
- javascript - 输入框的简单赋值返回 NaN
- node.js - 使用 SQL Server 处理多对多对一关系 - 分配数据的配置文件
- youtube - 访问 Youtube URL 时自动暂停
- html - 当视口宽度不足以使元素水平显示时,如何使元素相互堆叠?