将JSON从一个S3存储桶转换为csv并通过AWS Lambda上传到另一个S3存储桶

问题描述 投票:0回答:1

我已经尝试过以下代码,但是无法将数据从json转换为csv。有人可以帮我吗?

import boto3
import botocore
import csv
def lambda_handler(event, context):
    BUCKET_NAME = 'name of the bucket' # replace with your bucket name
    KEY = 'OUTPUT.csv' # replace with your object key
    json_data = [{"id":"1","name":"test"},{"id":"2","name":"good"}]
    with open("data.csv", "w") as file:
        csv_file = csv.writer(file)
        csv_file.writerow(['id', 'name'])
        for item in data:
            csv_file.writerow([item.get('id'),item.get('name')])

    csv_binary = open('data.csv', 'rb').read()
    try:
        obj = s3.Object(BUCKET_NAME, KEY)
        obj.put(Body=csv_binary)
    except botocore.exceptions.ClientError as e:
        if e.response['Error']['Code'] == "404":
            print("The object does not exist.")
        else:
            raise
    s3client = boto3.client('s3')
    try:
        download_url = s3client.generate_presigned_url(
                         'get_object',
                          Params={
                              'Bucket': BUCKET_NAME,
                              'Key': KEY
                              },
                          ExpiresIn=3600
        )
        return {"csv_link": download_url}
    except Exception as e:
        raise utils_exception.ErrorResponse(400, e, Log)

这是上面代码的响应:

{
  "errorMessage": "[Errno 30] Read-only file system: 'data.csv'",
  "errorType": "OSError",
  "stackTrace": [
    "  File \"/var/task/lambda_function.py\", line 8, in lambda_handler\n    with open(\"data.csv\", \"wb\") as file:\n"
  ]
}
amazon-s3 aws-lambda python-3.7 json2csv
1个回答
0
投票

在AWS Lambda中,您只能在/tmp/目录中创建文件。因此,使用:

with open("/tmp/data.csv", "w") as file:

提供的最大容量为512MB,因此最好删除任何临时文件,以免它们干扰Lambda函数的未来执行。

© www.soinside.com 2019 - 2024. All rights reserved.