Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
574 views
in Technique[技术] by (71.8m points)

amazon web services - PNG image not uploading correctly to AWS S3 with Python and boto3

I'm trying to create an endpoint that allows uploading a picture to my S3 bucket, everything seems to work correctly when uploading PNG images (JPEG images do not work but that's an issue for another time), however if I try to visualize the uploaded image in the browser it just shows as an black page with a little grey square in the middle. If I check the Metadata of the uploaded file, the Content-Type of S3 is correctly set to image/png, so I don't know what may be going on.

I pass the base64 image as multipart form-data and then decode it with

for part in multipart_data.parts:
    file = base64.b64encode(part.content).decode()

after that I use mimetypes guess_all_extensions() function to get the extension of the file. My uploading code looks like the following:

s3_client = boto3.client("s3")
default_bucket = "some_bucket"
        try:
            s3_client.put_object(
                Body=file,
                Bucket=default_bucket,
                Key=file_key,
                ContentType=content_type
            )
       except botocore.exceptions.ClientError as error:
            print(f"error: {error}")

The image opened from the s3 management console looks like the folowing: 1

question from:https://stackoverflow.com/questions/65942399/png-image-not-uploading-correctly-to-aws-s3-with-python-and-boto3

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Update! After many tries, I found out that I had to pass the multipart.part as the file on my put_object() function instead of the decoded base64 string. With that, everything is working as expected.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...