Created
April 28, 2017 18:47
-
-
Save ilmesi/d1f565a969f7bf1823f87e0bd6e689e7 to your computer and use it in GitHub Desktop.
Migrate media from django to S3 bucket
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
""" | |
Simple way to re-upload files on django. | |
Using: | |
Django>1.9 | |
django-storages>1.5 | |
boto3 | |
""" | |
from django.core.files import File | |
from my_package.models import ModelWithImage | |
for item in ModelWithImage.objects.all(): | |
filename = '%s_%s.png' % (item.title, item.id) | |
try: | |
with open('media/%s' % filename, 'r') as f: | |
myfile = File(f) | |
item.image = myfile | |
item.save() | |
except IOError, e: | |
print filename, e |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment