Skip to content
This repository has been archived by the owner on Nov 5, 2024. It is now read-only.

Image corruption when uploading to S3 using Boto3 in Lambda #799

Closed
xanderbilla opened this issue Aug 4, 2024 · 2 comments
Closed

Image corruption when uploading to S3 using Boto3 in Lambda #799

xanderbilla opened this issue Aug 4, 2024 · 2 comments
Assignees

Comments

@xanderbilla
Copy link

I'm encountering an issue when uploading image files to S3 using Boto3 in a Lambda function. The uploaded image is corrupted.

Image format: JPG
S3 bucket settings: No Public Access, no specific ACLs or encryption.
Lambda environment: Python 3.12

Expected behavior: The image should be uploaded to S3 without corruption.
Actual behavior: The uploaded image is corrupted and cannot be viewed correctly.

s3 = boto3.client('s3')

postMethod = 'POST'
uploadFile = '/media/file'

def lambda_handler(event, context):
    logger.info(event)
    httpMethod = event['httpMethod']
    path = event['path']
    
    if httpMethod == postMethod and path == uploadFile:
        file_data = event['body']
        response = s3_upload_file(file_data)
    else:
        response = {
            'statusCode': 404,
            'body': json.dumps('Resource not found')
        }
    return response
        
def s3_upload_file(data):
    try:
        file_name = str(uuid.uuid4())
        response = s3.put_object(
            Body=data,
            Bucket='imggallery-st',
            Key=file_name,
            ContentType='image/jpg'
        )
        return {
            'statusCode': 200,
            'body': json.dumps('File uploaded successfully')
        }
    except ClientError as e:
        return {
            'statusCode': 500,
            'body': json.dumps(str(e))
        }

I've tried different image formats and S3 configurations without success. Any help would be appreciated.

@RyanFitzSimmonsAK
Copy link

Hi @xanderbilla, thanks for reaching out. This repository is for tracking issues that apply to multiple AWS SDKs. I've created an issue in the Boto3 repository (boto/boto3#4229) for this. Please follow-up on that issue. Thanks!

Copy link

github-actions bot commented Aug 6, 2024

This issue is now closed.

Comments on closed issues are hard for our team to see.
If you need more assistance, please either tag a team member or open a new issue that references this one.
If you wish to keep having a conversation with other community members under this issue feel free to do so.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

2 participants