From 8cb7ca9825dc35b97acd5f922f549254f91a9ec5 Mon Sep 17 00:00:00 2001 From: Ben Simon Hartung <42031100+bentsku@users.noreply.github.com> Date: Tue, 16 Apr 2024 09:19:07 +0200 Subject: [PATCH] fix S3 post not passing all fields (#24) --- bin/deploy.sh | 2 +- lambdas/presign/handler.py | 5 ++++- website/app.js | 18 ++++++++++-------- 3 files changed, 15 insertions(+), 10 deletions(-) diff --git a/bin/deploy.sh b/bin/deploy.sh index 8f63de7..80f4a2d 100755 --- a/bin/deploy.sh +++ b/bin/deploy.sh @@ -51,7 +51,7 @@ if [ "$os" == "Darwin" ]; then ( cd lambdas/resize rm -rf libs lambda.zip - docker run --platform linux/x86_64 -v "$PWD":/var/task "public.ecr.aws/sam/build-python3.9" /bin/sh -c "pip install -r requirements.txt -t libs; exit" + docker run --platform linux/x86_64 --rm -v "$PWD":/var/task "public.ecr.aws/sam/build-python3.9" /bin/sh -c "pip install -r requirements.txt -t libs; exit" cd libs && zip -r ../lambda.zip . && cd .. zip lambda.zip handler.py rm -rf libs diff --git a/lambdas/presign/handler.py b/lambdas/presign/handler.py index 31ac15b..3e31689 100644 --- a/lambdas/presign/handler.py +++ b/lambdas/presign/handler.py @@ -31,7 +31,10 @@ def handler(event, context): raise ValueError("no key given") # make sure the bucket exists - s3.create_bucket(Bucket=bucket) + try: + s3.head_bucket(Bucket=bucket) + except Exception: + s3.create_bucket(Bucket=bucket) # make sure the object does not exist try: diff --git a/website/app.js b/website/app.js index c4d79c9..143d541 100644 --- a/website/app.js +++ b/website/app.js @@ -78,21 +78,23 @@ let urlToCall = functionUrlPresign + "/" + fileName console.log(urlToCall); - let form = this; - $.ajax({ url: urlToCall, success: function (data) { console.log("got pre-signed POST URL", data); - // set form fields to make it easier to serialize let fields = data['fields']; - $(form).attr("action", data['url']); - for (let key in fields) { - $("#" + key).val(fields[key]); - } - let formData = new FormData($("#uploadForm")[0]); + let formData = new FormData() + + Object.entries(fields).forEach(([field, value]) => { + formData.append(field, value); + }); + + // the file element, "file" needs to be the last element of the form + const fileElement = document.querySelector("#customFile"); + formData.append("file", fileElement.files[0]); + console.log("sending form data", formData); $.ajax({