Last active
July 22, 2022 10:24
-
-
Save amalgjose/9007f5aac9e9751d595a5232fa3dd6bf to your computer and use it in GitHub Desktop.
Python program to Stream data from a URL and upload it directly to S3 without saving it to local disk. This program is very suitable to use in AWS Lambda functions. This works well with large files. This program acts like a relay between the source and the S3. For more details refer to https://amalgjose.com/2020/08/13/python-program-to-stream-da…
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import boto3 | |
import requests | |
authentication = {"USER": "", "PASSWORD": ""} | |
payload = {"query": "some query"} | |
session = requests.Session() | |
response = session.post("URL", | |
data=payload, | |
auth=(authentication["USER"], | |
authentication["PASSWORD"]), stream=True) | |
s3_bucket = "bucket_name" | |
s3_file_path = "path_in_s3" | |
s3 = boto3.client('s3') | |
with response as part: | |
part.raw.decode_content = True | |
conf = boto3.s3.transfer.TransferConfig(multipart_threshold=10000, max_concurrency=4) | |
s3.upload_fileobj(part.raw, s3_bucket, s3_file_path, Config=conf) |
This is not in the structure of a lambda function. Please include the snippet of code in the Lambda format.
def lambda_handler(event, context):
"code goes here"
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi,
I tried this in AWS Lambda, but getting error -
"errorMessage": "Handler 'lambda_handler' missing on module 'lambda_function'",
"errorType": "Runtime.HandlerNotFound",
"stackTrace": []
Could you please help here?