简体   繁体   中英

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

Does anyone know how I can modify the following code to get the json file from one s3 bucket, modify it and send it to another s3 bucket in python

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()

Currently I get the following error when I test it:

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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