1

当我尝试将过滤后的数据附加到 output.txt 时出现错误

错误:响应:

{
  "errorMessage": "[Errno 30] Read-only file system: 'output.txt'",
  "errorType": "OSError",
  "stackTrace": [
    [
      "/var/task/lambda_function.py",
      37,
      "lambda_handler",
      "open_file = open(\"output.txt\", \"a\")"
    ]
  ]
}
import json
import urllib.parse
import boto3

print('Loading function')


s3 = boto3.client('s3')

def lambda_handler(event, context):


    #1 - Get the bucket name
    bucket = event['Records'][0]['s3']['bucket']['name']

    #2 - Get the file/key name
    key = urllib.parse.unquote_plus(event['Records'][0]['s3']['object']['key'], encoding='utf-8')



    #3 - Fetch the file from S3
    response = s3.get_object(Bucket=bucket, Key=key)
    target_bucket='blocketrequest'
    FILE='Filtered' + key



    #4 - Deserialize the file's content
    text = response["Body"].read().decode()
    e  = text.split("\n")


    #5 - Print the content
    print(text)

    #6 - Parse and print the Action
    open_file = open("output.txt", "a")

    for each in e:
        loaded_data = json.loads(e)
        if loaded_data["action"] == "ALLOW":
            print("dropped")
        else :
            open_file.write(json.dumps(loaded_data))
    open_file.close()        
          s3.put_object(Body=Output,Bucket='target_bucket',Key='FILE')
    print('File written to s3')
4

1 回答 1

1

通过在正文中添加 json.dumps 解决

于 2020-04-13T10:57:33.560 回答