Skip to content

Commit 1ef7a37

Browse files
author
Matthew Fisher
authored
Merge pull request #176 from bacongobbler/fixup-bucket-create
fix(create_bucket): try default s3 region on create error
2 parents 6863e0b + 8bf4c52 commit 1ef7a37

File tree

1 file changed

+14
-2
lines changed

1 file changed

+14
-2
lines changed

rootfs/bin/create_bucket

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import boto.s3
66
import json
77
import swiftclient
88
from boto import config as botoconfig
9+
from boto.exception import S3CreateError
910
from boto.s3.connection import S3Connection, OrdinaryCallingFormat
1011
from oauth2client.service_account import ServiceAccountCredentials
1112
from gcloud.storage.client import Client
@@ -23,9 +24,20 @@ region = os.getenv('AWS_REGION')
2324

2425
if os.getenv('DATABASE_STORAGE') == "s3":
2526
conn = boto.s3.connect_to_region(region)
26-
2727
if not bucket_exists(conn, bucket_name):
28-
conn.create_bucket(bucket_name, location=region)
28+
try:
29+
conn.create_bucket(bucket_name, location=region)
30+
# NOTE(bacongobbler): for versions prior to v2.9.0, the bucket is created in the default region.
31+
# if we got here, we need to propagate "us-east-1" into WALE_S3_ENDPOINT because the bucket
32+
# exists in a different region and we cannot find it.
33+
# TODO(bacongobbler): deprecate this once we drop support for v2.8.0 and lower
34+
except S3CreateError as err:
35+
if region != 'us-east-1':
36+
print('Failed to create bucket in {}. We are now assuming that the bucket was created in us-east-1.'.format(region))
37+
with open(os.path.join(os.environ['WALE_ENVDIR'], "WALE_S3_ENDPOINT"), "w+") as file:
38+
file.write('https+path://s3.amazonaws.com:443')
39+
else:
40+
raise
2941

3042
elif os.getenv('DATABASE_STORAGE') == "gcs":
3143
scopes = ['https://www.googleapis.com/auth/devstorage.full_control']

0 commit comments

Comments
 (0)