Niko Gamulin
Niko Gamulin

Reputation: 66575

AWS boto3 - how to show image instead of forcing download

I am trying to upload the image to S3 and display it but can't figure out how to prevent force download.

this is the function I have written for image upload:

def upload_to_aws(local_file, bucket, s3_file):
    s3 = boto3.client('s3', aws_access_key_id=ACCESS_KEY,
                      aws_secret_access_key=SECRET_KEY)

    try:
        s3.upload_file(local_file, bucket, s3_file, {'ACL': 'public-read', 'ContentType': 'image/jpeg'})
        print("Upload Successful")
        return True
    except FileNotFoundError:
        print("The file was not found")
        return False
    except NoCredentialsError:
        print("Credentials not available")
        return False

Does anyone what is the right configuration in order to display uploaded images?

Upvotes: 0

Views: 1440

Answers (1)

Amit Baranes
Amit Baranes

Reputation: 8152

Looks like you miss the ExtraArgs property. Take a look at the following code snippet :

import boto3
s3 = boto3.resource('s3')
s3.meta.client.upload_file(local_file, bucket, s3_file, ExtraArgs={'ACL': 'public-read', 'ContentType': 'image/jpeg'})

More about S3.Client.upload_file.

To make sure the Content-type set properly, you can check it from the S3 console, right-click on the object and select Properties => Change metadata.

enter image description here

Upvotes: 4

Related Questions