-
-
Save Keda87/a20a1598f79b8d1f5ea701e08f56e999 to your computer and use it in GitHub Desktop.
Simple example of an S3 django storage backend saving a file from local and url
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
import os | |
import urllib2 | |
import contextlib | |
import StringIO | |
from django.core.files.storage import get_storage_class, FileSystemStorage | |
from django.core.files import File | |
from django.conf import settings | |
from galleries.models import GalleryImage | |
OLD_STORAGE = FileSystemStorage() | |
NEW_STORAGE = get_storage_class(settings.REMOTE_FILE_STORAGE)() | |
def save_local_file(local_path): | |
with open(local_path) as file: | |
fn = os.path.split(local_path)[1] | |
django_file = File(file, fn) | |
path = NEW_STORAGE.save(fn, django_file) | |
print local_path, 'saved to', path | |
# save path | |
def save_from_url(url): | |
with contextlib.closing(urllib2.urlopen(url)) as response: | |
with contextlib.closing(StringIO.StringIO(response)) as file: | |
file.seek(0) | |
fn = os.path.split(url)[1] | |
django_file = File(file, fn) | |
path = NEW_STORAGE.save(fn, django_file) | |
# with an object this would be | |
# obj.image_field.save(fn, file) | |
print url, 'saved to', path | |
save_local_file('pip-log.txt') | |
save_from_url('http://static.bbci.co.uk/frameworks/barlesque/2.8.11/desktop/3.5/img/blq-blocks_grey_alpha.png') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment