修改一个S3桶中的Json文件,通过lambda发送到另一个S3桶



有没有人知道我如何修改以下代码从一个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")

最新更新