8000 functions/slack: config file -> env vars (#3695) · code4ward/python-docs-samples@c83f924 · GitHub
[go: up one dir, main page]

Skip to content

Commit c83f924

Browse files
author
Ace Nassri
authored
functions/slack: config file -> env vars (GoogleCloudPlatform#3695)
1 parent 32777eb commit c83f924

File tree

2 files changed

+7
-17
lines changed

2 files changed

+7
-17
lines changed

functions/ocr/app/config.json

Lines changed: 0 additions & 7 deletions
This file was deleted.

functions/ocr/app/main.py

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818

1919
from google.cloud import pubsub_v1
2020
from google.cloud import storage
21-
from google.cloud import translate
21+
from google.cloud import translate_v2 as translate
2222
from google.cloud import vision
2323

2424
vision_client = vision.ImageAnnotatorClient()
@@ -27,10 +27,6 @@
2727
storage_client = storage.Client()
2828

2929
project_id = os.environ['GCP_PROJECT']
30-
31-
with open('config.json') as f:
32-
data = f.read()
33-
config = json.loads(data)
3430
# [END functions_ocr_setup]
3531

3632

@@ -55,10 +51,11 @@ def detect_text(bucket, filename):
5551
print('Detected language {} for text {}.'.format(src_lang, text))
5652

5753
# Submit a message to the bus for each target language
58-
for target_lang in config.get('TO_LANG', []):
59-
topic_name = config['TRANSLATE_TOPIC']
54+
to_langs = os.environ['TO_LANG'].split(',')
55+
for target_lang in to_langs:
56+
topic_name = os.environ['TRANSLATE_TOPIC']
6057
if src_lang == target_lang or src_lang == 'und':
61-
topic_name = config['RESULT_TOPIC']
58+
topic_name = os.environ['RESULT_TOPIC']
6259
message = {
6360
'text': text,
6461
'filename': filename,
@@ -120,7 +117,7 @@ def translate_text(event, context):
120117
translated_text = translate_client.translate(text,
121118
target_language=target_lang,
122119
source_language=src_lang)
123-
topic_name = config['RESULT_TOPIC']
120+
topic_name = os.environ['RESULT_TOPIC']
124121
message = {
125122
'text': translated_text['translatedText'],
126123
'filename': filename,
@@ -147,7 +144,7 @@ def save_result(event, context):
147144

148145
print('Received request to save file {}.'.format(filename))
149146

150-
bucket_name = config['RESULT_BUCKET']
147+
bucket_name = os.environ['RESULT_BUCKET']
151148
result_filename = '{}_{}.txt'.format(filename, lang)
152149
bucket = storage_client.get_bucket(bucket_name)
153150
blob = bucket.blob(result_filename)

0 commit comments

Comments
 (0)
0