forked from elastic/connectors
-
Notifications
You must be signed in to change notification settings - Fork 0
/
google_cloud_storage.py
420 lines (366 loc) · 15.3 KB
/
google_cloud_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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
#
# Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
# or more contributor license agreements. Licensed under the Elastic License 2.0;
# you may not use this file except in compliance with the Elastic License 2.0.
#
"""Google Cloud Storage source module responsible to fetch documents from Google Cloud Storage."""
import os
import urllib.parse
from functools import cached_property, partial
from aiogoogle import Aiogoogle, HTTPError
from aiogoogle.auth.creds import ServiceAccountCreds
from connectors.logger import logger
from connectors.source import BaseDataSource
from connectors.sources.google import (
load_service_account_json,
validate_service_account_json,
)
from connectors.utils import RetryStrategy, get_pem_format, retryable
CLOUD_STORAGE_READ_ONLY_SCOPE = "https://www.googleapis.com/auth/devstorage.read_only"
CLOUD_STORAGE_BASE_URL = "https://console.cloud.google.com/storage/browser/_details/"
API_NAME = "storage"
API_VERSION = "v1"
BLOB_ADAPTER = {
"_id": "id",
"component_count": "componentCount",
"content_encoding": "contentEncoding",
"content_language": "contentLanguage",
"created_at": "timeCreated",
"last_updated": "updated",
"metadata": "metadata",
"name": "name",
"size": "size",
"storage_class": "storageClass",
"_timestamp": "updated",
"type": "contentType",
"url": "selfLink",
"version": "generation",
"bucket_name": "bucket",
}
RETRY_COUNT = 3
RETRY_INTERVAL = 2
STORAGE_EMULATOR_HOST = os.environ.get("STORAGE_EMULATOR_HOST")
RUNNING_FTEST = (
"RUNNING_FTEST" in os.environ
) # Flag to check if a connector is run for ftest or not.
REQUIRED_CREDENTIAL_KEYS = [
"type",
"project_id",
"private_key_id",
"private_key",
"client_email",
"client_id",
"auth_uri",
"token_uri",
]
class GoogleCloudStorageClient:
"""A google client to handle api calls made to Google Cloud Storage."""
def __init__(self, json_credentials):
"""Initialize the ServiceAccountCreds class using which api calls will be made.
Args:
json_credentials (dict): Service account credentials json.
"""
self.service_account_credentials = ServiceAccountCreds(
scopes=[CLOUD_STORAGE_READ_ONLY_SCOPE],
**json_credentials,
)
self.user_project_id = self.service_account_credentials.project_id
self._logger = logger
def set_logger(self, logger_):
self._logger = logger_
@retryable(
retries=RETRY_COUNT,
interval=RETRY_INTERVAL,
strategy=RetryStrategy.EXPONENTIAL_BACKOFF,
)
async def api_call(
self,
resource,
method,
sub_method=None,
full_response=False,
**kwargs,
):
"""Make a GET call for Google Cloud Storage with retry for the failed API calls.
Args:
resource (aiogoogle.resource.Resource): Resource name for which api call will be made.
method (aiogoogle.resource.Method): Method available for the resource.
sub_method (aiogoogle.resource.Method, optional): Sub-method available for the method. Defaults to None.
full_response (bool, optional): Specifies whether the response is paginated or not. Defaults to False.
Raises:
exception: A instance of an exception class.
Yields:
Dictionary: Response returned by the resource method.
"""
while True:
try:
async with Aiogoogle(
service_account_creds=self.service_account_credentials
) as google_client:
storage_client = await google_client.discover(
api_name=API_NAME, api_version=API_VERSION
)
if RUNNING_FTEST and not sub_method and STORAGE_EMULATOR_HOST:
self._logger.debug(
f"Using the storage emulator at {STORAGE_EMULATOR_HOST}"
)
# Redirecting calls to fake Google Cloud Storage server for e2e test.
storage_client.discovery_document["rootUrl"] = (
STORAGE_EMULATOR_HOST + "/"
)
resource_object = getattr(storage_client, resource)
method_object = getattr(resource_object, method)
self._logger.debug(
f"Calling '{resource}.{method}' with args: '{dict(**kwargs)}'"
)
if full_response:
first_page_with_next_attached = (
await google_client.as_service_account(
method_object(**kwargs),
full_res=True,
)
)
async for page_items in first_page_with_next_attached:
yield page_items
else:
if sub_method:
method_object = getattr(method_object, sub_method)
yield await google_client.as_service_account(
method_object(**kwargs)
)
break
except AttributeError as error:
self._logger.error(
f"Error occurred while generating the resource/method object for an API call. Error: {error}"
)
raise
class GoogleCloudStorageDataSource(BaseDataSource):
"""Google Cloud Storage"""
name = "Google Cloud Storage"
service_type = "google_cloud_storage"
incremental_sync_enabled = True
def __init__(self, configuration):
"""Set up the connection to the Google Cloud Storage Client.
Args:
configuration (DataSourceConfiguration): Object of DataSourceConfiguration class.
"""
super().__init__(configuration=configuration)
def _set_internal_logger(self):
self._google_storage_client.set_logger(self._logger)
@classmethod
def get_default_configuration(cls):
"""Get the default configuration for Google Cloud Storage.
Returns:
dictionary: Default configuration.
"""
return {
"buckets": {
"display": "textarea",
"label": "Google Cloud Storage buckets",
"order": 1,
"type": "list",
},
"service_account_credentials": {
"display": "textarea",
"label": "Google Cloud service account JSON",
"sensitive": True,
"order": 2,
"type": "str",
},
"use_text_extraction_service": {
"display": "toggle",
"label": "Use text extraction service",
"order": 3,
"tooltip": "Requires a separate deployment of the Elastic Text Extraction Service. Requires that pipeline settings disable text extraction.",
"type": "bool",
"ui_restrictions": ["advanced"],
"value": False,
},
}
async def validate_config(self):
"""Validates whether user inputs are valid or not for configuration field.
Raises:
Exception: The format of service account json is invalid.
"""
await super().validate_config()
validate_service_account_json(
self.configuration["service_account_credentials"], "Google Cloud Storage"
)
@cached_property
def _google_storage_client(self):
"""Initialize and return the GoogleCloudStorageClient
Returns:
GoogleCloudStorageClient: An instance of the GoogleCloudStorageClient.
"""
json_credentials = load_service_account_json(
self.configuration["service_account_credentials"], "Google Cloud Storage"
)
if (
json_credentials.get("private_key")
and "\n" not in json_credentials["private_key"]
):
json_credentials["private_key"] = get_pem_format(
key=json_credentials["private_key"].strip(),
postfix="-----END PRIVATE KEY-----",
)
required_credentials = {
key: value
for key, value in json_credentials.items()
if key in REQUIRED_CREDENTIAL_KEYS
}
return GoogleCloudStorageClient(json_credentials=required_credentials)
async def ping(self):
"""Verify the connection with Google Cloud Storage"""
if RUNNING_FTEST:
return
try:
await anext(
self._google_storage_client.api_call(
resource="projects",
method="serviceAccount",
sub_method="get",
projectId=self._google_storage_client.user_project_id,
)
)
except Exception:
self._logger.exception(
"Error while connecting to the Google Cloud Storage."
)
raise
async def fetch_buckets(self, buckets):
"""Fetch the buckets from the Google Cloud Storage.
Args:
buckets (List): List of buckets.
Yields:
Dictionary: Contains the list of fetched buckets from Google Cloud Storage.
"""
if "*" in buckets:
self._logger.info(
"Fetching all buckets as the configuration field 'buckets' is set to '*'"
)
async for bucket in self._google_storage_client.api_call(
resource="buckets",
method="list",
full_response=True,
project=self._google_storage_client.user_project_id,
userProject=self._google_storage_client.user_project_id,
):
yield bucket
else:
self._logger.info(f"Fetching user configured buckets: {buckets}")
for bucket in buckets:
yield {"items": [{"id": bucket, "name": bucket}]}
async def fetch_blobs(self, buckets):
"""Fetches blobs stored in the bucket from Google Cloud Storage.
Args:
buckets (Dictionary): Contains the list of fetched buckets from Google Cloud Storage.
Yields:
Dictionary: Contains the list of fetched blobs from Google Cloud Storage.
"""
for bucket in buckets.get("items", []):
blob_count = 0
self._logger.info(f"Fetching blobs for '{bucket['id']}' bucket")
try:
async for blob in self._google_storage_client.api_call(
resource="objects",
method="list",
full_response=True,
bucket=bucket["id"],
userProject=self._google_storage_client.user_project_id,
):
blob_count += 1
yield blob
self._logger.info(
f"Total {blob_count} blobs fetched for bucket '{bucket.get('name')}'"
)
except HTTPError as exception:
exception_log_msg = f"Permission denied for {bucket['name']} while fetching blobs. Exception: {exception}."
if exception.res.status_code == 403:
self._logger.warning(exception_log_msg)
else:
self._logger.error(
f"Something went wrong while fetching blobs from {bucket['name']}. Error: {exception}"
)
def prepare_blob_document(self, blob):
"""Apply key mappings to the blob document.
Args:
blob (dictionary): Blob's metadata returned from the Google Cloud Storage.
Returns:
dictionary: Blobs metadata mapped with the keys of `BLOB_ADAPTER`.
"""
blob_document = {}
for elasticsearch_field, google_cloud_storage_field in BLOB_ADAPTER.items():
blob_document[elasticsearch_field] = blob.get(google_cloud_storage_field)
blob_name = urllib.parse.quote(blob_document["name"], safe="'")
blob_document["url"] = (
f"{CLOUD_STORAGE_BASE_URL}{blob_document['bucket_name']}/{blob_name};tab=live_object?project={self._google_storage_client.user_project_id}"
)
return blob_document
def get_blob_document(self, blobs):
"""Generate blob document.
Args:
blobs (dictionary): Dictionary contains blobs list.
Yields:
dictionary: Blobs metadata mapped with the keys of `BLOB_ADAPTER`.
"""
for blob in blobs.get("items", []):
yield self.prepare_blob_document(blob=blob)
async def get_content(self, blob, timestamp=None, doit=None):
"""Extracts the content for allowed file types.
Args:
blob (dictionary): Formatted blob document.
timestamp (timestamp, optional): Timestamp of blob last modified. Defaults to None.
doit (boolean, optional): Boolean value for whether to get content or not. Defaults to None.
Returns:
dictionary: Content document with id, timestamp & text
"""
if not doit:
self._logger.debug(f"Skipping attachment downloading for {blob['name']}")
return
file_size = int(blob["size"])
if file_size <= 0:
self._logger.warning(
f"Skipping file '{blob['name']}' as file size is {file_size}"
)
return
filename = blob["name"]
file_extension = self.get_file_extension(filename)
if not self.can_file_be_downloaded(file_extension, filename, file_size):
return
self._logger.debug(f"Downloading content for file: {filename}")
document = {
"_id": blob["id"],
"_timestamp": blob["_timestamp"],
}
# gcs has a unique download method so we can't utilize
# the generic download_and_extract_file func
async with self.create_temp_file(file_extension) as async_buffer:
await anext(
self._google_storage_client.api_call(
resource="objects",
method="get",
bucket=blob["bucket_name"],
object=filename,
alt="media",
userProject=self._google_storage_client.user_project_id,
pipe_to=async_buffer,
path_params_safe_chars={"object": "'"},
)
)
await async_buffer.close()
document = await self.handle_file_content_extraction(
document, filename, async_buffer.name
)
return document
async def get_docs(self, filtering=None):
"""Get buckets & blob documents from Google Cloud Storage.
Yields:
dictionary: Documents from Google Cloud Storage.
"""
self._logger.info("Successfully connected to the Google Cloud Storage.")
async for bucket in self.fetch_buckets(self.configuration["buckets"]):
async for blobs in self.fetch_blobs(
buckets=bucket,
):
for blob_document in self.get_blob_document(blobs=blobs):
yield blob_document, partial(self.get_content, blob_document)