question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

boto3 s3.Object().put() always injects text/plain; charset=utf-8' into ContentType

See original GitHub issue

Python 3.5.2, Ubuntu 14.04, moto 0.4.23, boto3 1.3.1.

It seems like moto is injecting ‘text/plain; charset=utf-8’ into the content type of all files when I read them / upload them as below.

    def test_direct_s3(self):
        bucket_name = '...'
        local_path = 'core/bucket_template/index.html'
        s3_path = 'testzooga/index.html'
        s3 = boto3.resource('s3', verify=False)

        with open(local_path, 'rb') as _file:
            content_type = mimetypes.guess_type(local_path)
            s3.Object(bucket_name, s3_path).put(
                ContentType=content_type[0],
                Body=_file,
                ACL='public-read'
            )

            print(s3.Object(bucket_name, s3_path).content_type)
            # print(s3.Object(bucket_name, s3_path).get())

        assert 1 == 0

    @mock_s3
    def test_mocked_s3(self):
        my_bucket = s3.Bucket('my-cool-bucket')
        my_bucket.create()
        local_path = 'core/bucket_template/index.html'
        s3_path = 'testzooga/index.html'
        s3 = boto3.resource('s3', verify=False)

        with open(local_path, 'rb') as _file:
            content_type = mimetypes.guess_type(local_path)
            s3.Object(my_bucket.name, s3_path).put(
                ContentType=content_type[0],
                Body=_file,
                ACL='public-read'
            )

            print(s3.Object(my_bucket.name, s3_path).content_type)
            # print(s3.Object(my_bucket.name, s3_path).get())

        assert 1 == 0

Will print: text/html during the first test as it writes to the un-mocked S3 text/plain; charset=utf-8, text/html during the second test when writing to the mocked S3

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
thanizebracommented, May 10, 2017

i am seeing this in both Python 3 and Python 2

0reactions
spuleccommented, Mar 15, 2020
Read more comments on GitHub >

github_iconTop Results From Across the Web

S3 — Boto3 Docs 1.26.34 documentation - AWS
Upon receiving this request, Amazon S3 concatenates all the parts in ascending order by part number to create a new object. In the...
Read more >
Text files uploaded to S3 are encoded strangely?
You can explicitly set the "Content-Type: text/plain; charset=utf-8", on the file in the S3 console. This will tell S3 to serve as text....
Read more >
Quickstart and Tutorial — Python Serverless Microframework ...
In this tutorial, you'll use the chalice command line utility to create and deploy a basic REST API. This quickstart uses Python 3.7,...
Read more >
aws_s3_bucket_object | Resources | hashicorp/aws
If you prefer to not have Terraform recreate the object, import the object using aws_s3_object . Provides an S3 object resource.
Read more >
cgi — Common Gateway Interface support ... - Python Docs
A CGI script is invoked by an HTTP server, usually to process user input ... print("Content-Type: text/html") # HTML is following print() #...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found