From acdfa41a69c562e28d4f47f4ee04e3a20906543e Mon Sep 17 00:00:00 2001 From: Eugene Prikazchikov Date: Wed, 14 May 2014 15:32:38 +0400 Subject: [PATCH] Changed create_all to try to get bucket first and create if one does not exist --- flask_s3.py | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/flask_s3.py b/flask_s3.py index 20637bc..da9acb3 100644 --- a/flask_s3.py +++ b/flask_s3.py @@ -5,7 +5,7 @@ from flask import url_for as flask_url_for from flask import current_app from boto.s3.connection import S3Connection -from boto.exception import S3CreateError +from boto.exception import S3ResponseError from boto.s3.key import Key logger = logging.getLogger('flask_s3') @@ -184,10 +184,13 @@ def create_all(app, user=None, password=None, bucket_name=None, conn = S3Connection(user, password) # connect to s3 # get_or_create bucket try: - bucket = conn.create_bucket(bucket_name, location=location) - bucket.make_public(recursive=True) - except S3CreateError as e: - raise e + bucket = conn.get_bucket(bucket_name) + except S3ResponseError as e: + if '404 Not Found' in str(e): + bucket = conn.create_bucket(bucket_name, location=location) + else: + raise + bucket.make_public(recursive=True) _upload_files(app, all_files, bucket)