@@ -6,6 +6,7 @@ import boto.s3
66import json
77import swiftclient
88from boto import config as botoconfig
9+ from boto .exception import S3CreateError
910from boto .s3 .connection import S3Connection , OrdinaryCallingFormat
1011from oauth2client .service_account import ServiceAccountCredentials
1112from gcloud .storage .client import Client
@@ -23,9 +24,20 @@ region = os.getenv('AWS_REGION')
2324
2425if 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+ except S3CreateError as err :
31+ # try again in the default S3 region before giving up
32+ default_region = 'us-east-1'
33+ if region != default_region :
34+ print ('Failed to create bucket in {}, trying default region {}...' .format (
35+ region , default_region ))
36+ conn = boto .s3 .connect_to_region (default_region )
37+ if not bucket_exists (conn , bucket_name ):
38+ conn .create_bucket (bucket_name , location = default_region )
39+ # TODO: if we got here, somehow we need to propagate "us-east-1"
40+ # into WALE_S3_ENDPOINT...
2941
3042elif os .getenv ('DATABASE_STORAGE' ) == "gcs" :
3143 scopes = ['https://www.googleapis.com/auth/devstorage.full_control' ]
0 commit comments