我在AWS上的S3存储桶中存储了一系列json文件.
我希望使用AWS lambda python服务来解析此json,并将解析结果发送到AWS RDS MySQL数据库.
我有一个稳定的python脚本,用于解析和写入数据库.我需要lambda脚本来迭代json文件(在添加它们时).
Each json file contains a list, simple consisting of results = [content]
In pseudo-code what I want is:
- Connect to the S3 bucket (
jsondata
) - Read the contents of the JSON file (
results
) - Execute my script for this data (
results
)
I can list the buckets I have by:
import boto3
s3 = boto3.resource('s3')
for bucket in s3.buckets.all():
print(bucket.name)
给予:
jsondata
But I cannot access this bucket to read its results.
似乎没有read
或load
函数.
我希望像这样
for bucket in s3.buckets.all():
print(bucket.contents)
编辑
我有点误会.lambda必须自己下载文件,而不是在S3中读取文件.
从here开始,您似乎必须给lambda一个下载路径,它可以从该路径访问文件本身
import libraries
s3_client = boto3.client('s3')
def function to be executed:
blah blah
def handler(event, context):
for record in event['Records']:
bucket = record['s3']['bucket']['name']
key = record['s3']['object']['key']
download_path = '/tmp/{}{}'.format(uuid.uuid4(), key)
s3_client.download_file(bucket, key, download_path)