I have triggered to lambda with the kinesis stream and looking for a record where action is blocked and have appeneded the data to output file.
how can i push that file to s3 ?I have written below but not sure.
New code 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)
#4 - Deserialize the file's content
text = response["Body"].read().decode()
e = text.split("\n")
Output=[]
#5 - Print the content
print(text)
#6 - Parse and print the Action
for each in e:
loaded_data = json.loads(e)
if loaded_data["action"] == "ALLOW":
print("dropped")
else :
Output.append(loaded_data)
s3.put_object(Body='json.dumps(output)',Bucket='blocketreques',Key='Filtered.txt')
print('Put Complete')
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)
#4 - Deserialize the file's content
text = response["Body"].read().decode()
e = text.split("\n")
Output=[]
#5 - Print the content
print(text)
#6 - Parse and print the Action
for each in e:
loaded_data = json.loads(e)
if loaded_data["action"] == "ALLOW":
print("dropped")
else :
Output.append(loaded_data)
s3.put_object(Body='json.dumps(output)',Bucket='blocketreques',Key='Filtered.txt')
print('Put Complete')
The code is using s3.upload_file()
, which uploads a file from disk.
If you want to upload the contents from memory (eg the output
array), you can use:
s3.put_object(Body=json.dumps(output), Bucket=..., Key=...)