# Path: buckets/utils.py
# def validate_settings():
#     assert settings.AWS, \
#         "No AWS settings found"
#     assert settings.AWS.get('ACCESS_KEY'), \
#         "AWS access key is not set in settings"
#     assert settings.AWS.get('SECRET_KEY'), \
#         "AWS secret key is not set in settings"
#     assert settings.AWS.get('BUCKET'), \
#         "AWS bucket name is not set in settings"
# 
# 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:])))
# 
# 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)


import os
import boto3
from django.conf import settings
from django.core.files.storage import Storage
from django.core.files import File
from botocore.client import Config
from botocore.exceptions import ClientError
from .utils import validate_settings, random_id, ensure_dirs
        self.access_key = settings.AWS['ACCESS_KEY']
        self.secret_key = settings.AWS['SECRET_KEY']
        self.region = settings.AWS['REGION']
        self.bucket_name = settings.AWS['BUCKET']
        self.max_size = settings.AWS.get('MAX_FILE_SIZE')

        ensure_dirs('downloads')

    def get_boto_ressource(self):
        return boto3.resource(
            's3',
            aws_access_key_id=self.access_key,
            aws_secret_access_key=self.secret_key,
            region_name=self.region,
            config=Config(signature_version='s3v4')
        )

    def _open(self, name, mode='rb'):
        s3 = self.get_boto_ressource()
        f = s3.Object(self.bucket_name, name).get()
        write_path = os.path.join(settings.MEDIA_ROOT, 's3/downloads',
                                  name.split('/')[-1])

        with open(write_path, 'wb') as w:
            to_read = True
            body = f['Body']
            while to_read:
                chunk = body.read(1024)
                if chunk:
                    w.write(chunk)

# Path: buckets/utils.py
# def validate_settings():
#     assert settings.AWS, \
#         "No AWS settings found"
#     assert settings.AWS.get('ACCESS_KEY'), \
#         "AWS access key is not set in settings"
#     assert settings.AWS.get('SECRET_KEY'), \
#         "AWS secret key is not set in settings"
#     assert settings.AWS.get('BUCKET'), \
#         "AWS bucket name is not set in settings"
# 
# 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:])))
# 
# 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)


# Path: buckets/storage.py
import os
import boto3
from django.conf import settings
from django.core.files.storage import Storage
from django.core.files import File
from botocore.client import Config
from botocore.exceptions import ClientError
from .utils import validate_settings, random_id, ensure_dirs

        self.access_key = settings.AWS['ACCESS_KEY']
        self.secret_key = settings.AWS['SECRET_KEY']
        self.region = settings.AWS['REGION']
        self.bucket_name = settings.AWS['BUCKET']
        self.max_size = settings.AWS.get('MAX_FILE_SIZE')

        ensure_dirs('downloads')

    def get_boto_ressource(self):
        return boto3.resource(
            's3',
            aws_access_key_id=self.access_key,
            aws_secret_access_key=self.secret_key,
            region_name=self.region,
            config=Config(signature_version='s3v4')
        )

    def _open(self, name, mode='rb'):
        s3 = self.get_boto_ressource()
        f = s3.Object(self.bucket_name, name).get()
        write_path = os.path.join(settings.MEDIA_ROOT, 's3/downloads',
                                  name.split('/')[-1])

        with open(write_path, 'wb') as w:
            to_read = True
            body = f['Body']
            while to_read:
                chunk = body.read(1024)
                if chunk:
                    w.write(chunk)

                else: