django-pipeline和s3boto存储似乎不能一起工作

时间:2012-03-07 20:59:56

标签: python django static-content django-pipeline

我正在尝试使用django-pipeline-1.1.27和s3boto来压缩和过滤静态文件,然后将它们上传到s3存储桶。如果我只是使用:

PIPELINE_STORAGE = 'pipeline.storage.PipelineFinderStorage'

然后它工作,我得到一个静态文件夹,其中包含我配置的版本很好的文件。一切换到

PIPELINE_STORAGE = 'storages.backends.s3boto.S3BotoStorage'

我得到了

Traceback (most recent call last):
  File "manage.py", line 15, in <module>
    execute_manager(settings)
  File "/my/virtual/env/lib/python2.7/site-packages/django/core/management/__init__.py", line 438, in execute_manager
    utility.execute()
  File "/my/virtual/env/lib/python2.7/site-packages/django/core/management/__init__.py", line 379, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/my/virtual/env/lib/python2.7/site-packages/django/core/management/base.py", line 191, in run_from_argv
    self.execute(*args, **options.__dict__)
  File "/my/virtual/env/lib/python2.7/site-packages/django/core/management/base.py", line 220, in execute
    output = self.handle(*args, **options)
  File "/my/virtual/env/lib/python2.7/site-packages/pipeline/management/commands/synccompress.py", line 39, in handle
    packager.pack_stylesheets(package, sync=sync, force=force)
  File "/my/virtual/env/lib/python2.7/site-packages/pipeline/packager.py", line 52, in pack_stylesheets
    **kwargs)
  File "/my/virtual/env/lib/python2.7/site-packages/pipeline/packager.py", line 60, in pack
    package['output'], package['paths'])
  File "/my/virtual/env/lib/python2.7/site-packages/pipeline/versioning/__init__.py", line 45, in need_update
    version = self.version(paths)
  File "/my/virtual/env/lib/python2.7/site-packages/pipeline/versioning/__init__.py", line 20, in version
    return getattr(self.versioner, 'version')(paths)
  File "/my/virtual/env/lib/python2.7/site-packages/pipeline/versioning/hash/__init__.py", line 37, in version
    buf = self.concatenate(paths)
  File "/my/virtual/env/lib/python2.7/site-packages/pipeline/versioning/hash/__init__.py", line 27, in concatenate
    return '\n'.join([self.read_file(path) for path in paths])
  File "/my/virtual/env/lib/python2.7/site-packages/pipeline/versioning/hash/__init__.py", line 31, in read_file
    file = storage.open(path, 'rb')
  File "/my/virtual/env/lib/python2.7/site-packages/django/core/files/storage.py", line 33, in open
    file = self._open(name, mode)
  File "/my/virtual/env/lib/python2.7/site-packages/storages/backends/s3boto.py", line 177, in _open
    raise IOError('File does not exist: %s' % name)
IOError: File does not exist: css/style.css

这是我的文件之一。那么,当我切换到s3boto存储时,为什么管道不再需要执行过滤/连接/压缩步骤?

可能是我在做某事。如果它有帮助,这是其他配置:

INSTALLED_APPS = (
    ...
    'pipeline',
    'storages',
)

STATICFILES_FINDERS = (
    'pipeline.finders.PipelineFinder',
    'django.contrib.staticfiles.finders.FileSystemFinder',
    'django.contrib.staticfiles.finders.AppDirectoriesFinder',
)

STATIC_ROOT = "/some/path/outside/django_project/deploy_static"
STATICFILES_DIRS = () # All statics in this site are in apps

STATICFILES_STORAGE = 'pipeline.storage.PipelineStorage'
PIPELINE_STORAGE = 'storages.backends.s3boto.S3BotoStorage'

PIPELINE = True
PIPELINE_AUTO = True
PIPELINE_VERSION = True
PIPELINE_VERSION_PLACEHOLDER = 'VERSION'
PIPELINE_VERSIONING = 'pipeline.versioning.hash.SHA1Versioning'

PIPELINE_CSS = {
    'standard': {
        'source_filenames': (
          'css/style.css',
          ...
        ),
        'output_filename': 'css/all-VERSION.css',
        'extra_context': {
            'media': 'screen,projection',
        },
    }
}

我的网站是在Django 1.3.1上。

我正在运行的命令是:

python manage.py synccompress --force

AWS信誉也在设置中,但这没有实际意义,因为它甚至没有达到这一点。

更新添加了评论中要求的完整堆栈和设置

