Rich Leland committed 5e76f7b

Added get or create bucket on init.

Comments (0)

Files changed (1)


 from django.core.exceptions import ImproperlyConfigured
-    from boto.s3.connection import S3Connection
+    from boto.s3.connection import S3Connection, S3ResponseError
     from boto.s3.key import Key
 except ImportError:
     raise ImproperlyConfigured, "Could not load Boto's S3 bindings.\
 class S3BotoStorage(Storage):
     """Amazon Simple Storage Service using Boto"""
-    def __init__(self, bucket="root", bucketprefix=BUCKET_PREFIX, 
-            access_key=None, secret_key=None, acl=DEFAULT_ACL, headers=HEADERS):
+    def __init__(self, bucket=BUCKET_PREFIX, access_key=None, secret_key=None,
+                       acl=DEFAULT_ACL, headers=HEADERS):
         self.acl = acl
         self.headers = headers
              access_key, secret_key = self._get_access_keys()
         self.connection = S3Connection(access_key, secret_key)
-        self.bucket = self.connection.create_bucket(bucketprefix + bucket)
+        self.bucket = self._get_or_create_bucket(bucket)
     def _get_access_keys(self):
         return None, None
+    def _get_or_create_bucket(self, name):
+        """Retrieves a bucket if it exists, otherwise creates it."""
+        try:
+            return self.connection.get_bucket(name)
+        except S3ResponseError:
+            return self.connection.create_bucket(name)
     def _clean_name(self, name):
         # Useful for windows' paths
         return os.path.normpath(name).replace('\\', '/')
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.