Skip to content

Commit

Permalink
Add ContentType in S3 upload (#42)
Browse files Browse the repository at this point in the history
Co-authored-by: Amin Alaee <[email protected]>
  • Loading branch information
Shauryadhaka and aminalaee authored Jan 31, 2024
1 parent 9cfe9d4 commit ebd48fd
Showing 1 changed file with 9 additions and 2 deletions.
11 changes: 9 additions & 2 deletions fastapi_storages/s3.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import mimetypes
import os
from pathlib import Path
from typing import BinaryIO
Expand All @@ -18,6 +19,8 @@ class S3Storage(BaseStorage):
Requires `boto3` to be installed.
"""

default_content_type = "application/octet-stream"

AWS_ACCESS_KEY_ID = os.environ.get("AWS_ACCESS_KEY_ID", "")
"""AWS access key ID. Either set here or as an environment variable."""

Expand Down Expand Up @@ -110,8 +113,12 @@ def write(self, file: BinaryIO, name: str) -> str:

file.seek(0, 0)
key = self.get_name(name)

self._bucket.upload_fileobj(file, key, ExtraArgs={"ACL": self.AWS_DEFAULT_ACL})
content_type, _ = mimetypes.guess_type(key)
params = {
"ACL": self.AWS_DEFAULT_ACL,
"ContentType": content_type or self.default_content_type,
}
self._bucket.upload_fileobj(file, key, ExtraArgs=params)
return key

def generate_new_filename(self, filename: str) -> str:
Expand Down

0 comments on commit ebd48fd

Please sign in to comment.