问题描述
这个问题使用Heroku的,Django的存储器(W /博托为S3)和CloudFront的服务于静态内容涉及到一个设置。
this question pertains to a setup using Heroku, django-storages (w/ boto for s3), and CloudFront to serve static content.
我一直试图在过去的几个小时,我的静态文件成功加载到我的Heroku应用程序。我已经成功地得到的Cloudfront勾住我的S3存储,而且好像桶设置正确,但因为种种原因,我对 AWS_STORAGE_BUCKET_NAME
似乎并没有登记值正常。
I've been trying for the past several hours to successfully load up my static files to my Heroku app. I have successfully got Cloudfront hooked to my s3 bucket, and it seems as though the bucket is set up properly, but for whatever reason, my value for AWS_STORAGE_BUCKET_NAME
does not seem to registering properly.
如果任何人有关于如何调试这任何线索或想法,我会非常感激。我在我束手无策。感谢您的阅读。
If anyone has any clues or ideas on how to debug this, I'd be much obliged. I am at my wit's end. Thanks for reading.
settings.py(重要的东西):
settings.py (the important stuff):
try:
from settings_local import *
except:
import s3utils
DEBUG = False
#s3 stuff
DEFAULT_FILE_STORAGE = 's3utils.MediaRootS3BotoStorage'
STATICFILES_STORAGE = 's3utils.StaticRootS3BotoStorage'
STATIC_URL = 'https://[domain].cloudfront.net/'
#use heroku postgres database
import dj_database_url
DATABASES['default'] = dj_database_url.config()
s3utils.py
s3utils.py
from storages.backends.s3boto import S3BotoStorage
from django.utils.functional import SimpleLazyObject
import os
AWS_ACCESS_KEY_ID = os.environ['AWS_ACCESS_KEY_ID']
AWS_SECRET_ACCESS_KEY = os.environ['AWS_SECRET_ACCESS_KEY']
AWS_STORAGE_BUCKET_NAME = 'static.[website].org'
StaticRootS3BotoStorage = lambda: S3BotoStorage(location='static')
MediaRootS3BotoStorage = lambda: S3BotoStorage(location='media')
这是我试图collectstatic时,你得到的回溯,可以通过Heroku上运行或Procfile:
This is the traceback I get when attempting to collectstatic, either via 'heroku run' or in the Procfile:
Traceback (most recent call last):
File "manage.py", line 10, in <module>
execute_from_command_line(sys.argv)
File "/app/.heroku/python/lib/python2.7/site-packages/django/core/management/__init__.py", line 443, in execute_from_command_line
utility.execute()
File "/app/.heroku/python/lib/python2.7/site-packages/django/core/management/__init__.py", line 382, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/app/.heroku/python/lib/python2.7/site-packages/django/core/management/base.py", line 196, in run_from_argv
self.execute(*args, **options.__dict__)
File "/app/.heroku/python/lib/python2.7/site-packages/django/core/management/base.py", line 232, in execute
output = self.handle(*args, **options)
File "/app/.heroku/python/lib/python2.7/site-packages/django/core/management/base.py", line 371, in handle
return self.handle_noargs(**options)
File "/app/.heroku/python/lib/python2.7/site-packages/django/contrib/staticfiles/management/commands/collectstatic.py", line 163, in handle_noargs
collected = self.collect()
File "/app/.heroku/python/lib/python2.7/site-packages/django/contrib/staticfiles/management/commands/collectstatic.py", line 113, in collect
handler(path, prefixed_path, storage)
File "/app/.heroku/python/lib/python2.7/site-packages/django/contrib/staticfiles/management/commands/collectstatic.py", line 287, in copy_file
if not self.delete_file(path, prefixed_path, source_storage):
File "/app/.heroku/python/lib/python2.7/site-packages/django/contrib/staticfiles/management/commands/collectstatic.py", line 215, in delete_file
if self.storage.exists(prefixed_path):
File "/app/.heroku/python/lib/python2.7/site-packages/storages/backends/s3boto.py", line 284, in exists
return k.exists()
File "/app/.heroku/python/lib/python2.7/site-packages/boto/s3/key.py", line 399, in exists
return bool(self.bucket.lookup(self.name))
File "/app/.heroku/python/lib/python2.7/site-packages/boto/s3/bucket.py", line 148, in lookup
return self.get_key(key_name, headers=headers)
File "/app/.heroku/python/lib/python2.7/site-packages/boto/s3/bucket.py", line 181, in get_key
query_args=query_args)
File "/app/.heroku/python/lib/python2.7/site-packages/boto/s3/connection.py", line 458, in make_request
auth_path = self.calling_format.build_auth_path(bucket, key)
File "/app/.heroku/python/lib/python2.7/site-packages/boto/s3/connection.py", line 92, in build_auth_path
path = '/' + bucket
TypeError: cannot concatenate 'str' and 'NoneType' objects
请注意,我省略了域名和这样的,我实际上没有[域]或[公司网址]在code。
Note that I've omitted domain names and such, I don't actually have [domain] or [website] in the code.
推荐答案
您不是说从s3utils导入*
,所以 AWS_STORAGE_BUCKET_NAME
从未导入设置模块。
You are not saying from s3utils import *
, so AWS_STORAGE_BUCKET_NAME
is never imported into the settings module.
S3BotoStorage
将拉动 AWS_ACCESS_KEY_ID
和 AWS_SECRET_ACCESS_KEY
从环境变量,但 AWS_STORAGE_BUCKET_NAME
必须在 settings.py
设置。这似乎是一个奇怪的不一致之处,但我认为这是因为 AWS_ACCESS_KEY_ID
和 AWS_SECRET_ACCESS_KEY
实际上博托
参数和 AWS_STORAGE_BUCKET_NAME
没有(博托
将拉动从ENV凭据瓦尔)。*
S3BotoStorage
will pull AWS_ACCESS_KEY_ID
and AWS_SECRET_ACCESS_KEY
from environment variables, but AWS_STORAGE_BUCKET_NAME
must be set in settings.py
. This seems like a bizarre inconsistency, but I think it is because AWS_ACCESS_KEY_ID
and AWS_SECRET_ACCESS_KEY
are actually boto
parameters and AWS_STORAGE_BUCKET_NAME
is not (boto
will pull credentials from env vars).*
另外提及s3utils是明确的:
The other references to s3utils are explicit:
DEFAULT_FILE_STORAGE = 's3utils.MediaRootS3BotoStorage'
STATICFILES_STORAGE = 's3utils.StaticRootS3BotoStorage'
所以,这是不照顾的唯一设置是 AWS_STORAGE_BUCKET_NAME
,并导致你的错误。
So, the only setting that isn't taken care of is AWS_STORAGE_BUCKET_NAME
, and that causes your error.
*我想看看Django的储藏接受其他设置的ENV瓦尔( 12因子应用程序,任何人吗?)而正在考虑开设一个问题/提交拉请求的效果。
*I'd like to see django-storages accept other settings from env vars (12-Factor App, anyone?) and am considering opening an issue/submitting a pull request to that effect.
这篇关于无法collectstatic通过Heroku的使用博托到S3 - S3存储返回NoneType的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!