Пу sh файл из лямбды в с3 - PullRequest
0 голосов
/ 07 апреля 2020

Я переключился на лямбду с потоком кинезиса и ищу запись, в которой действие заблокировано, и добавил данные в выходной файл.

как я могу sh передать этот файл в s3? У меня есть написано ниже, но не уверен.

Новый импорт кода 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')

1 Ответ

0 голосов
/ 08 апреля 2020

Код использует s3.upload_file(), который загружает файл с диска .

Если вы хотите загрузить содержимое из памяти (например, output массив), вы можете использовать:

s3.put_object(Body=json.dumps(output), Bucket=..., Key=...)
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...