Skip to content

Commit 8cb7ca9

Browse files
authored
fix S3 post not passing all fields (#24)
1 parent c32e540 commit 8cb7ca9

File tree

3 files changed

+15
-10
lines changed

3 files changed

+15
-10
lines changed

bin/deploy.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ if [ "$os" == "Darwin" ]; then
5151
(
5252
cd lambdas/resize
5353
rm -rf libs lambda.zip
54-
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"
54+
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"
5555
cd libs && zip -r ../lambda.zip . && cd ..
5656
zip lambda.zip handler.py
5757
rm -rf libs

lambdas/presign/handler.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,10 @@ def handler(event, context):
3131
raise ValueError("no key given")
3232

3333
# make sure the bucket exists
34-
s3.create_bucket(Bucket=bucket)
34+
try:
35+
s3.head_bucket(Bucket=bucket)
36+
except Exception:
37+
s3.create_bucket(Bucket=bucket)
3538

3639
# make sure the object does not exist
3740
try:

website/app.js

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -78,21 +78,23 @@
7878
let urlToCall = functionUrlPresign + "/" + fileName
7979
console.log(urlToCall);
8080

81-
let form = this;
82-
8381
$.ajax({
8482
url: urlToCall,
8583
success: function (data) {
8684
console.log("got pre-signed POST URL", data);
8785

88-
// set form fields to make it easier to serialize
8986
let fields = data['fields'];
90-
$(form).attr("action", data['url']);
91-
for (let key in fields) {
92-
$("#" + key).val(fields[key]);
93-
}
9487

95-
let formData = new FormData($("#uploadForm")[0]);
88+
let formData = new FormData()
89+
90+
Object.entries(fields).forEach(([field, value]) => {
91+
formData.append(field, value);
92+
});
93+
94+
// the file <input> element, "file" needs to be the last element of the form
95+
const fileElement = document.querySelector("#customFile");
96+
formData.append("file", fileElement.files[0]);
97+
9698
console.log("sending form data", formData);
9799

98100
$.ajax({

0 commit comments

Comments
 (0)