0

I am uploading png images to a public S3 bucket. However, when I access the images in my browser, instead of displaying inline, they automatically download.

Using this answer, I wrote the following code.

with open(new_img_path, "rb") as f:
    s3.put_object(Bucket=bucket, Key=s3_img_path, Body=f, ContentType="image/png")

When I view the file uploaded, it still forces me to download it.

The way I can fix that problem is by running the CLI command found in this answer.

aws s3 cp \                                              
                s3://bucket/ \
                s3://bucket/ \
                --exclude '*' \
                --include '*.png' \
                --no-guess-mime-type \
                --content-type="image/png" \
                --metadata-directive="REPLACE" \
                --recursive

After running those commands it works just fine. The problem is, I don't want to have to use the CLI to fix something that is being done programmatically.

For reference, here are screenshots of the metadata:

Lance Johnson
  • 103
  • 1
  • 7

1 Answers1

0

I found the problem. The ACL argument wasn't set to public-read.

Here is the working code, formatted for readability:

with open(new_img_path, "rb") as f:
    s3.put_object(
        Bucket=bucket,
        Key=s3_img_path,
        Body=f,
        ContentType="image/png",
        ACL="public-read"
    )
Lance Johnson
  • 103
  • 1
  • 7