Serverless and uploading image from Lambda endpoint to S3

Serverless and uploading image from Lambda endpoint to S3



I'm using the Serverless framework with Python. What I'm trying to accomplish is have an HTTP endpoint setup by Serverless and have that endpoint upload an image to S3. The client, Android, would upload an image from their phone and hit the endpoint with the image. I've been able to successfully upload to S3 but when I click on download for the image and try to open it, it appears to be corrupted.


download



enter image description here



I'm using the file_uploadobj function and it expects a file like object as its first argument. It should be noted that the type of event['body'] is unicode.


event['body']


unicode



Here is the code that I'm using to accomplish this upload.


import boto3

def upload_image(event, context):
headers, body = event['headers'] ,event['body']
client = boto3.client('s3', region_name='us-west-2')
filename = event['queryStringParameters']['filename']
tmp_filename = '/tmp/' + filename
file__ = open(tmp_filename, 'wb')
file__.write(body)
# file__.write(body.encode('utf-8'))
# file__.write(body.encode('base64'))
file__.close()
with open(tmp_filename, 'rb') as file_data:
client.upload_fileobj(file_data, 'bucket', filename, ExtraArgs='ContentType': 'image/jpeg')
return
'statusCode': 200,
'body': 'path': filename



Does anyone have any ideas as to what I'm doing wrong?



I've also tried the following using upload_file.


import base64

def upload_image(event, context):
headers, body = event['headers'] ,event['body']
client = boto3.client('s3', region_name='us-west-2')
filename = event['queryStringParameters']['filename']
tmp_filename = '/tmp/' + filename
with open(tmp_filename, 'wb') as file_descriptor:
file_descriptor.write(base64.b64decode(data))
client.upload_file(tmp_filename, 'bucket', filename, ExtraArgs='ContentType': 'image/jpeg')
return
'statusCode': 200,
'body': 'path': filename





can you look at the metadata of the object? What's the content-type and the size of the object?
– Kendrick Kesley
Aug 22 at 5:29






@KendrickKesley here's the data of the body gist.github.com/Petesta/d1d0d18b0694923f29b97d747d22cff2. The content-type is multipart/form-data.
– Petesta
Aug 22 at 18:07


content-type


multipart/form-data









By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

Edmonton

Crossroads (UK TV series)