-
-
Save SiriusDely/10ecb2dc205fb93677f5a8b35282fa1c 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') |
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 contextlib | |
import os | |
from django.core.files import File | |
from django.core.files.storage import default_storage | |
from io import BytesIO | |
from urllib.request import urlopen | |
def save_local_file(local_path): | |
with open(local_path, "rb") as file: | |
fn = os.path.split(local_path)[1] | |
django_file = File(file, fn) | |
path = default_storage.save(fn, django_file) | |
# with an object this would be | |
# obj.image_field.save(fn, file) | |
def save_from_url(url): | |
with contextlib.closing(urlopen(url)) as response: | |
with contextlib.closing(BytesIO(response.read())) as file: | |
file.seek(0) | |
fn = os.path.split(url)[1] | |
django_file = File(file, fn) | |
path = default_storage.save(fn, django_file) | |
# with an object this would be | |
# obj.image_field.save(fn, file) |
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
# Taken from: http://www.revsys.com/blog/2014/dec/03/loading-django-files-from-code/ | |
# Suppose we have a model like this: | |
from django.db import models | |
class Company(models.Model): | |
name = models.CharField(max_length=100) | |
logo = models.ImageField() | |
# then: | |
import requests | |
from django.core.files import File | |
from .models import Company | |
r = requests.get(“http://media.revsys.com/img/revsys-logo.png”) | |
with open(“/tmp/revsys-logo.png”, “wb”) as f: | |
f.write(r.content) | |
reopen = open(“/tmp/revsys-logo.png”, “rb”) | |
django_file = File(reopen) | |
revsys = Company() | |
revsys.name = “Revolution Systems” | |
revsys.logo.save(“revsys-logo.png”, django_file, save=True) | |
# for a bunch of images: | |
import os | |
from urllib.parse import urlparse | |
# from urlparse import urlparse for Python 2.7 | |
url = “http://media.revsys.com/img/revsys-logo.png” | |
filename = os.path.basename(urlparse(url).path) | |
# This returns ‘revsys-logo.png’ from the URL | |
… | |
revsys.logo.save(filename, django_file, save=True) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment