简体   繁体   English

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

[英]Convert json to csv from one S3 bucket and upload to another S3 bucket through AWS Lambda

I have tried below code but I am not able to convert the data from json to csv.我尝试了以下代码,但无法将数据从 json 转换为 csv。 Can someone please help me?有人可以帮帮我吗?

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)

Here is the response I am getting for the above code:这是我对上述代码的响应:

{
  "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"
  ]
}

In AWS Lambda, you can only create files in the /tmp/ directory.在 AWS Lambda 中,您只能在/tmp/目录中创建文件。 Therefore, use:因此,使用:

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

A maximum of 512MB is provided, so it is a good idea to delete any temporary files so they do not interfere with future executions of the Lambda function.最多提供 512MB,因此最好删除任何临时文件,以免它们干扰 Lambda function 的未来执行。

暂无
暂无

声明:本站的技术帖子网页,遵循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函数将文件从一个s3存储桶复制到另一个s3存储桶 - In AWS how to Copy file from one s3 bucket to another s3 bucket using lambda function 从一个 S3 存储桶修改 Json 文件并通过 lamda 发送到另一个 S3 存储桶 - Modify Json File from one S3 bucket and send to another S3 bucket through Lamda 使用Lambda将S3数据从一个AWS账户推送到另一个S3存储桶 - Pushing S3 data from one AWS account to another S3 bucket using Lambda 使用 AWS Lambda 将存储在 S3 存储桶中的 JSON 文件转换为 CSV - Using AWS Lambda to convert JSON files stored in S3 Bucket to CSV 将 json 文件从一个 s3 存储桶复制到另一个 s3 存储桶时,无法识别 Json 文件? - Json file is not recognising when copy json files from one s3 bucket to another s3 bucket? 使用 AWS lambda 将文件从一个 s3 存储桶移动到 AWS 中的另一个存储桶 - Move files from one s3 bucket to another in AWS using AWS lambda 使用Aws Lambda将映像上传到AWS S3存储桶 - Upload Image into AWS S3 bucket using Aws Lambda AWS - 使用 CloudFormation 将数据从一个 S3 存储桶移动到另一个存储桶 - AWS - Moving data from one S3 bucket to another with CloudFormation 从一个存储桶到另一个存储桶的 AWS S3 同步命令 - AWS S3 sync command from one bucket to another
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM