2016-02-15 7 views
12

Nach this guide, und diese [1][2] Beiträge, habe ich versucht, statische Speicher auf AWS S3 mit Django-Speicher Boto einrichten.Django AWS S3 mit Boto mit Compressor nicht komprimiert UncompressableFileError

Bei Ausführung von collectstatic wird der Befehl erfolgreich unter STATIC_ROOT erfasst. Die Dateien werden jedoch nicht in S3-komprimierte Dateien hochgeladen, und der Server kann sie nicht bereitstellen. Ein Fehler von 500 wird zurückgegeben. Mit Blick auf die Protokolle:

Fehlermeldung:

UncompressableFileError: 'https://<myapp>.s3.amazonaws.com/static/oscar/css/styles.css' could not be found in the COMPRESS_ROOT '/var/www/<myappname>/static' or with staticfiles. 

EDIT:

ich auch STATIC_URL zu http://%s/ % AWS_S3_CUSTOM_DOMAIN geändert, und ich bekomme den gleichen Fehler, mit der Ausnahme, dass es nach wie vor bei https suchen ist, aber die COMPRESS_URL ist http.

UncompressableFileError: 'https://<myappname>.s3.amazonaws.com/static/oscar/css/styles.css' isn't accessible via COMPRESS_URL ('http://<myappname>.s3.amazonaws.com/') and can't be compressed 

Ist das eine Inkompatibilität mit Kompressor und Boto?

relevanten Code:

# settings/prod.py 

AWS_ACCESS_KEY_ID = <Key_ID> 
AWS_SECRET_ACCESS_KEY = <Secret_Key> 
AWS_STORAGE_BUCKET_NAME = "<my_bucket_name>" 
AWS_S3_CUSTOM_DOMAIN = "%s.s3.amazonaws.com" % AWS_STORAGE_BUCKET_NAME 
STATIC_URL = "https://%s/" % AWS_S3_CUSTOM_DOMAIN 
AWS_LOCATION = 'static' 

DEFAULT_FILE_STORAGE = "storages.backends.s3boto.S3BotoStorage" 
STATICFILES_STORAGE = "myapp.storage.s3utils.CachedS3BotoStorage" 

COMPRESS_STORAGE = "myapp.storage.s3utils.CachedS3BotoStorage" 
AWS_IS_GZIPPED = True 
COMPRESS_URL = STATIC_URL 
STATIC_ROOT = "/var/www/<myappname>/static/" 
COMPRESS_ROOT = STATIC_ROOT 

Lagerung/s3utils.py von

from django.core.files.storage import get_storage_class 
from storages.backends.s3boto import S3BotoStorage 

class CachedS3BotoStorage(S3BotoStorage): 
    """ 
    S3 storage backend that saves the files locally, too. 
    """ 
    def __init__(self, *args, **kwargs): 
     super(CachedS3BotoStorage, self).__init__(*args, **kwargs) 
     self.local_storage = get_storage_class(
      "compressor.storage.CompressorFileStorage")() 

    def save(self, name, content): 
     name = super(CachedS3BotoStorage, self).save(name, content) 
     self.local_storage._save(name, content) 
     return name 
+0

Können Sie den Befehl collectstatic mit --traceback ausführen, die ein bisschen mehr Detail darüber, was falsch läuft geben kann. Ich erinnere mich, dass ich mehrere Probleme mit dem Kompressor hatte. Wenn Sie jemals Cloudfront verwenden möchten, beachten Sie, dass die CORS-Einstellungen sehr, sehr schmerzhaft sind, um in AWS richtig zu funktionieren. – Paul

Antwort

3

mit diesen Einstellungen Gelöst:

AWS_ACCESS_KEY_ID = '<KEY_ID>' 
AWS_SECRET_ACCESS_KEY = '<SECRET_KEY>' 
AWS_STORAGE_BUCKET_NAME = "<app_name>" 
AWS_S3_CUSTOM_DOMAIN = "s3.amazonaws.com/%s" % AWS_STORAGE_BUCKET_NAME 

MEDIA_URL = "https://%s/media/" % AWS_S3_CUSTOM_DOMAIN 
STATIC_URL = "https://%s/static/" % AWS_S3_CUSTOM_DOMAIN 
COMPRESS_URL = STATIC_URL 

DEFAULT_FILE_STORAGE = '<app_name>.storage.s3utils.MediaS3BotoStorage' 
STATICFILES_STORAGE = '<app_name>.storage.s3utils.CachedS3BotoStorage' 
COMPRESS_STORAGE = '<app_name>.storage.s3utils.CachedS3BotoStorage' 


MEDIA_ROOT = '<app_name>/media/' 
STATIC_ROOT = '<app_name>/static/' 
COMPRESS_ROOT = STATIC_ROOT 


COMPRESS_ENABLED = True 
COMPRESS_CSS_FILTERS = ['compressor.filters.css_default.CssAbsoluteFilter', 
         'compressor.filters.cssmin.CSSMinFilter' 
         ] 
COMPRESS_PARSER = 'compressor.parser.HtmlParser' 

STATICFILES_FINDERS = (
    'django.contrib.staticfiles.finders.FileSystemFinder', 
    'django.contrib.staticfiles.finders.AppDirectoriesFinder', 
    'compressor.finders.CompressorFinder' 
) 

und meine s3utils.py

class CachedS3BotoStorage(S3BotoStorage): 
    """ 
    S3 storage backend that saves files locally too. 
    """ 
    location = 'static' 
    def __init__(self, *args, **kwargs): 
     super(CachedS3BotoStorage, self).__init__(*args, **kwargs) 
     self.local_storage = get_storage_class(
      "compressor.storage.CompressorFileStorage")() 

    def save(self, name, content): 
     name = super(CachedS3BotoStorage, self).save(name, content) 
     self.local_storage._save(name, content) 
     return name 



class MediaS3BotoStorage(S3BotoStorage): 
    """ S3 storage backend that saves to the 'media' subdirectory""" 
    location = 'media' 
+1

Große Antwort, ich habe wenig unterschiedliche Setup und ich zog mir die Haare aus und was mir geholfen habe, habe ich https: - hardcoded statt // zu meinem Code hinzugefügt und es hat funktioniert, danke die Frage ist hier wenn es jemandem hilft. http://stackoverflow.com/questions/40825990/django-compressor-throws-uncompressablefileerror-with-django-storages-using-amaz/40832184#40832184 – Radek

0

Sieht aus wie jemand das gleiche Problem hier hatte: https://github.com/django-compressor/django-compressor/issues/368#issuecomment-182817810

Try this:

import copy 

def save(self, name, content): 
    content2 = copy.copy(content) 
    name = super(CachedS3BotoStorage, self).save(name, content) 
    self.local_storage._save(name, content2) 
    return name 

Hinweis: Ich verwende Django-Speicher S3BotoStorage & Django-Kompressor zusammen ohne Problem. Ich denke, es ist das Gippen, das Probleme verursacht.