更新应图书馆作者的要求,我尝试升级到最新测试版。到目前为止的观察结果:

  1. 我现在不知道如何获取版本化的压缩文件
  2. collectstatic给我留下了压缩文件原件
  3. 配置boto存储时仍然从django-pipeline获得相同的错误:它想将我的文件发送到s3,但我甚至看不到它在哪里暂存我的资产。 STATIC_ROOT中没有任何内容。
  4. UPDATE 我创建了最适合查找程序存储的项目,然后使用S3Boto打破。我已将它推送到github,并包含了对堆栈跟踪的捕获。

    https://github.com/estebistec/simple_pipeline https://raw.github.com/estebistec/simple_pipeline/master/STACKTRACE

    如果我被告知我做了一些非常愚蠢的话,我会欣喜若狂。这一切都应该起作用。

5 个答案:

答案 0 :(得分:5)

django-pipeline 1.1.x对于如何使用静态文件有点愚蠢,它更喜欢将所有内容放在一个地方。 我建议您使用最新的django-pipeline 1.2django-staticfiles来尝试django 1.4

使用这样的自定义:

STATICFILES_STORAGE = 'your.app.S3PipelineStorage'

代码如下所示:

from staticfiles.storage import CachedFilesMixin

from pipeline.storage import PipelineMixin

from storages.backends.s3boto import S3BotoStorage


class S3PipelineStorage(PipelineMixin, CachedFilesMixin, S3BotoStorage):
     pass

您可以找到如何修复您的应用程序,但除非您使用版本1.2c1,否则编译文件仍然存在错误:https://gist.github.com/1999564

答案 1 :(得分:2)

我刚刚在使用django-pipeline==1.3.23的Django 1.6项目中遇到了同样的错误,解决方案只是删除了PIPELINE_STORAGE设置。

答案 2 :(得分:2)

类似的错误消息还有另一个问题,它会影响django-pipeline的早期版本和当前版本(1.5.4)。

错误消息为IOError: File does not exist,它出现在s3boto.py.open()packager.pack_stylesheets()中。如果您使用任何编译器(Compass,Sass,Less等),您可能会遇到问题。我怀疑它也会影响JS编译器,但我还没有确认。

简而言之,编译器在本地静态存储中生成输出文件,接下来的步骤compress正试图在s3存储中找到输出。

如果它影响到您,您可能需要查看https://github.com/cyberdelia/django-pipeline/issues/473。有两个拉请求(补丁),一个由skirsdeda制作,另一个由thomasyip(我)制作。两者都可以解决您的问题。如果您希望将已编译的(但是,预压缩的)文件复制到s3并可供应用程序使用,那么您将使用thomasyip(me)的补丁。

以下是该问题的完整回溯:

Traceback (most recent call last):
  File "apps/manage.py", line 16, in <module>
    execute_from_command_line(sys.argv)
  File "/Users/thomas/Dev/Project/main-server/venv/lib/python2.7/site-packages/django/core/management/__init__.py", line 385, in execute_from_command_line
    utility.execute()
  File "/Users/thomas/Dev/Project/main-server/venv/lib/python2.7/site-packages/django/core/management/__init__.py", line 377, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/Users/thomas/Dev/Project/main-server/venv/lib/python2.7/site-packages/django/core/management/base.py", line 288, in run_from_argv
    self.execute(*args, **options.__dict__)
  File "/Users/thomas/Dev/Project/main-server/venv/lib/python2.7/site-packages/django/core/management/base.py", line 338, in execute
    output = self.handle(*args, **options)
  File "/Users/thomas/Dev/Project/main-server/venv/lib/python2.7/site-packages/django/core/management/base.py", line 533, in handle
    return self.handle_noargs(**options)
  File "/Users/thomas/Dev/Project/main-server/venv/lib/python2.7/site-packages/django/contrib/staticfiles/management/commands/collectstatic.py", line 171, in handle_noargs
    collected = self.collect()
  File "/Users/thomas/Dev/Project/main-server/venv/lib/python2.7/site-packages/django/contrib/staticfiles/management/commands/collectstatic.py", line 117, in collect
    for original_path, processed_path, processed in processor:
  File "/Users/thomas/Dev/Project/main-server/venv/lib/python2.7/site-packages/pipeline/storage.py", line 26, in post_process
    packager.pack_stylesheets(package)
  File "/Users/thomas/Dev/Project/main-server/venv/lib/python2.7/site-packages/pipeline/packager.py", line 96, in pack_stylesheets
    variant=package.variant, **kwargs)
  File "/Users/thomas/Dev/Project/main-server/venv/lib/python2.7/site-packages/pipeline/packager.py", line 106, in pack
    content = compress(paths, **kwargs)
  File "/Users/thomas/Dev/Project/main-server/venv/lib/python2.7/site-packages/pipeline/compressors/__init__.py", line 73, in compress_css
    css = self.concatenate_and_rewrite(paths, output_filename, variant)
  File "/Users/thomas/Dev/Project/main-server/venv/lib/python2.7/site-packages/pipeline/compressors/__init__.py", line 137, in concatenate_and_rewrite
    content = self.read_text(path)
  File "/Users/thomas/Dev/Project/main-server/venv/lib/python2.7/site-packages/pipeline/compressors/__init__.py", line 220, in read_text
    content = self.read_bytes(path)
  File "/Users/thomas/Dev/Project/main-server/venv/lib/python2.7/site-packages/pipeline/compressors/__init__.py", line 214, in read_bytes
    file = staticfiles_storage.open(path)
  File "/Users/thomas/Dev/Project/main-server/venv/lib/python2.7/site-packages/django/core/files/storage.py", line 35, in open
    return self._open(name, mode)
  File "/Users/thomas/Dev/Project/main-server/venv/lib/python2.7/site-packages/storages/backends/s3boto.py", line 366, in _open
    raise IOError('File does not exist: %s' % name)
