Django S3图片上传错误“ boto.exception.S3ResponseError:S3ResponseError:400错误的请求”

时间:2019-02-21 08:08:37

标签: python django amazon-web-services amazon-s3 django-rest-framework

我正在尝试使用Django Rest Framework和AWS S3上传图像。

settings.py:

AWS_ACCESS_KEY_ID = ... 
AWS_SECRET_ACCESS_KEY = ... 
AWS_STORAGE_BUCKET_NAME = ... 
AWS_S3_CUSTOM_DOMAIN = '%s.s3.amazonaws.com' % AWS_STORAGE_BUCKET_NAME AWS_S3_OBJECT_PARAMETERS = {
        'CacheControl': 'max-age=86400', } 
AWS_LOCATION = 'static'

STATICFILES_DIRS = [
        os.path.join(BASE_DIR, 'backend/static'), ] STATIC_URL = 'https://%s/%s/' % (AWS_S3_CUSTOM_DOMAIN, AWS_LOCATION) STATICFILES_STORAGE = 'storages.backends.s3boto3.S3Boto3Storage'  DEFAULT_FILE_STORAGE = 'storages.backends.s3boto.S3BotoStorage' 

AWS_DEFAULT_ACL = None

models.py:

class Beat(models.Model):
    ...
    image = models.ImageField(upload_to='static/', default = 'static/None/No-img.gif', null = True) 

S3存储桶策略:

{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Sid": "Allow All",
            "Effect": "Allow",
            "Principal": "*",
            "Action": [
                "s3:GetObject",
                "s3:PutObject",
                "s3:PutObjectAcl"
            ],
            "Resource": "arn:aws:s3:::my_bucket/*"
        }
    ]
}

但是,当我上传图片时,会出现错误:

  

boto.exception.S3ResponseError:S3ResponseError:400错误的请求

我不知道是什么问题。

python manage.py collectstatic效果很好。

如何解决此错误?

谢谢。

以下错误消息:

