# Path: buckets/utils.py
# def ensure_dirs(*args):
#     for dir in args:
#         path = os.path.join(settings.MEDIA_ROOT, 's3', dir)
#         if not os.path.exists(path):
#             os.makedirs(path)
# 
# def random_id():
#     rand_id = [random.randint(0, 0xFF) for i in range(ID_FIELD_LENGTH)]
#     return (byte_to_letter(rand_id[0]) +
#             ''.join(map(byte_to_base32_chr, rand_id[1:])))


import os
import urllib
from django.conf import settings
from buckets.utils import ensure_dirs, random_id
        ensure_dirs('downloads', 'uploads')

    def open(self, url):
        name = os.path.basename(urllib.request.url2pathname(url))
        uploaded = os.path.join(self.dir, 'uploads', url)

        with open(os.path.join(self.dir, 'downloads', name), 'wb') as dest:
            with open(uploaded, 'rb') as source:
                dest.write(source.read())

            return dest.name

    def save(self, name, content):
        url = '/media/s3/uploads/' + name

        if '/' in name:
            path = os.path.join(settings.MEDIA_ROOT,
                                's3/uploads', name[:name.rfind('/')])
            if not os.path.exists(path):
                os.makedirs(path)

        with open(os.path.join(self.dir, 'uploads', name), 'wb') as dest:
            dest.write(content)

        return url

    def delete(self, name):
        uploaded = os.path.join(self.dir, 'uploads', name)
        try:
            os.remove(uploaded)

# Path: buckets/utils.py
# def ensure_dirs(*args):
#     for dir in args:
#         path = os.path.join(settings.MEDIA_ROOT, 's3', dir)
#         if not os.path.exists(path):
#             os.makedirs(path)
# 
# def random_id():
#     rand_id = [random.randint(0, 0xFF) for i in range(ID_FIELD_LENGTH)]
#     return (byte_to_letter(rand_id[0]) +
#             ''.join(map(byte_to_base32_chr, rand_id[1:])))


# Path: buckets/test/storage.py
import os
import urllib
from django.conf import settings
from buckets.utils import ensure_dirs, random_id

        ensure_dirs('downloads', 'uploads')

    def open(self, url):
        name = os.path.basename(urllib.request.url2pathname(url))
        uploaded = os.path.join(self.dir, 'uploads', url)

        with open(os.path.join(self.dir, 'downloads', name), 'wb') as dest:
            with open(uploaded, 'rb') as source:
                dest.write(source.read())

            return dest.name

    def save(self, name, content):
        url = '/media/s3/uploads/' + name

        if '/' in name:
            path = os.path.join(settings.MEDIA_ROOT,
                                's3/uploads', name[:name.rfind('/')])
            if not os.path.exists(path):
                os.makedirs(path)

        with open(os.path.join(self.dir, 'uploads', name), 'wb') as dest:
            dest.write(content)

        return url

    def delete(self, name):
        uploaded = os.path.join(self.dir, 'uploads', name)
        try:
            os.remove(uploaded)

        except FileNotFoundError: