-
-
Save chenchiyuan/3664536 to your computer and use it in GitHub Desktop.
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
# regist by chenchiyuan, chenchiyuan03@gmail.com, Upyun python sdk, storage for django. | |
# UpYun storage for django, written by Tyr Chen @ tukeq.com | |
# to run this gist you need to get upyun python sdk first. | |
# Waring: need to force name str, so in UpYunStorage _save, name = to_str(name). | |
# Implement django file storage. | |
class Photo(models.Model): | |
name = models.CharField(_('图片名'), max_length=64, help_text=_(''), unique=True) | |
filename = UpYunFileField(verbose_name = _('文件名'), help_text=_('')) | |
@classmethod | |
def create(cls, data, name): | |
photo = cls(name=name) | |
photo.filename.save( | |
name, ContentFile(data), save=False | |
) | |
photo.save() | |
def test(): | |
import urllib2 | |
url_path = 'http://...test.jpg' | |
image = urllib2.open(url_path) | |
data = image.read() | |
Photo.create('test', data) | |
# upyun storage | |
class UpYunStorage(Storage): | |
def __init__(self, bucket=settings.UPYUN_BUCKET): | |
self.upyun = UpYun(bucket, settings.UPYUN_USERNAME, settings.UPYUN_PASSWORD) | |
self.upyun.setApiDomain(settings.UPYUN_API_DOMAIN) | |
self.binding_domain = settings.UPYUN_BINDING_DOMAIN | |
def _open(self, name, mode='rb'): | |
class UpYunFile(File): | |
def __init__(self, name, upyun): | |
self.name = name | |
self.upyun = upyun | |
def size(self): | |
info = self.upyun.getFileInfo(name) | |
if info: | |
return info['size'] | |
return 0 | |
def read(self, *args, **kwargs): | |
return self.upyun.readFile(self.name) | |
def write(self, data): | |
return self.upyun.writeFile(self.name, data) | |
def close(self): | |
return | |
return UpYunFile(name, self.upyun) | |
def _save(self, name, content): | |
name = to_str(name) | |
self.upyun.writeFile(name, content) | |
return name | |
def delete(self, name): | |
self.upyun.deleteFile(name) | |
def exists(self, name): | |
return self.upyun.getFileInfo(name) != None | |
def listdir(self, path): | |
return [d.filename for d in self.upyun.readDir(path)] | |
def path(self, name): | |
raise NotImplementedError | |
def size(self, name): | |
info = self.upyun.getFileInfo(name) | |
if info: | |
return int(info['size']) | |
return 0 | |
def url(self, name): | |
return urljoin(self.binding_domain, name) | |
def get_available_name(self, name): | |
return name | |
def get_upload_to(instance, filename): | |
import os, uuid | |
return '%s%s' % (uuid.uuid4().hex, os.path.splitext(filename)[1]) | |
def set_storage(bucket): | |
if settings.USE_UPYUN: | |
return UpYunStorage(bucket) | |
return None | |
def set_upload_to(upload_to=''): | |
if not upload_to: | |
return get_upload_to | |
return upload_to | |
class UpYunFileField(models.FileField): | |
def __init__(self, bucket=settings.UPYUN_BUCKET, verbose_name=None, name=None, upload_to='', **kwargs): | |
storage = set_storage(bucket) | |
upload_to = set_upload_to(upload_to) | |
super(UpYunFileField, self).__init__(verbose_name, name, upload_to, storage, **kwargs) | |
# usage: | |
# just add UpYunFileField into your model, and the storage will do the magic. You need to setup several consts in settings to make UpYun storage | |
# work. Good luck. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Example by chenchiyuan, chenchiyuan03@gmail.com
First, need to modify some bug of this gist.
def _save(self, name, content):
name = to_str(name) # name must be str, if you use unicode, will raise Exception
self.upyun.writeFile(name, content)
return name
class Photo(models.Model):
class Meta:
verbose_name = verbose_name_plural = _('照片')
name = models.CharField(('图片md5'), max_length=64, help_text=(''), unique=True)
filename = UpYunFileField(verbose_name = ('文件名'), help_text=(''))
@classmethod
def create(cls, data, name, _args, *_kwargs):
photo = cls(name=name, _args, *_kwargs)
photo.filename.save(
name, ContentFile(data), save=False # save the field
)
photo.save()
def test_save():
import urllib2
url_path = 'http://.....some.jpg'
image = urllib2.urlopen(url_path)
data = image.read()
Photo.create('test', data)