1

I am looking for a solution to upload gzipped media files with django using django-storages (AWS S3).

My settings.py in order to accomplish that are:

# Django Storage - Amazon S3
AWS_QUERYSTRING_AUTH = False
AWS_IS_GZIPPED = True
AWS_PRELOAD_METADATA = True
GZIP_CONTENT_TYPES = (
    'text/css',
    'text/csv',
    'text/html',
    'text/javascript',
    'text/plain',
    'text/xml',
    'text/x-markdown',
    'application/javascript',
    'application/x-javascript',
    'application/json',
    'application/pdf',
    'application/font-woff',
    'application/octet-stream',
    'font/opentype',
    'application/xml',
    'image/png',
    'image/jpeg',
    'image/pjpeg',
    'image/svg+xml',
    'image/gif',
    'image/jpg',
    'video/mp4',
    'video/ogg',
    'video/avi',
    'video/mpeg',
    'video/webm',
)

AWS_ACCESS_KEY_ID = os.environ.get('AWS_ACCESS_KEY', '')
AWS_SECRET_ACCESS_KEY = os.environ.get('AWS_SECRET_KEY', '')

AWS_HEADERS = {
    'Expires': 'Thu, 1 Apr 2020 20:00:00 GMT',
    'Cache-Control': 'max-age=2592000',
}

AWS_STORAGE_BUCKET_NAME = os.environ.get('AWS_STORAGE_BUCKET_NAME', '')

DEFAULT_FILE_STORAGE = 'dgnest.s3utils.MediaRootS3BotoStorage'
MEDIA_URL = 'http://s3.amazonaws.com/%s/media/' % AWS_STORAGE_BUCKET_NAME

STATICFILES_STORAGE = 'dgnest.s3utils.StaticRootS3BotoStorage'
STATIC_URL = 'http://s3.amazonaws.com/%s/static/' % AWS_STORAGE_BUCKET_NAME

And my file storages:

from storages.backends.s3boto import S3BotoStorage
from django.contrib.staticfiles.storage import CachedFilesMixin


class CachedStaticS3BotoStorage(CachedFilesMixin, S3BotoStorage):

    """Extends S3BotoStorage to save static files with hashed filenames."""
    pass


class MediaS3BotoStorage(S3BotoStorage):

    """Extends S3BotoStorage to save media files."""
    # gzip = False
    pass


StaticRootS3BotoStorage = lambda: CachedStaticS3BotoStorage(location='static')
MediaRootS3BotoStorage = lambda: MediaS3BotoStorage(location='media')

My static files are hashed and gzipped very well. I am only struggling with the media files that seems they are gzipped and uploaded to amazon s3 but django can't handle 'cStringIO.StringO' object. This is the error that I got when I try to do a post in one of my models that has a image or file field.

Django Version: 1.6.4
Exception Type: AttributeError
Exception Value:    
'cStringIO.StringO' object has no attribute 'size'
Exception Location: /home/oscar/.virtualenvs/dgnest/local/lib/python2.7/site-packages/django/db/models/fields/files.py in _get_size, line 70

Hope to find some help. Thank you.

oskargicast
  • 667
  • 1
  • 7
  • 14

0 Answers0