Skip to content

Commit

Permalink
Fixes #112 - Check if bucket exists for path=''
Browse files Browse the repository at this point in the history
  • Loading branch information
jschneier committed Jan 26, 2016
1 parent 5e14712 commit b95ec91
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 0 deletions.
7 changes: 7 additions & 0 deletions storages/backends/s3boto.py
Original file line number Diff line number Diff line change
Expand Up @@ -428,6 +428,13 @@ def delete(self, name):
self.bucket.delete_key(self._encode_name(name))

def exists(self, name):
if not name: # root element aka the bucket
try:
self.bucket
return True
except ImproperlyConfigured:
return False

name = self._normalize_name(self._clean_name(name))
if self.entries:
return name in self.entries
Expand Down
8 changes: 8 additions & 0 deletions tests/test_s3boto.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from django.core.files.base import ContentFile
import django

from boto.exception import S3ResponseError
from boto.s3.key import Key
from boto.utils import parse_ts, ISO8601

Expand Down Expand Up @@ -196,6 +197,13 @@ def test_storage_open_write(self):
)
file._multipart.complete_upload.assert_called_once()

def test_storage_exists_bucket(self):
self.storage._connection.get_bucket.side_effect = S3ResponseError(404, 'No bucket')

This comment has been minimized.

Copy link
@mbarrien

mbarrien Feb 11, 2016

Contributor

I'm not quite sure that this test does what you think it's testing; I don't think S3ResponseError could ever be raised.

When auto_create_bucket is False (the default), then in the real non-test code, get_bucket(validate=False) is called inside _get_or_create_bucket(), and with validate=False, no actual call ever goes to S3 that could return the S3ResponseError. Thus this is a no-op, but I think in this case you actually /do/ want it it actually check the buckets existence.

Also note that I'm trying to get S3Boto3 up to date with the commits, and figuring out how to properly emulate this is my last blocker to getting you an update pull request.

self.assertFalse(self.storage.exists(''))

self.storage._connection.get_bucket.side_effect = None
self.assertTrue(self.storage.exists(''))

def test_storage_exists(self):
key = self.storage.bucket.new_key.return_value
key.exists.return_value = True
Expand Down

0 comments on commit b95ec91

Please sign in to comment.