Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

notification: download attachments by s3 api urls #1857

Merged
merged 3 commits into from
Dec 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/dev.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ on:

jobs:
run_tests_development:
runs-on: ubuntu-latest
runs-on: ubuntu-22.04
# container: node:20-bookworm
services:
mysql:
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/master.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ on:

jobs:
run_tests_master:
runs-on: ubuntu-latest
runs-on: ubuntu-22.04
# container: node:20-bookworm
services:
mysql:
Expand Down
5 changes: 5 additions & 0 deletions notification/config.py
Original file line number Diff line number Diff line change
@@ -1 +1,6 @@
EMAIL_FOR_SENDING_NOTIFICATION = ""

S3_DOWNLOAD_API = {
"HOST": "",
"TOKEN": ""
}
27 changes: 22 additions & 5 deletions notification/lambda_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from botocore.exceptions import ClientError

from common.logger import get_logger
from notification.config import EMAIL_FOR_SENDING_NOTIFICATION
from notification.config import EMAIL_FOR_SENDING_NOTIFICATION, S3_DOWNLOAD_API

logger = get_logger(__name__)

Expand All @@ -34,6 +34,23 @@ class NotificationType(Enum):
client = boto3.client('ses')


def check_and_fetch_s3_url(attachment_url: str):
"""
Intermediate function to download attachment from specific S3 Bucket by pre-signed links API
Checks attachment url and fetches temporary link to download file from S3 Bucket
If attachment url is not link on S3 download API it will be returned without changes
"""
parsed_url = urlparse(attachment_url)
if not parsed_url.hostname == S3_DOWNLOAD_API["HOST"]:
return attachment_url
headers = {"Authorization": S3_DOWNLOAD_API["TOKEN"]}
response = requests.get(attachment_url, headers=headers)
body = response.json()
key, download_url = body["key"], body["downloadURL"]
logger.info(f"Got a link to download {key} file from S3")
return download_url


def send_email_with_attachment(recipient: str, subject: str, body_html: str, sender: str, attachment_urls: list):
logger.info(f"Receipent={recipient}, subject={subject}, body={body_html}, sender={sender}, "
f"attachment_urls={attachment_urls}")
Expand All @@ -50,17 +67,17 @@ def send_email_with_attachment(recipient: str, subject: str, body_html: str, sen

for attachment_url in attachment_urls:
logger.info(f"Downloading the file from url={attachment_url}")
parsed_url = urlparse(attachment_url)
filename = os.path.basename(parsed_url.path)

try:
attachment_url = check_and_fetch_s3_url(attachment_url)
parsed_url = urlparse(attachment_url)
filename = os.path.basename(parsed_url.path)
response = requests.get(attachment_url)
filepath = f"/tmp/{filename}"
open(filepath, "wb").write(response.content)
attachment_filepaths.append(filepath)
logger.info(f"Download completed for the file from url={attachment_url}")
except Exception as e:
logger.error(f"Unable to download the file from the url={attachment_url} and error={e}")
logger.exception(f"Unable to download the file from the url={attachment_url} and error={e}")

logger.info(f"Attachment file path={attachment_filepaths}")
for attachment_filepath in attachment_filepaths:
Expand Down