繁体   English   中英

从一个 S3 存储桶修改 Json 文件并通过 lamda 发送到另一个 S3 存储桶

[英]Modify Json File from one S3 bucket and send to another S3 bucket through Lamda

有谁知道我如何修改以下代码以从一个 s3 存储桶获取 json 文件,对其进行修改并将其发送到 python 中的另一个 s3 存储桶

import json
import boto3


s3_client = boto3.client('s3')
def lambda_handler(event, context):
    # First we will fetch bucket name from event json object
    bucket = event['Records'][0]['s3']['bucket']['name']
    # Now we will fetch file name which is uploaded in s3 bucket from event json object
    json_file_name = event['Records'][0]['s3']['object']['key']
    #Lets call get_object() function which Retrieves objects from Amazon S3 as dictonary
    json_object = s3_client.get_object(Bucket=bucket,Key=json_file_name)
    # Lets decode the json object returned by function which will retun string
    file_reader = json_object['Body'].read().decode("utf-8")
    
    # We will now change this json string to dictonary
    openAWSEC2PricesJson = json.loads(file_reader)
    
    openReferenceUUIDAWSEC2Prices = open("./assets/referenceUUIDAwsEC2Prices.json", "r")
    openReferenceUUIDAWSEC2PricesJson = json.load(openReferenceUUIDAWSEC2Prices)
    openReferenceUUIDAWSEC2Prices.close()

    for i in openAWSEC2PricesJson:
        for j in openReferenceUUIDAWSEC2PricesJson:
            grouping_code = str(i['region']+'_'+i['operatingSystem']+'_'+i['name'])
            if grouping_code == j['groupingCode']:
                id = j['uniqueID']
                i['id'] = id
        if 'id' not in i:
            id_new = uuid.uuid4()
            i['id'] = str(id_new)
            grouping_code_new = str(i['region']+'_'+i['operatingSystem']+'_'+i['name'])
            res = {}
            res['groupingCode'] = grouping_code_new
            res['uniqueID'] = str(id_new)
            openReferenceUUIDAWSEC2PricesJson.append(res)
            
    writeAWSEC2Prices = open("awsEC2Pricebook.json", "w")
    json.dump(openAWSEC2PricesJson, writeAWSEC2Prices)
    writeAWSEC2Prices.close()


    writeReferenceUUIDAWSEC2Prices = open("./assets/referenceUUIDAwsEC2Prices.json", "w")
    json.dump(openReferenceUUIDAWSEC2PricesJson, writeReferenceUUIDAWSEC2Prices)
    writeReferenceUUIDAWSEC2Prices.close()

目前我在测试时收到以下错误:

"errorMessage": "[Errno 30] Read-only file system: 'awsEC2Pricebook.json'",

您可以尝试将“awsEC2Pricebook.json”存储到“/tmp/awsEC2Pricebook.json”并查看是否可以解决吗?

writeAWSEC2Prices = open("/tmp/awsEC2Pricebook.json", "w")

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM