import json
import boto3
s3 = boto3.client('s3')
def lambda_handler(event, context):
bucket_name = event['Records'][0]['s3']['bucket']['name']
object_key = event['Records'][0]['s3']['object']['key']
file_obj = s3.get_object(Bucket=bucket_name, Key=object_key)
file_content = file_obj["Body"].read().decode('utf-8')
print(file_content)
import json
import requests
def lambda_handler(event, context):
url = 'https://jsonplaceholder.typicode.com/posts/1'
response = requests.get(url)
data = json.loads(response.text)
print(data['title'])
在此示例中,我们使用requests库,它不是内置的Python库。在将此函数打包为zip文件时,我们需要包括requests库及其所有依赖项。
无论哪种方法都有其优缺点。AWS Lambda Layers使依赖项的管理更容易,但要付出一些开发和部署的成本。将依赖项直接包含在功能包中可能会使部署更容易,但有时可能需要额外的存储空间。因此,您需要根据您的需求和资源来确定哪种方法最适合您。