Internal Server Error: /api/beats/create/
Traceback (most recent call last):
  File "/home/seokchan/server/mdocker/lib/python3.5/site-packages/django/core/handlers/exception.py", line 34, in inner
    response = get_response(request)
  File "/home/seokchan/server/mdocker/lib/python3.5/site-packages/django/core/handlers/base.py", line 126, in _get_response
    response = self.process_exception_by_middleware(e, request)
  File "/home/seokchan/server/mdocker/lib/python3.5/site-packages/django/core/handlers/base.py", line 124, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "/home/seokchan/server/mdocker/lib/python3.5/site-packages/django/views/decorators/csrf.py", line 54, in wrapped_view
    return view_func(*args, **kwargs)
  File "/home/seokchan/server/mdocker/lib/python3.5/site-packages/django/views/generic/base.py", line 68, in view
    return self.dispatch(request, *args, **kwargs)
  File "/home/seokchan/server/mdocker/lib/python3.5/site-packages/rest_framework/views.py", line 495, in dispatch
    response = self.handle_exception(exc)
  File "/home/seokchan/server/mdocker/lib/python3.5/site-packages/rest_framework/views.py", line 455, in handle_exception
    self.raise_uncaught_exception(exc)
  File "/home/seokchan/server/mdocker/lib/python3.5/site-packages/rest_framework/views.py", line 492, in dispatch
    response = handler(request, *args, **kwargs)
  File "/home/seokchan/server/mdocker/mkdir2/mkdir/backend/beats/views.py", line 114, in post
    beat = serializer.save(owner = mowner)
  File "/home/seokchan/server/mdocker/lib/python3.5/site-packages/rest_framework/serializers.py", line 214, in save
    self.instance = self.create(validated_data)
  File "/home/seokchan/server/mdocker/lib/python3.5/site-packages/rest_framework/serializers.py", line 940, in create
    instance = ModelClass._default_manager.create(**validated_data)
  File "/home/seokchan/server/mdocker/lib/python3.5/site-packages/django/db/models/manager.py", line 82, in manager_method
    return getattr(self.get_queryset(), name)(*args, **kwargs)
  File "/home/seokchan/server/mdocker/lib/python3.5/site-packages/django/db/models/query.py", line 413, in create
    obj.save(force_insert=True, using=self.db)
  File "/home/seokchan/server/mdocker/lib/python3.5/site-packages/django/db/models/base.py", line 718, in save
    force_update=force_update, update_fields=update_fields)
  File "/home/seokchan/server/mdocker/lib/python3.5/site-packages/django/db/models/base.py", line 748, in save_base
    updated = self._save_table(raw, cls, force_insert, force_update, using, update_fields)
  File "/home/seokchan/server/mdocker/lib/python3.5/site-packages/django/db/models/base.py", line 831, in _save_table
    result = self._do_insert(cls._base_manager, using, fields, update_pk, raw)
  File "/home/seokchan/server/mdocker/lib/python3.5/site-packages/django/db/models/base.py", line 869, in _do_insert
    using=using, raw=raw)
  File "/home/seokchan/server/mdocker/lib/python3.5/site-packages/django/db/models/manager.py", line 82, in manager_method
    return getattr(self.get_queryset(), name)(*args, **kwargs)
  File "/home/seokchan/server/mdocker/lib/python3.5/site-packages/django/db/models/query.py", line 1136, in _insert
    return query.get_compiler(using=using).execute_sql(return_id)
  File "/home/seokchan/server/mdocker/lib/python3.5/site-packages/django/db/models/sql/compiler.py", line 1288, in execute_sql
    for sql, params in self.as_sql():
  File "/home/seokchan/server/mdocker/lib/python3.5/site-packages/django/db/models/sql/compiler.py", line 1241, in as_sql
    for obj in self.query.objs
  File "/home/seokchan/server/mdocker/lib/python3.5/site-packages/django/db/models/sql/compiler.py", line 1241, in <listcomp>
    for obj in self.query.objs
  File "/home/seokchan/server/mdocker/lib/python3.5/site-packages/django/db/models/sql/compiler.py", line 1240, in <listcomp>
    [self.prepare_value(field, self.pre_save_val(field, obj)) for field in fields]
  File "/home/seokchan/server/mdocker/lib/python3.5/site-packages/django/db/models/sql/compiler.py", line 1192, in pre_save_val
    return field.pre_save(obj, add=True)
  File "/home/seokchan/server/mdocker/lib/python3.5/site-packages/django/db/models/fields/files.py", line 288, in pre_save
    file.save(file.name, file.file, save=False)
  File "/home/seokchan/server/mdocker/lib/python3.5/site-packages/django/db/models/fields/files.py", line 87, in save
    self.name = self.storage.save(name, content, max_length=self.field.max_length)
  File "/home/seokchan/server/mdocker/lib/python3.5/site-packages/django/core/files/storage.py", line 49, in save
    return self._save(name, content)
  File "/home/seokchan/server/mdocker/lib/python3.5/site-packages/storages/backends/s3boto.py", line 425, in _save
    key = self.bucket.get_key(encoded_name)
  File "/home/seokchan/server/mdocker/lib/python3.5/site-packages/boto/s3/bucket.py", line 193, in get_key
    key, resp = self._get_key_internal(key_name, headers, query_args_l)
  File "/home/seokchan/server/mdocker/lib/python3.5/site-packages/boto/s3/bucket.py", line 232, in _get_key_internal
    response.status, response.reason, '')
boto.exception.S3ResponseError: S3ResponseError: 400 Bad Request

1 个答案:

答案 0 :(得分:1)

settings.py需要以下几行:

AWS_REGION = 'ap-northeast-2'
AWS_S3_HOST = 's3.%s.amazonaws.com' % AWS_REGION