作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个允许用户上传文件的基本 Web 应用程序。不涉及处理,我只需要将它们存储在特定的 Amazon S3 存储桶中。我在 AWS Lambda 上使用 Zappa 部署了应用程序。文档上传功能在本地测试服务器上完美运行,但我不确定如何使其在实际部署的实例上运行。如果我直接尝试通过管理面板添加文件,我会得到同样的错误。有谁知道我做错了什么或如何解决这个问题?我将在此处包括追溯:
File "/var/task/django/core/handlers/exception.py" in inner
34. response = get_response(request)
File "/var/task/django/core/handlers/base.py" in _get_response
115. response = self.process_exception_by_middleware(e, request)
File "/var/task/django/core/handlers/base.py" in _get_response
113. response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/var/task/django/views/generic/base.py" in view
71. return self.dispatch(request, *args, **kwargs)
File "/var/task/django/contrib/auth/mixins.py" in dispatch
52. return super().dispatch(request, *args, **kwargs)
File "/var/task/django/contrib/auth/mixins.py" in dispatch
85. return super().dispatch(request, *args, **kwargs)
File "/var/task/django/views/generic/base.py" in dispatch
97. return handler(request, *args, **kwargs)
File "/var/task/django/views/generic/edit.py" in post
172. return super().post(request, *args, **kwargs)
File "/var/task/django/views/generic/edit.py" in post
142. return self.form_valid(form)
File "/var/task/myapp/views.py" in form_valid
83. return super(UploadView, self).form_valid(form)
File "/var/task/django/views/generic/edit.py" in form_valid
125. self.object = form.save()
File "/var/task/django/forms/models.py" in save
458. self.instance.save()
File "/var/task/django/db/models/base.py" in save
741. force_update=force_update, update_fields=update_fields)
File "/var/task/django/db/models/base.py" in save_base
779. force_update, using, update_fields,
File "/var/task/django/db/models/base.py" in _save_table
870. result = self._do_insert(cls._base_manager, using, fields, update_pk, raw)
File "/var/task/django/db/models/base.py" in _do_insert
908. using=using, raw=raw)
File "/var/task/django/db/models/manager.py" in manager_method
82. return getattr(self.get_queryset(), name)(*args, **kwargs)
File "/var/task/django/db/models/query.py" in _insert
1186. return query.get_compiler(using=using).execute_sql(return_id)
File "/var/task/django/db/models/sql/compiler.py" in execute_sql
1334. for sql, params in self.as_sql():
File "/var/task/django/db/models/sql/compiler.py" in as_sql
1278. for obj in self.query.objs
File "/var/task/django/db/models/sql/compiler.py" in <listcomp>
1278. for obj in self.query.objs
File "/var/task/django/db/models/sql/compiler.py" in <listcomp>
1277. [self.prepare_value(field, self.pre_save_val(field, obj)) for field in fields]
File "/var/task/django/db/models/sql/compiler.py" in pre_save_val
1228. return field.pre_save(obj, add=True)
File "/var/task/django/db/models/fields/files.py" in pre_save
288. file.save(file.name, file.file, save=False)
File "/var/task/django/db/models/fields/files.py" in save
87. self.name = self.storage.save(name, content, max_length=self.field.max_length)
File "/var/task/django/core/files/storage.py" in save
51. name = self.get_available_name(name, max_length=max_length)
File "/var/task/storages/backends/s3boto.py" in get_available_name
525. return super(S3BotoStorage, self).get_available_name(name, max_length)
File "/var/task/django/core/files/storage.py" in get_available_name
75. while self.exists(name) or (max_length and len(name) > max_length):
File "/var/task/storages/backends/s3boto.py" in exists
464. return self._get_key(name) is not None
File "/var/task/storages/backends/s3boto.py" in _get_key
450. return self.bucket.get_key(self._encode_name(name))
File "/var/task/boto/s3/bucket.py" in get_key
193. key, resp = self._get_key_internal(key_name, headers, query_args_l)
File "/var/task/boto/s3/bucket.py" in _get_key_internal
232. response.status, response.reason, '')
Exception Type: S3ResponseError at /myapp/uploadfile
Exception Value: S3ResponseError: 400 Bad Request
我尝试了各种不同的设置配置,但没有找到解决方案。当我尝试在 Lambda 部署上上传文件时,我经常收到“S3ResponseError:400 错误请求”。我已经在我的 VPC 中添加了一个 S3 端点,因为我认为它可能与访问互联网有关,但我没有收到任何超时错误,如果那是问题的话,我会预料到这一点。我还在项目的主目录(我的 zappa_settings.json 和 manage.py 所在的位置)中创建了一个 .boto 文件,内容如下:
[Credentials]
aws_access_key_id=***
aws_secret_access_key=****
[s3]
host=s3.us-west-2.amazonaws.com
我的设置.py
AWS_S3_HOST = 's3.us-west-2.amazonaws.com'
AWS_S3_ACCESS_KEY_ID = '***'
AWS_S3_SECRET_ACCESS_KEY = '****'
AWS_STORAGE_BUCKET_NAME = 'bucketname'
AWS_QUERYSTRING_AUTH = False
AWS_HEADERS = {'Cache-Control': 'max-age = 86400',}
AWS_STORAGE_REGION = 'us-west-2'
AWS_S3_FILE_OVERWRITE = False
DEFAULT_FILE_STORAGE = "storages.backends.s3boto.S3BotoStorage"
MEDIA_URL = 'http://bucketname.s3.amazonaws.com/'
我的文件上传表单
class CSVUpload(forms.ModelForm):
class Meta:
model = UploadedDocument
fields = ['uploadfile', ]
labels = {
'uploadfile': 'Attach CSV or Excel file below (.csv or .xlsx): '
}
我为模型创建 View
class UploadView(LoginRequiredMixin, PermissionRequiredMixin, CreateView):
permission_required = 'UploadedDocument.can_add'
form_class = CSVUpload
template_name = 'document_upload.html'
login_url = 'login'
success_url = 'uploadfile/success'
def form_valid(self, form):
form.instance.uploader = self.request.user
return super(UploadView, self).form_valid(form)
我的 UploadedDocument 模型
class UploadedDocument(models.Model):
"""A model representing an uploaded document"""
id = models.AutoField(primary_key=True)
uploaddate = models.DateTimeField(auto_now_add=True)
uploadfile = models.FileField(upload_to='files/', null=True, verbose_name="")
uploader = models.ForeignKey('users.CustomUser', on_delete=models.PROTECT, null=True)
def __str__(self):
return str(self.id)
带有表单的 HTML 页面
{% extends 'base.html' %}
{% load crispy_forms_tags %}
{% block title %}CSV upload{% endblock title %}
{% load staticfiles %}
{% block content %}
<div class="jumbotron">
<h1>Upload CSV file</h1>
<form enctype="multipart/form-data" method="post">
{% csrf_token %}
{{ form|crispy }}
<button class="btn btn-success" type="submit">Upload</button>
</form>
</div>
{% endblock content %}
最佳答案
尝试在您的 .boto 文件中添加一行:
[s3]
host=s3.us-west-2.amazonaws.com
use-sigv4 = True
如果您随后收到错误消息,告诉您使用 SigV4 时,您必须指定“主机”参数
,这将有所帮助:S3 using boto and SigV4 - missing host parameter
关于python - 如何使用 Zappa 从 Django lambda 部署将文件上传到 S3 存储桶?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56552745/
我是一名优秀的程序员,十分优秀!