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
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
@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.
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