Skip to content

Instantly share code, notes, and snippets.

@ilmesi
Created April 28, 2017 18:47
Show Gist options
  • Save ilmesi/d1f565a969f7bf1823f87e0bd6e689e7 to your computer and use it in GitHub Desktop.
Save ilmesi/d1f565a969f7bf1823f87e0bd6e689e7 to your computer and use it in GitHub Desktop.
Migrate media from django to S3 bucket
"""
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