Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

S3Boto3Storage raises a misleading exception on bucket creation #574

Closed
ainterr opened this issue Aug 31, 2018 · 3 comments
Closed

S3Boto3Storage raises a misleading exception on bucket creation #574

ainterr opened this issue Aug 31, 2018 · 3 comments

Comments

@ainterr
Copy link

ainterr commented Aug 31, 2018

S3Boto3Storage assumes any HTTP response besides a 301 or a 404 indicates that the bucket could not be created and AWS_AUTO_CREATE_BUCKET is False and raises an exception saying this. However, if AWS_S3_ENDPOINT_URL is set to point to a custom S3 host that happens to return some other HTTP error code, this exception is misleading.

Example (using Minio S3 instance running at http://localhost:9000 with no buckets initially configured):

settings.py

...
AWS_ACCESS_KEY_ID = '****'
AWS_SECRET_ACCESS_KEY = '****'
AWS_STORAGE_BUCKET_NAME = 'bucket'

AWS_S3_ENDPOINT_URL = 'http://localhost:9000'

AWS_AUTO_CREATE_BUCKET = True
AWS_S3_FILE_OVERWRITE = True
AWS_S3_USE_SSL = False

STATIC_URL = '%s/minio/%s/' % (AWS_S3_ENDPOINT_URL, AWS_STORAGE_BUCKET_NAME)
STATICFILES_STORAGE = 'storages.backends.s3boto3.S3Boto3Storage'

DEFAULT_FILE_STORAGE = 'storages.backends.s3boto3.S3Boto3Storage'
...

models.py

from django.db import models

class Document(models.Model):
    upload = models.FileField(upload_to='documents')

error

(env) ainterr@dev ~/g/test> python manage.py shell
Python 2.7.12 (default, Dec  4 2017, 14:50:18) 
[GCC 5.4.0 20160609] on linux2
Type "help", "copyright", "credits" or "license" for more information.
(InteractiveConsole)
>>> from app import models
>>> from django.core.files import File
>>> d = models.Document()
>>> d.upload.save('test.txt', File(open('test.txt')))
Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "/home/ainterr/git/test/env/local/lib/python2.7/site-packages/django/db/models/fields/files.py", line 95, in save
    self.name = self.storage.save(name, content, max_length=self.field.max_length)
  File "/home/ainterr/git/test/env/local/lib/python2.7/site-packages/django/core/files/storage.py", line 54, in save
    return self._save(name, content)
  File "/home/ainterr/git/test/env/local/lib/python2.7/site-packages/storages/backends/s3boto3.py", line 436, in _save
    obj = self.bucket.Object(encoded_name)
  File "/home/ainterr/git/test/env/local/lib/python2.7/site-packages/storages/backends/s3boto3.py", line 279, in bucket
    self._bucket = self._get_or_create_bucket(self.bucket_name)
  File "/home/ainterr/git/test/env/local/lib/python2.7/site-packages/storages/backends/s3boto3.py", line 353, in _get_or_create_bucket
    "``True``." % name)
ImproperlyConfigured: Bucket bucket does not exist. Buckets can be automatically created by setting AWS_AUTO_CREATE_BUCKET to ``True``.
>>> from django.conf import settings
>>> settings.AWS_AUTO_CREATE_BUCKET
True

further

>>> r.upload.storage.auto_create_bucket
True
>>> s = r.upload.storage
>>> bucket = s.connection.Bucket(s.bucket_name)
>>> bucket.meta.client.head_bucket(Bucket=s.bucket_name)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/ainterr/git/test/env/local/lib/python2.7/site-packages/botocore/client.py", line 314, in _api_call
    return self._make_api_call(operation_name, kwargs)
  File "/home/ainterr/git/test/env/local/lib/python2.7/site-packages/botocore/client.py", line 612, in _make_api_call
    raise error_class(parsed_response, operation_name)
botocore.exceptions.ClientError: An error occurred (502) when calling the HeadBucket operation (reached max retries: 4): Bad Gateway

In this case a bad proxy configuration caused the Minio instance at http://localhost:9000 to return a 502 but I had to dig through the django-storages source code to make sure I wasn't going crazy and actually had set AWS_AUTO_CREATE_BUCKET to True.

@jschneier
Copy link
Owner

jschneier commented Aug 31, 2018 via email

@ainterr
Copy link
Author

ainterr commented Aug 31, 2018

Purely convenience in our case. Though it looks like to me like a 502 from the configured S3 endpoint would raise this exception regardless.

@jschneier
Copy link
Owner

jschneier commented Aug 31, 2018 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants