-
Notifications
You must be signed in to change notification settings - Fork 0
/
storage.py
312 lines (264 loc) · 11.5 KB
/
storage.py
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
# Copyright 2016-2019 The Van Valen Lab at the California Institute of
# Technology (Caltech), with support from the Paul Allen Family Foundation,
# Google, & National Institutes of Health (NIH) under Grant U24CA224309-01.
# All rights reserved.
#
# Licensed under a modified Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.github.com/vanvalenlab/kiosk-redis-consumer/LICENSE
#
# The Work provided may be used for non-commercial academic purposes only.
# For any other use of the Work, including commercial use, please contact:
# vanvalenlab@gmail.com
#
# Neither the name of Caltech nor the names of its contributors may be used
# to endorse or promote products derived from this software without specific
# prior written permission.
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ============================================================================
"""Storage Interface to upload / download files from / to the cloud"""
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import time
import timeit
import os
import logging
import boto3
from google.cloud import storage as google_storage
from google.cloud import exceptions as google_exceptions
from redis_consumer import settings
from redis_consumer.settings import DOWNLOAD_DIR
class StorageException(Exception):
"""Custom Exception for the Storage classes"""
pass
def get_client(cloud_provider):
"""Returns the Storage Client appropriate for the cloud provider
# Arguments:
cloud_provider: Indicates which cloud platform (AWS vs GKE)
# Returns:
storage_client: Client for interacting with the cloud.
"""
cloud_provider = str(cloud_provider).lower()
logger = logging.getLogger('storage.get_client')
if cloud_provider == 'aws':
storage_client = S3Storage(settings.AWS_S3_BUCKET)
elif cloud_provider == 'gke':
storage_client = GoogleStorage(settings.GCLOUD_STORAGE_BUCKET)
else:
errmsg = 'Bad value for CLOUD_PROVIDER: %s'
logger.error(errmsg, cloud_provider)
raise ValueError(errmsg % cloud_provider)
return storage_client
class Storage(object): # pylint: disable=useless-object-inheritance
"""General class to interact with cloud storage buckets.
Supported cloud stroage provider will have child class implementations.
Args:
bucket: cloud storage bucket name
download_dir: path to local directory to save downloaded files
"""
def __init__(self, bucket, download_dir=DOWNLOAD_DIR, backoff=1.5):
self._client = None
self.bucket = bucket
self.download_dir = download_dir
self.output_dir = 'output'
self.backoff = backoff
self.logger = logging.getLogger(str(self.__class__.__name__))
def get_download_path(self, filepath, download_dir=None):
"""Get local filepath for soon-to-be downloaded file.
Args:
filepath: key of file in cloud storage to download
download_dir: path to directory to save file
Returns:
dest: local path to downloaded file
"""
if download_dir is None:
download_dir = self.download_dir
no_upload_dir = os.path.join(*(filepath.split(os.path.sep)[1:]))
dest = os.path.join(download_dir, no_upload_dir)
if not os.path.isdir(os.path.dirname(dest)):
os.makedirs(os.path.dirname(dest))
return dest
def download(self, filepath, download_dir):
"""Download a file from the cloud storage bucket.
Args:
filepath: key of file in cloud storage to download
download_dir: path to directory to save file
Returns:
dest: local path to downloaded file
"""
raise NotImplementedError
def upload(self, filepath, subdir=None):
"""Upload a file to the cloud storage bucket.
Args:
filepath: local path to file to upload
Returns:
dest: key of uploaded file in cloud storage
"""
raise NotImplementedError
class GoogleStorage(Storage):
"""Interact with Google Cloud Storage buckets.
Args:
bucket: cloud storage bucket name
download_dir: path to local directory to save downloaded files
"""
def __init__(self, bucket, download_dir=DOWNLOAD_DIR):
super(GoogleStorage, self).__init__(bucket, download_dir)
self._client = google_storage.Client()
self.bucket_url = 'www.googleapis.com/storage/v1/b/{}/o'.format(bucket)
def get_public_url(self, filepath):
"""Get the public URL to download the file.
Args:
filepath: key to file in cloud storage
Returns:
url: Public URL to download the file
"""
bucket = self._client.get_bucket(self.bucket)
blob = bucket.blob(filepath)
blob.make_public()
return blob.public_url
def upload(self, filepath, subdir=None):
"""Upload a file to the cloud storage bucket.
Args:
filepath: local path to file to upload
Returns:
dest: key of uploaded file in cloud storage
"""
start = timeit.default_timer()
self.logger.debug('Uploading %s to bucket %s.', filepath, self.bucket)
retrying = True
while retrying:
try:
dest = os.path.basename(filepath)
if subdir:
if str(subdir).startswith('/'):
subdir = subdir[1:]
dest = os.path.join(subdir, dest)
dest = os.path.join(self.output_dir, dest)
bucket = self._client.get_bucket(self.bucket)
blob = bucket.blob(dest)
blob.upload_from_filename(filepath, predefined_acl='publicRead')
self.logger.debug('Uploaded %s to bucket %s in %s seconds.',
filepath, self.bucket,
timeit.default_timer() - start)
retrying = False
return dest, blob.public_url
except google_exceptions.TooManyRequests as err:
self.logger.warning('Encountered %s: %s. Backing off for %s '
'seconds...', type(err).__name__, err,
self.backoff)
time.sleep(self.backoff)
retrying = True # Unneccessary but explicit
except Exception as err:
retrying = False
self.logger.error('Encountered %s: %s while uploading %s.',
type(err).__name__, err, filepath)
raise err
def download(self, filepath, download_dir=None):
"""Download a file from the cloud storage bucket.
Args:
filepath: key of file in cloud storage to download
download_dir: path to directory to save file
Returns:
dest: local path to downloaded file
"""
dest = self.get_download_path(filepath, download_dir)
self.logger.debug('Downloading %s to %s.', filepath, dest)
retrying = True
while retrying:
try:
start = timeit.default_timer()
blob = self._client.get_bucket(self.bucket).blob(filepath)
blob.download_to_filename(dest)
self.logger.debug('Downloaded %s from bucket %s in %s seconds.',
dest, self.bucket,
timeit.default_timer() - start)
return dest
except google_exceptions.TooManyRequests as err:
self.logger.warning('Encountered %s: %s. Backing off for %s '
'seconds and...', type(err).__name__, err,
self.backoff)
time.sleep(self.backoff)
retrying = True # Unneccessary but explicit
except Exception as err:
retrying = False
self.logger.error('Encountered %s: %s while downloading %s.',
type(err).__name__, err, filepath)
raise err
class S3Storage(Storage):
"""Interact with Amazon S3 buckets.
Args:
bucket: cloud storage bucket name
download_dir: path to local directory to save downloaded files
"""
def __init__(self, bucket, download_dir=DOWNLOAD_DIR):
super(S3Storage, self).__init__(bucket, download_dir)
self._client = boto3.client(
's3',
region_name=settings.AWS_REGION,
aws_access_key_id=settings.AWS_ACCESS_KEY_ID,
aws_secret_access_key=settings.AWS_SECRET_ACCESS_KEY)
self.bucket_url = 's3.amazonaws.com/{}'.format(bucket)
def get_public_url(self, filepath):
"""Get the public URL to download the file.
Args:
filepath: key to file in cloud storage
Returns:
url: Public URL to download the file
"""
return 'https://{url}/{obj}'.format(url=self.bucket_url, obj=filepath)
def upload(self, filepath, subdir=None):
"""Upload a file to the cloud storage bucket.
Args:
filepath: local path to file to upload
Returns:
dest: key of uploaded file in cloud storage
"""
start = timeit.default_timer()
dest = os.path.basename(filepath)
if subdir:
if str(subdir).startswith('/'):
subdir = subdir[1:]
dest = os.path.join(subdir, dest)
dest = os.path.join(self.output_dir, dest)
self.logger.debug('Uploading %s to bucket %s.', filepath, self.bucket)
try:
self._client.upload_file(filepath, self.bucket, dest)
self.logger.debug('Uploaded %s to bucket %s in %s seconds.',
filepath, self.bucket,
timeit.default_timer() - start)
return dest, self.get_public_url(dest)
except Exception as err:
self.logger.error('Encountered %s: %s while uploading %s.',
type(err).__name__, err, filepath)
raise err
def download(self, filepath, download_dir=None):
"""Download a file from the cloud storage bucket.
Args:
filepath: key of file in cloud storage to download
download_dir: path to directory to save file
Returns:
dest: local path to downloaded file
"""
start = timeit.default_timer()
# Bucket keys shouldn't start with "/"
if filepath.startswith('/'):
filepath = filepath[1:]
dest = self.get_download_path(filepath, download_dir)
self.logger.debug('Downloading %s to %s.', filepath, dest)
try:
self._client.download_file(self.bucket, filepath, dest)
self.logger.debug('Downloaded %s from bucket %s in %s seconds.',
dest, self.bucket, timeit.default_timer() - start)
return dest
except Exception as err:
self.logger.error('Encountered %s: %s while downloading %s.',
type(err).__name__, err, filepath)
raise err