簡體   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