IOError: File does not exist: sheets/sass/sheets.css

答案 3 :(得分:0)

补充答案,压缩时也可以使用GZIP:

from django.contrib.staticfiles.storage import CachedFilesMixin

from pipeline.storage import PipelineMixin

from storages.backends.s3boto import S3BotoStorage


class S3PipelineStorage(PipelineMixin, CachedFilesMixin, S3BotoStorage):
    def __init__(self, *args, **kwargs):
        self.gzip = True
        super(S3PipelineStorage, self).__init__(*args, **kwargs)

使用以下设置:

COMPRESS_STORAGE = STATICFILES_STORAGE = 'my.apps.main.S3PipelineStorage'

答案 4 :(得分:0)

不确定这对其他人的效果如何。我按照上面的解决方案继续得到以下错误:

Traceback (most recent call last):
  File "manage.py", line 24, in <module>
    execute_from_command_line(sys.argv)
  File "python3.4/site-packages/django/core/management/__init__.py", line 338, in execute_from_command_line
    utility.execute()
  File "python3.4/site-packages/django/core/management/__init__.py", line 330, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "python3.4/site-packages/django/core/management/base.py", line 390, in run_from_argv
    self.execute(*args, **cmd_options)
  File "python3.4/site-packages/django/core/management/base.py", line 441, in execute
    output = self.handle(*args, **options)
  File "python3.4/site-packages/django/contrib/staticfiles/management/commands/collectstatic.py", line 168, in handle
    collected = self.collect()
  File "python3.4/site-packages/django/contrib/staticfiles/management/commands/collectstatic.py", line 114, in collect
    for original_path, processed_path, processed in processor:
  File "python3.4/site-packages/pipeline/storage.py", line 26, in post_process
    packager.pack_stylesheets(package)
  File "python3.4/site-packages/pipeline/packager.py", line 96, in pack_stylesheets
    variant=package.variant, **kwargs)
  File "python3.4/site-packages/pipeline/packager.py", line 105, in pack
    paths = self.compile(package.paths, force=True)
  File "python3.4/site-packages/pipeline/packager.py", line 99, in compile
    return self.compiler.compile(paths, force=force)
  File "python3.4/site-packages/pipeline/compilers/__init__.py", line 56, in compile
    return list(executor.map(_compile, paths))
  File "/usr/local/lib/python3.4/concurrent/futures/_base.py", line 549, in result_iterator
    yield future.result()
  File "/usr/local/lib/python3.4/concurrent/futures/_base.py", line 402, in result
    return self.__get_result()
  File "/usr/local/lib/python3.4/concurrent/futures/_base.py", line 354, in __get_result
    raise self._exception
  File "/usr/local/lib/python3.4/concurrent/futures/thread.py", line 54, in run
    result = self.fn(*self.args, **self.kwargs)
  File "python3.4/site-packages/pipeline/compilers/__init__.py", line 42, in _compile
    outdated = compiler.is_outdated(input_path, output_path)
  File "python3.4/site-packages/pipeline/compilers/__init__.py", line 85, in is_outdated
    return self.storage.modified_time(infile) > self.storage.modified_time(outfile)
  File "python3.4/site-packages/storages/backends/s3boto.py", line 480, in modified_time
    return parse_ts(entry.last_modified)
AttributeError: 'NoneType' object has no attribute 'last_modified'

直到我遇到this solution我才开始发现对我有用的东西。这是我最终使用的存储本地以及S3中保存文件的存储,这让我传递了所有错误:

from django.contrib.staticfiles.storage import ManifestFilesMixin
from django.core.files.storage import get_storage_class
from pipeline.storage import PipelineMixin
from storages.backends.s3boto import S3BotoStorage


class StaticStorage(PipelineMixin, ManifestFilesMixin, S3BotoStorage):
    """Custom storage for static content."""

    def __init__(self, *args, **kwargs):
        super(StaticStorage, self).__init__(*args, **kwargs)
        self.local_storage = get_storage_class(
            'django.contrib.staticfiles.storage.StaticFilesStorage')()

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