简体   繁体   English

使用 AWS Lambda 将存储在 S3 存储桶中的 JSON 文件转换为 CSV

[英]Using AWS Lambda to convert JSON files stored in S3 Bucket to CSV

I'm new to Lambda and Python and I've faced an issue with my Lambda function.我是 Lambda 和 Python 的新手,我的 Lambda 函数遇到了问题。 I have several JSON files stored in a S3 bucket, and I wish to convert all JSON files to CSV format.我有几个 JSON 文件存储在 S3 存储桶中,我希望将所有 JSON 文件转换为 CSV 格式。 As I was referring to the Lambda function posted in this tutorial: https://sysadmins.co.za/convert-csv-to-json-files-with-aws-lambda-and-s3-events/正如我指的是本教程中发布的 Lambda 函数: https : //sysadmins.co.za/convert-csv-to-json-files-with-aws-lambda-and-s3-events/

Lambda function:拉姆达函数:

import json
import csv
import boto3
import os
import datetime as dt

s3 = boto3.client('s3')

def lambda_handler(event, context):
    
    datestamp = dt.datetime.now().strftime("%Y/%m/%d")
    timestamp = dt.datetime.now().strftime("%s")
    
    filename_json = "/tmp/file_{ts}.json".format(ts=timestamp)
    filename_csv = "/tmp/file_{ts}.csv".format(ts=timestamp)
    keyname_s3 = "uploads/output/{ds}/{ts}.json".format(ds=datestamp, ts=timestamp)
    
    json_data = []

    for record in event['Records']:
        bucket_name = record['s3']['bucket']['name']
        key_name = record['s3']['object']['key']
        
    s3_object = s3.get_object(Bucket=bucket_name, Key=key_name)
    data = s3_object['Body'].read()
    contents = data.decode('utf-8')
    
    with open(filename_csv, 'a') as csv_data:
        csv_data.write(contents)
    
    with open(filename_csv) as csv_data:
        csv_reader = csv.DictReader(csv_data)
        for csv_row in csv_reader:
            json_data.append(csv_row)
            
    with open(filename_json, 'w') as json_file:
        json_file.write(json.dumps(json_data))
    
    with open(filename_json, 'r') as json_file_contents:
        response = s3.put_object(Bucket=bucket_name, Key=keyname_s3, Body=json_file_contents.read())

    os.remove(filename_csv)
    os.remove(filename_json)

    return {
        'statusCode': 200,
        'body': json.dumps('CSV converted to JSON and available at: {bucket}/{key}'.format(bucket=bucket_name,key=keyname_s3))
    }

I want to achieve a similar outcome using Lambda, but from JSON to CSV instead.我想使用 Lambda 实现类似的结果,但是从 JSON 到 CSV。 How may I go about doing this?我该怎么做呢?

I'd suggest having a look at convtools library:我建议看看convtools库:

from convtools import conversion as c
from convtools.contrib.tables import Table
import json

# input.json
"""
{
    "records": [
        {"a": 1, "b": "c"},
        {"a": 2, "b": "d"},
        {"a": 3, "b": "e"}
    ]
}
"""
with open("input.json") as f:
    input_data = json.load(f)


Table.from_rows(input_data["records"]).into_csv("output.csv")

# output.csv
"""
a,b
1,c
2,d
3,e
"""

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 AWS Lambda:如何读取 S3 存储桶中的 CSV 文件然后将其上传到另一个 S3 存储桶? - AWS Lambda: How to read CSV files in S3 bucket then upload it to another S3 bucket? AWS Lambda:使用Python从s3存储桶中读取csv文件尺寸,而无需使用Pandas或CSV包 - AWS Lambda: read csv file dimensions from an s3 bucket with Python without using Pandas or CSV package 如何从 AWS Lambda 中的 s3 存储桶读取 csv 文件? - How to read csv file from s3 bucket in AWS Lambda? 使用 AWS lambda 将文件从一个 s3 存储桶移动到 AWS 中的另一个存储桶 - Move files from one s3 bucket to another in AWS using AWS lambda 如何使用 AWS Lambda 函数将文件从 AWS S3 存储桶复制到 EC2 linux 机器 - How to copy files from AWS S3 bucket to EC2 linux machine using AWS Lambda Functions 如何使用 Glue 作业将 JSON 从 s3 转换为 CSV 文件并将其保存在同一个 s3 存储桶中 - How to convert JSON to CSV file from s3 and save it in same s3 bucket using Glue job 从 AWS lambda function 中的 s3 存储桶中读取 .mdb 或 .accdb 文件并使用 python 将其转换为 excel 或 csv - Reading .mdb or .accdb file from s3 bucket in AWS lambda function and converting it into excel or csv using python AWS Lambda和S3和Pandas-将CSV加载到S3中,触发Lambda,加载到pandas,放回存储桶中? - AWS Lambda and S3 and Pandas - Load CSV into S3, trigger Lambda, load into pandas, put back in bucket? Python 中的 AWS Lambda 将新文件复制到另一个 s3 存储桶 - AWS Lambda in Python to copy new files to another s3 bucket 创建 AWS lambda function 以在 s3 存储桶中拆分 pdf 个文件 - Creating an AWS lambda function to split pdf files in a s3 bucket
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM