我一直在努力尝试从Django工作上传媒体文件上传到Amazon S3,如果有人能提供帮助,我会非常感激。
我的应用程序使用Sqlite3数据库以及Postgres在本地上传图像文件,但是当我尝试将我的媒体文件上传到S3时,我收到以下500错误:
cannot concatenate 'str' and 'NoneType' objects
11月29日更新
似乎boto找不到我的桶名,即使它是在我的设置中设置的。上面的NoneType'
来自以下内容,来自connection.py
和bucket.py
:
bucket: None
我已经安装了django-storage和boto,如下文所述:
我测试了手动在S3上将我的媒体文件上传到我的存储桶,我在本地创建的数据库实例链接到我的存储桶中的正确媒体文件。从我的角度来看,当我的django视图尝试通过boto(DEFAULT_FILE_STORAGE
中的local_settings.py
)将图像上传到S3时,会出现问题。
以下内容属于local_settings.py
:
INSTALLED_APPS = (
...
'storages',
)
# Local postgres database
DATABASES = {
"default": {
"ENGINE": "django.db.backends.postgresql_psycopg2",
"NAME": "name",
"USER": "username",
"PASSWORD": "pw",
"PORT": "5432",
}
}
STATICFILES_STORAGE = 'whitenoise.django.GzipManifestStaticFilesStorage'
AWS_QUERYSTRING_AUTH = False
AWS_ACCESS_KEY_ID = 'my-key'
AWS_SECRET_ACCESS_KEY = 'my-key2'
AWS_STORAGE_BUCKET_NAME = 'my-bucket-name'
MEDIAFILES_LOCATION = 'media'
MEDIA_URL = 'http://%s.s3.amazonaws.com/media/' % AWS_STORAGE_BUCKET_NAME
DEFAULT_FILE_STORAGE = "storages.backends.s3boto.S3BotoStorage"
在我views.py
的上传表单中,我使用了类似的内容:
photo = form.cleaned_data['photo']
...
recipe = Recipe(title=title, ..., photo=photo)
recipe.save()
我还尝试添加一个s3utils.py文件,如2中所述。如上文所述,添加了桶策略和CORS。我授予"每个人"访问上传/删除。
我的requirements.txt
包含:
Django==1.7.8
Pillow==2.6.1
awscli==1.7.36
boto==2.38.0
botocore==1.0.1
django-storages==1.1.8
whitenoise==1.0.6
...
我将Heroku用于我的应用程序,并且需要存储和托管来自S3的媒体文件。
有人看到我错过的东西吗?如果您需要更多详细信息,请与我们联系。
提前多多感谢!
回溯
Environment:
Request Method: POST
Request URL: http://127.0.0.1:5000/add/
Django Version: 1.7.8
Python Version: 2.7.10
Installed Applications:
('django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'django.contrib.admindocs',
'matboken_app',
'crispy_forms',
'PIL',
'django_summernote',
'djangoratings',
'imagekit',
'storages',
's3direct')
Installed Middleware:
('django.middleware.common.CommonMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.middleware.locale.LocaleMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware')
Traceback:
File "/Users/Kenneth/Dropbox/matboken/env_1.8/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response
111. response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/Users/Kenneth/Dropbox/matboken/env_1.8/lib/python2.7/site-packages/django/contrib/auth/decorators.py" in _wrapped_view
21. return view_func(request, *args, **kwargs)
File "/Users/Kenneth/Dropbox/matboken/matboken_app/views.py" in addRecipe
678. recipe.save()
File "/Users/Kenneth/Dropbox/matboken/env_1.8/lib/python2.7/site-packages/django/db/models/base.py" in save
589. force_update=force_update, update_fields=update_fields)
File "/Users/Kenneth/Dropbox/matboken/env_1.8/lib/python2.7/site-packages/django/db/models/base.py" in save_base
617. updated = self._save_table(raw, cls, force_insert, force_update, using, update_fields)
File "/Users/Kenneth/Dropbox/matboken/env_1.8/lib/python2.7/site-packages/django/db/models/base.py" in _save_table
698. result = self._do_insert(cls._base_manager, using, fields, update_pk, raw)
File "/Users/Kenneth/Dropbox/matboken/env_1.8/lib/python2.7/site-packages/django/db/models/base.py" in _do_insert
731. using=using, raw=raw)
File "/Users/Kenneth/Dropbox/matboken/env_1.8/lib/python2.7/site-packages/django/db/models/manager.py" in manager_method
92. return getattr(self.get_queryset(), name)(*args, **kwargs)
File "/Users/Kenneth/Dropbox/matboken/env_1.8/lib/python2.7/site-packages/django/db/models/query.py" in _insert
921. return query.get_compiler(using=using).execute_sql(return_id)
File "/Users/Kenneth/Dropbox/matboken/env_1.8/lib/python2.7/site-packages/django/db/models/sql/compiler.py" in execute_sql
920. for sql, params in self.as_sql():
File "/Users/Kenneth/Dropbox/matboken/env_1.8/lib/python2.7/site-packages/django/db/models/sql/compiler.py" in as_sql
878. for obj in self.query.objs
File "/Users/Kenneth/Dropbox/matboken/env_1.8/lib/python2.7/site-packages/django/db/models/fields/files.py" in pre_save
301. file.save(file.name, file, save=False)
File "/Users/Kenneth/Dropbox/matboken/env_1.8/lib/python2.7/site-packages/django/db/models/fields/files.py" in save
89. self.name = self.storage.save(name, content)
File "/Users/Kenneth/Dropbox/matboken/env_1.8/lib/python2.7/site-packages/django/core/files/storage.py" in save
51. name = self._save(name, content)
File "/Users/Kenneth/Dropbox/matboken/env_1.8/lib/python2.7/site-packages/storages/backends/s3boto.py" in _save
385. key = self.bucket.get_key(encoded_name)
File "/Users/Kenneth/Dropbox/matboken/env_1.8/lib/python2.7/site-packages/boto/s3/bucket.py" in get_key
192. key, resp = self._get_key_internal(key_name, headers, query_args_l)
File "/Users/Kenneth/Dropbox/matboken/env_1.8/lib/python2.7/site-packages/boto/s3/bucket.py" in _get_key_internal
199. query_args=query_args)
File "/Users/Kenneth/Dropbox/matboken/env_1.8/lib/python2.7/site-packages/boto/s3/connection.py" in make_request
652. auth_path = self.calling_format.build_auth_path(bucket, key)
File "/Users/Kenneth/Dropbox/matboken/env_1.8/lib/python2.7/site-packages/boto/s3/connection.py" in build_auth_path
94. path = '/' + bucket
Exception Type: TypeError at /add/
Exception Value: cannot concatenate 'str' and 'NoneType' objects