简体   繁体   中英

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?

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.

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM