Created
September 11, 2018 14:05
-
-
Save melvinkcx/1ab770e45373b61f7b265564a268ec90 to your computer and use it in GitHub Desktop.
Reducing Memory Footprint When Creating Archive in Django
This file contains hidden or 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
from django.core.files.base import ContentFile | |
from django.db import models | |
file_storage = FileSystemStorage(location='/archive') | |
class MyFileModel(models.Model): | |
""" | |
A sample model | |
""" | |
archive = models.FileField(storage=file_storage) | |
@classmethod | |
def upload(cls, file): | |
my_file_model = cls() | |
my_file_model.archive.save( | |
file.name, | |
ContentFile(file.read()) | |
) |
This file contains hidden or 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
import tempfile | |
import zipfile | |
from django.core.files import File | |
temp = tempfile.NamedTemporaryFile() # Django cannot read TempFile | |
with zipfile.ZipFile(temp, mode='w', compression=zipfile.ZIP_DEFLATED) as archive: | |
# Write something into archive | |
# Eg: archive.writestr('{}'.format(filename), file_content) | |
pass | |
# Assume upload() is one of the methods in our Model | |
with transaction.atomic(): | |
temp.seek(0) # Move the cursor back to the begining of the file | |
file = File(temp) # Wrap NamedTempFile with DjangoFile | |
file.name = 'Some File Name.zip' | |
MyFileModel.upload(file) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment