我在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:

  1. Connect to the S3 bucket (jsondata)
  2. Read the contents of the JSON file (results)
  3. 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.

似乎没有readload函数.

我希望像这样

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)

推荐答案

You can use bucket.objects.all() to get a list of the all objects in the bucket (you also have alternative methods like filter, page_sizeand limit depending on your need)

These methods return an iterator with S3.ObjectSummary objects in it, from there you can use the method object.get to retrieve the file.

Json相关问答推荐

从Razor Pages的AJAX Json呈现DataTables问题.Net GET

如何在使用GO时检测JSON中不需要的字段?

如何循环访问多个子数组并在单个JSON中检索数据点

使用更高级别架构中的字段值在$def内实现约束

如何创建生成两个不同对象的JSON数组的SQL查询?

JQ-JSON将键转换为对象

无法使用Jolt变换在嵌套的JSON中提取值

当由.sh脚本执行时,AWS查询字符串不会提取任何数据

使用 jolt 变换展平嵌套 JSON - 非常复杂

添加到数组时出错:找不到Add的重载和参数计数:1

Serde JSON 反序列化枚举

将 YAML 文件转换为 Python JSON 对象

使用 boost 属性树读取 int 数组

Django - 异常处理最佳实践和发送自定义错误消息

通过 RestAssured 中的 JsonPath 访问匿名数组的元素

jQuery JSON 响应总是触发 ParseError

获取一个数字的 PHP 对象属性

YAML 将 5e-6 加载为字符串而不是数字

有没有一种快速的方法可以在文本编辑器中将 JavaScript 对象转换为有效的 JSON?

通过url获取json数据并在python中使用(simplejson)