I am trying to upload files using pre-signed url to s3 bucket. I am getting the pre-signed url by hitting a rest endpoint, which returns a json with the required details.
Now, I have to take this json and get the input file from user, to upload the file.
Here is the code, I have written in coffeescript. I am taking an input file obj from user and storing in file object and then calling the pre-signed api to get the json.
uploadFile: (e) =>
files_container = e.target.form.elements[0].files
if files_container.length > 0
@file_obj = files_container[0]
console.log(@file_obj)
result = ""
$.ajax
type: 'GET'
url: '/api/upload/get_lead_upload_url'
dataType: "json"
success: (response) ->
console.log(response)
Below, I have to write this python code in coffeescript to upload the file.
with open(object_name, 'rb') as f:
files = {'file': (object_name, f)}
http_response = requests.post(response['url'], data=response['fields'], files=files)
# If successful, returns HTTP status code 204
Below is the part, where I am going wrong while creating the payload like the python code.
formdata = new FormData
formdata = response.fields
formdata['file']= {@file_obj['name'], @file_obj}
console.log(formdata)
The post request is giving me 403 forbidden error.
$.ajax
type:'POST'
dataType: "jsonp"
url: response.url
headers: {'Access-Control-Allow-Origin': '*'}
data: formdata
success: (uploadResponse) ->
console.log(uploadResponse)
error: (uploadResponse) ->
console.log('error')
console.log(uploadResponse)
error: (response) ->
console.log('error')