简体   繁体   English

使用django-storages / boto在本地收集静态作品,但不在Heroku上收集

[英]Collect static works locally but no on Heroku using django-storages/boto

When I run collectstatic on Heroku I get the error below because I believe collect static is trying to write to a local file instead of using S3. 当我在Heroku上运行collectstatic时,出现以下错误,因为我相信collect static试图写入本地文件而不是使用S3。

When I run collectstatic locally with the same AWS credentials my static files are properly transferred to my S3 bucket. 当我使用相同的AWS凭证在本地运行collectstatic时,我的静态文件已正确传输到我的S3存储桶。

Another oddity is that my media upload on Heroku get saved in S3 properly. 另一个奇怪的是,我在Heroku上上传的媒体已正确保存在S3中。 So I know my Django app is connecting to S3 just fine. 所以我知道我的Django应用可以很好地连接到S3。

I've gone into the django shell and I've imported DEFAULT_FILE_STORAGE & STATICFILES_STORAGE and verified they point to storages.backends.s3boto.S3BotoStorage . 我已经去到Django的外壳和我已经导入DEFAULT_FILE_STORAGESTATICFILES_STORAGE并验证它们指向storages.backends.s3boto.S3BotoStorage

I've also been able to use boto to view the files in my bucket, 我还可以使用Boto查看存储桶中的文件,

from storages.backends import s3boto
h = s3boto.S3BotoStorage()
h.bucket  # Shows me my bucket name

Through django's interactive shell I can list the files in my bucket and other things. 通过django的交互式外壳,我可以列出存储桶和其他内容中的文件。

I can't figure out why running collectstatic locally works but when I run on heroku, using the same credentials, app, environment, etc doesn't work. 我无法弄清楚为什么在本地运行collectstatic可以工作,但是当我在heroku上运行时,使用相同的凭据,应用程序,环境等无法正常工作。 But my media uploads to S3 do work on heroku...? 但是我上传到S3的媒体可以在heroku上工作吗...?

This the error I get running collectstatic on heroku, 这是我在heroku上运行collectstatic的错误,

Running `python manage.py collectstatic --noinput` attached to terminal... up, run.2485
/app/.heroku/python/lib/python3.3/site-packages/dotenv.py:53: UserWarning: Not reading .env - it doesn't exist.
  warnings.warn("Not reading {0} - it doesn't exist.".format(dotenv))
Traceback (most recent call last):
  File "manage.py", line 13, in <module>
    execute_from_command_line(sys.argv)
  File "/app/.heroku/python/lib/python3.3/site-packages/django/core/management/__init__.py", line 385, in execute_from_command_line
    utility.execute()
  File "/app/.heroku/python/lib/python3.3/site-packages/django/core/management/__init__.py", line 377, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/app/.heroku/python/lib/python3.3/site-packages/django/core/management/base.py", line 288, in run_from_argv
    self.execute(*args, **options.__dict__)
  File "/app/.heroku/python/lib/python3.3/site-packages/django/core/management/base.py", line 338, in execute
    output = self.handle(*args, **options)
  File "/app/.heroku/python/lib/python3.3/site-packages/django/core/management/base.py", line 533, in handle
    return self.handle_noargs(**options)
  File "/app/.heroku/python/lib/python3.3/site-packages/django/contrib/staticfiles/management/commands/collectstatic.py", line 168, in handle_noargs
    collected = self.collect()
  File "/app/.heroku/python/lib/python3.3/site-packages/django/contrib/staticfiles/management/commands/collectstatic.py", line 98, in collect
    for path, storage in finder.list(self.ignore_patterns):
  File "/app/.heroku/python/lib/python3.3/site-packages/django/contrib/staticfiles/finders.py", line 111, in list
    for path in utils.get_files(storage, ignore_patterns):
  File "/app/.heroku/python/lib/python3.3/site-packages/django/contrib/staticfiles/utils.py", line 27, in get_files
    directories, files = storage.listdir(location)
  File "/app/.heroku/python/lib/python3.3/site-packages/django/core/files/storage.py", line 270, in listdir
    for entry in os.listdir(path):
FileNotFoundError: [Errno 2] No such file or directory: '/app/static'

Every time I push to heroku I see, 每当我推到heroku时,我都会看到

remote: -----> Preparing static assets
remote:        Collectstatic configuration error. To debug, run:
remote:        $ heroku run python ./manage.py collectstatic --noinput
remote:

I found my error, it was stupid. 我发现我的错误,这很愚蠢。 It turns out my static files directory was being ignored in both my gitignore and my slugignore file. 事实证明,我的gitignore和slugignore文件都忽略了我的静态文件目录。

Lesson learned, don't blindly copy other's gitignore and slugignore files... 经验教训,不要盲目复制他人的gitignore和slugignore文件...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM