Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
SAE storage for Django
# -*- coding: utf-8 -*-
import os, time, random
from django.core.files.base import File
from import Storage
from django.conf import settings
from django.core.files import File
from cStringIO import StringIO
except ImportError:
from StringIO import StringIO
class SaeStorage(Storage):
def __init__(self, location="base"
#self.accesskey = accesskey
#self.secretkey = secretkey
#self.prefix = prefix
#self.media_root = media_root
#self.media_url = media_url
#self.client =, secretkey, prefix)
self.prefix = location
self.client =
def _put_file(self, name, content):
ob =
self.client.put(self.prefix, name, ob)
def _read(self, name):
memory_file = StringIO()
o = self.client.get(self.prefix, name)
except, e:
return memory_file
def _open(self, name, mode="rb"):
return SaeStorageFile(name, self, mode=mode)
def _save(self, name, content):
if hasattr(content, 'chunks'):
content_str = ''.join(chunk for chunk in content.chunks())
content_str =
self._put_file(name, content_str)
return name
def delete(self, name):
self.client.delete(self.prefix, name)
def exists(self, name):
o = self.client.stat(self.prefix, name)
except, e:
return False
return True
def listdir(self,domain):
files = self.client.list(domain)
return ((),files)
def size(self, name):
stat = self.client.stat(self.prefix, name)
except, e:
return 0
return stat.length
def url(self, name):
return self.client.url(self.prefix, name)
class SaeStorageFile(File):
"""docstring for SaeStorageFile"""
def __init__(self, name, storage, mode):
self._name = name
self._storage = storage
self._mode = mode
self._is_dirty = False
self.file = StringIO()
self._is_read = False
def size(self):
if not hasattr(self, '_size'):
self._size = self._storage.size(self._name)
return self._size
def read(self):
if not self._is_read:
self.file = self._storage._read(self._name)
self._is_read = True
return self.file.getvalue()
def write(self, content):
if 'w' not in self._mode:
raise AttributeError("File was opened for read-only access.")
self.file = StringIO(content)
self._is_dirty = True
self._is_read = True
def close(self):
if self._is_dirty:
self._storage._put_file(self._name, self.file.getvalue())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.