-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: Fix bug in boto3 with imagekit that closes files upon upload
- Loading branch information
Ale
committed
Apr 8, 2020
1 parent
b901799
commit 7015083
Showing
1 changed file
with
30 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,38 @@ | ||
# python | ||
import os | ||
# django | ||
from django.conf import settings | ||
# contrib | ||
from tempfile import SpooledTemporaryFile | ||
from storages.backends.s3boto3 import S3Boto3Storage | ||
|
||
|
||
class MediaStorage(S3Boto3Storage): | ||
location = settings.S3_MEDIA_FOLDER | ||
file_overwrite = False | ||
|
||
""" | ||
https://github.com/matthewwithanm/django-imagekit/issues/391#issuecomment-275367006 | ||
https://github.com/boto/boto3/issues/929 | ||
https://github.com/matthewwithanm/django-imagekit/issues/391 | ||
""" | ||
def _save(self, name, content): | ||
""" | ||
We create a clone of the content file as when this is passed to | ||
boto3 it wrongly closes the file upon upload where as the storage | ||
backend expects it to still be open | ||
""" | ||
# Seek our content back to the start | ||
content.seek(0, os.SEEK_SET) | ||
|
||
# Create a temporary file that will write to disk after a specified | ||
# size. This file will be automatically deleted when closed by | ||
# boto3 or after exiting the `with` statement if the boto3 is fixed | ||
with SpooledTemporaryFile() as content_autoclose: | ||
|
||
# Write our original content into our copy that will be closed by boto3 | ||
content_autoclose.write(content.read()) | ||
|
||
# Upload the object which will auto close the | ||
# content_autoclose instance | ||
return super(MediaStorage, self)._save(name, content_autoclose) |
7015083
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am glad I found a solution to this problem. What is the best location in my django project for this file?