Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enhancement: put_object to infer ContentType if not provided #3904

Closed
2 tasks
thunderpoot opened this issue Oct 19, 2023 · 3 comments
Closed
2 tasks

Enhancement: put_object to infer ContentType if not provided #3904

thunderpoot opened this issue Oct 19, 2023 · 3 comments
Assignees
Labels
feature-request This issue requests a feature. response-requested Waiting on additional information or feedback. s3

Comments

@thunderpoot
Copy link

thunderpoot commented Oct 19, 2023

Describe the feature

When using the put_object method in boto3, if the ContentType is not specified, the object does not have a ContentType set. Many tools, including the AWS CLI, are capable of inferring the ContentType based on the file extension. It might be beneficial for boto3 to have a similar capability, especially for commonly used file types.

Use Case

When doing something like the following:

s3.put_object(Bucket='mybucket', Key='path/to/file.html', Body=my_content)

One might expect the uploaded file to have a ContentType of text/html if it hasn't been explicitly set.

Proposed Solution

Enhance the put_object method to infer the ContentType based on the file extension if it's not provided. This behavior could be opt-in to preserve backward compatibility.

Other Information

No response

Acknowledgements

  • I may be able to implement this feature request
  • This feature might incur a breaking change

SDK version used

1.28.62

Environment details (OS name and version, etc.)

macOS Sonoma 14.0 (23A344), Python 3.11.5

@thunderpoot thunderpoot added feature-request This issue requests a feature. needs-triage This issue or PR still needs to be triaged. labels Oct 19, 2023
@tim-finnigan tim-finnigan self-assigned this Oct 20, 2023
@tim-finnigan
Copy link
Contributor

Hi @thunderpoot thanks for reaching out. The put_object command cannot support this because it must maintain consistency with the S3 PutObject API. As an alternative, you could consider the workaround referenced here using upload_file (or put_object):

import mimetypes 

mimetype, _ = mimetypes.guess_type(local_path)
if mimetype is None:
    raise Exception("Failed to guess mimetype")

s3.upload_file(
    Filename=local_path,
    Bucket=bucket,
    Key=remote_path,
    ExtraArgs={
        "ContentType": mimetype
    }
)

As you mentioned the AWS CLI does guess the MIME type for you when running the S3 cp and sync commands (and here is where that happens). To support this ability in Boto3 would be a backwards incompatible change as you suggested, and I don't think adding opt-in support for upload_file would be prioritized given the simple workaround provided above. (The S3 CLI commands cp/sync do have a --no-guess-mime-type parameter that allows you to opt out of the default behavior of those commands.)

@tim-finnigan tim-finnigan added response-requested Waiting on additional information or feedback. s3 and removed needs-triage This issue or PR still needs to be triaged. labels Oct 20, 2023
@thunderpoot
Copy link
Author

Hi @tim-finnigan, thank you for the detailed response. I understand the need for put_object to maintain consistency with the underlying S3 PutObject API and appreciate the explanation.

The workaround you provided with upload_file and mime-types is indeed helpful, and I'll most likely integrate it into my workflow.

Thank you for your time and the work you put into maintaining and improving Boto3.

@thunderpoot thunderpoot closed this as not planned Won't fix, can't repro, duplicate, stale Oct 20, 2023
@github-actions
Copy link

⚠️COMMENT VISIBILITY WARNING⚠️

Comments on closed issues are hard for our team to see.
If you need more assistance, please either tag a team member or open a new issue that references this one.
If you wish to keep having a conversation with other community members under this issue feel free to do so.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature-request This issue requests a feature. response-requested Waiting on additional information or feedback. s3
Projects
None yet
Development

No branches or pull requests

2 participants