-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathcreate_bucket
More file actions
executable file
·86 lines (77 loc) · 3.36 KB
/
create_bucket
File metadata and controls
executable file
·86 lines (77 loc) · 3.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#!/usr/bin/env python
import os
import boto.s3
import json
import swiftclient
from boto import config as botoconfig
from boto.exception import S3CreateError
from boto.s3.connection import S3Connection, OrdinaryCallingFormat
from oauth2client.service_account import ServiceAccountCredentials
from gcloud.storage.client import Client
from gcloud import exceptions
from azure.storage.blob import BlobService
def bucket_exists(conn, name):
bucket = conn.lookup(name)
if not bucket:
return False
return True
bucket_name = os.getenv('BUCKET_NAME')
region = os.getenv('AWS_REGION')
if os.getenv('DATABASE_STORAGE') == "s3":
conn = boto.s3.connect_to_region(region)
if not bucket_exists(conn, bucket_name):
try:
conn.create_bucket(bucket_name, location=region)
except S3CreateError as err:
# try again in the default S3 region before giving up
default_region = 'us-east-1'
if region != default_region:
print('Failed to create bucket in {}, trying default region {}...'.format(
region, default_region))
conn = boto.s3.connect_to_region(default_region)
if not bucket_exists(conn, bucket_name):
conn.create_bucket(bucket_name, location=default_region)
# TODO: if we got here, somehow we need to propagate "us-east-1"
# into WALE_S3_ENDPOINT...
elif os.getenv('DATABASE_STORAGE') == "gcs":
scopes = ['https://www.googleapis.com/auth/devstorage.full_control']
credentials = ServiceAccountCredentials.from_json_keyfile_name(os.getenv('GOOGLE_APPLICATION_CREDENTIALS'), scopes=scopes)
with open(os.getenv('GOOGLE_APPLICATION_CREDENTIALS')) as data_file:
data = json.load(data_file)
conn = Client(credentials=credentials, project=data['project_id'])
exists = True
try:
conn.get_bucket(bucket_name)
except exceptions.NotFound:
exists = False
except:
raise
if not exists:
conn.create_bucket(bucket_name)
elif os.getenv('DATABASE_STORAGE') == "azure":
conn = BlobService(account_name=os.getenv('WABS_ACCOUNT_NAME'), account_key=os.getenv('WABS_ACCESS_KEY'))
#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).
conn.create_container(bucket_name)
elif os.getenv('DATABASE_STORAGE') == "swift":
conn = swiftclient.Connection(
user=os.getenv('SWIFT_USER'),
key=os.getenv('SWIFT_PASSWORD'),
authurl=os.getenv('SWIFT_AUTHURL'),
auth_version=os.getenv('SWIFT_AUTH_VERSION'),
tenant_name=os.getenv('SWIFT_TENANT')
)
# swift also does not throw exception if container already exists.
conn.put_container(os.getenv('BUCKET_NAME'))
else:
botoconfig.add_section('s3')
botoconfig.set('s3', 'use-sigv4', 'True')
botoconfig.add_section('Boto')
botoconfig.set('Boto', 'is_secure', 'False')
conn = S3Connection(
host=os.getenv('S3_HOST'),
port=int(os.getenv('S3_PORT')),
calling_format=OrdinaryCallingFormat())
# HACK(bacongobbler): allow boto to connect to minio by changing the region name for s3v4 auth
conn.auth_region_name = os.getenv('AWS_REGION')
if not bucket_exists(conn, bucket_name):
conn.create_bucket(bucket_name)