22
33import os
44
5- import boto3
6- import botocore
5+ import boto
76import json
87import swiftclient
9- from botocore . utils import fix_s3_host
10- from botocore . client import Config
8+ from boto import config as botoconfig
9+ from boto . s3 . connection import S3Connection , OrdinaryCallingFormat
1110from oauth2client .service_account import ServiceAccountCredentials
1211from gcloud .storage .client import Client
1312from gcloud import exceptions
14- from azure .storage .blob import BlockBlobService
13+ from azure .storage .blob import BlobService
14+
15+ def bucket_exists (conn , name ):
16+ bucket = conn .lookup (name )
17+ if not bucket :
18+ return False
19+ return True
1520
1621bucket_name = os .getenv ('BUCKET_NAME' )
1722
1823if os .getenv ('DATABASE_STORAGE' ) == "s3" :
19- conn = boto3 .resource ('s3' )
20- exists = True
21- try :
22- conn .meta .client .head_bucket (Bucket = bucket_name )
23- except botocore .exceptions .ClientError as e :
24- # If a client error is thrown, then check that it was a 404 error.
25- # If it was a 404 error, then the bucket does not exist.
26- error_code = int (e .response ['Error' ]['Code' ])
27- if error_code == 404 :
28- exists = False
29- else :
30- raise
24+ conn = boto .connect_s3 ()
3125
32- if not exists :
33- conn .create_bucket (Bucket = bucket_name )
26+ if not bucket_exists ( conn , bucket_name ) :
27+ conn .create_bucket (bucket_name )
3428
3529elif os .getenv ('DATABASE_STORAGE' ) == "gcs" :
3630 scopes = ['https://www.googleapis.com/auth/devstorage.full_control' ]
3731 credentials = ServiceAccountCredentials .from_json_keyfile_name (os .getenv ('GS_APPLICATION_CREDS' ), scopes = scopes )
3832 with open (os .getenv ('GS_APPLICATION_CREDS' )) as data_file :
3933 data = json .load (data_file )
40- client = Client (credentials = credentials , project = data ['project_id' ])
34+ conn = Client (credentials = credentials , project = data ['project_id' ])
4135 exists = True
4236 try :
43- client .get_bucket (bucket_name )
37+ conn .get_bucket (bucket_name )
4438 except exceptions .NotFound :
4539 exists = False
4640 except :
4741 raise
4842 if not exists :
49- client .create_bucket (bucket_name )
43+ conn .create_bucket (bucket_name )
5044
5145elif os .getenv ('DATABASE_STORAGE' ) == "azure" :
52- block_blob_service = BlockBlobService (account_name = os .getenv ('WABS_ACCOUNT_NAME' ), account_key = os .getenv ('WABS_ACCESS_KEY' ))
46+ conn = BlobService (account_name = os .getenv ('WABS_ACCOUNT_NAME' ), account_key = os .getenv ('WABS_ACCESS_KEY' ))
5347 #It doesn't throw an exception if the container exists by default(https://github.com/Azure/azure-storage-python/blob/master/azure/storage/blob/baseblobservice.py#L504).
54- block_blob_service .create_container (bucket_name )
48+ conn .create_container (bucket_name )
5549
5650elif os .getenv ('DATABASE_STORAGE' ) == "swift" :
5751 conn = swiftclient .Connection (
@@ -65,20 +59,15 @@ elif os.getenv('DATABASE_STORAGE') == "swift":
6559 conn .put_container (os .getenv ('BUCKET_NAME' ))
6660
6761else :
68- conn = boto3 .resource ('s3' , endpoint_url = os .getenv ('S3_URL' ), config = Config (signature_version = 's3v4' ))
69- # stop boto3 from automatically changing the endpoint
70- conn .meta .client .meta .events .unregister ('before-sign.s3' , fix_s3_host )
71- exists = True
72- try :
73- conn .meta .client .head_bucket (Bucket = bucket_name )
74- except botocore .exceptions .ClientError as e :
75- # If a client error is thrown, then check that it was a 404 error.
76- # If it was a 404 error, then the bucket does not exist.
77- error_code = int (e .response ['Error' ]['Code' ])
78- if error_code == 404 :
79- exists = False
80- else :
81- raise
82-
83- if not exists :
84- conn .create_bucket (Bucket = bucket_name )
62+ botoconfig .add_section ('s3' )
63+ botoconfig .set ('s3' , 'use-sigv4' , 'True' )
64+ botoconfig .add_section ('Boto' )
65+ botoconfig .set ('Boto' , 'is_secure' , 'False' )
66+ conn = S3Connection (
67+ host = os .getenv ('S3_HOST' ),
68+ port = int (os .getenv ('S3_PORT' )),
69+ calling_format = OrdinaryCallingFormat ())
70+ # HACK(bacongobbler): allow boto to connect to minio by changing the region name for s3v4 auth
71+ conn .auth_region_name = os .getenv ('AWS_REGION' )
72+ if not bucket_exists (conn , bucket_name ):
73+ conn .create_bucket (bucket_name )
0 commit comments