Skip to content

Commit 925e5f2

Browse files
authored
Use aws credentials if available in examples (#1070)
1 parent a8512e1 commit 925e5f2

File tree

8 files changed

+56
-11
lines changed

8 files changed

+56
-11
lines changed

examples/keras/document-denoiser/predictor.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,11 @@ class PythonPredictor:
3838
def __init__(self, config):
3939
# download the model
4040
bucket, key = re.match("s3://(.+?)/(.+)", config["model"]).groups()
41-
s3 = boto3.client("s3", config=Config(signature_version=UNSIGNED))
41+
42+
if os.environ.get("AWS_ACCESS_KEY_ID"):
43+
s3 = boto3.client("s3") # client will use your credentials if available
44+
else:
45+
s3 = boto3.client("s3", config=Config(signature_version=UNSIGNED)) # anonymous client
4246

4347
model_path = os.path.join("/tmp/model.h5")
4448
s3.download_file(bucket, key, model_path)

examples/pytorch/iris-classifier/predictor.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import re
44
import torch
5+
import os
56
import boto3
67
from botocore import UNSIGNED
78
from botocore.client import Config
@@ -14,7 +15,12 @@ class PythonPredictor:
1415
def __init__(self, config):
1516
# download the model
1617
bucket, key = re.match("s3://(.+?)/(.+)", config["model"]).groups()
17-
s3 = boto3.client("s3", config=Config(signature_version=UNSIGNED))
18+
19+
if os.environ.get("AWS_ACCESS_KEY_ID"):
20+
s3 = boto3.client("s3") # client will use your credentials if available
21+
else:
22+
s3 = boto3.client("s3", config=Config(signature_version=UNSIGNED)) # anonymous client
23+
1824
s3.download_file(bucket, key, "/tmp/model.pth")
1925

2026
# initialize the model

examples/sklearn/iris-classifier/README.md

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ $ python3 trainer.py
5959
```python
6060
# predictor.py
6161

62+
import os
6263
import boto3
6364
from botocore import UNSIGNED
6465
from botocore.client import Config
@@ -69,7 +70,11 @@ labels = ["setosa", "versicolor", "virginica"]
6970

7071
class PythonPredictor:
7172
def __init__(self, config):
72-
s3 = boto3.client("s3", config=Config(signature_version=UNSIGNED))
73+
if os.environ.get("AWS_ACCESS_KEY_ID"):
74+
s3 = boto3.client("s3") # client will use your credentials if available
75+
else:
76+
s3 = boto3.client("s3", config=Config(signature_version=UNSIGNED)) # anonymous client
77+
7378
s3.download_file(config["bucket"], config["key"], "/tmp/model.pkl")
7479
self.model = pickle.load(open("/tmp/model.pkl", "rb"))
7580

@@ -343,6 +348,7 @@ First, implement `batch-predictor.py` with a `predict` function that can process
343348
```python
344349
# batch-predictor.py
345350
351+
import os
346352
import boto3
347353
from botocore import UNSIGNED
348354
from botocore.client import Config
@@ -353,7 +359,11 @@ labels = ["setosa", "versicolor", "virginica"]
353359
354360
class PythonPredictor:
355361
def __init__(self, config):
356-
s3 = boto3.client("s3", config=Config(signature_version=UNSIGNED))
362+
if os.environ.get("AWS_ACCESS_KEY_ID"):
363+
s3 = boto3.client("s3") # client will use your credentials if available
364+
else:
365+
s3 = boto3.client("s3", config=Config(signature_version=UNSIGNED)) # anonymous client
366+
357367
s3.download_file(config["bucket"], config["key"], "/tmp/model.pkl")
358368
self.model = pickle.load(open("/tmp/model.pkl", "rb"))
359369

examples/sklearn/iris-classifier/batch-predictor.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# WARNING: you are on the master branch, please refer to the examples on the branch that matches your `cortex version`
22

3+
import os
34
import boto3
45
from botocore import UNSIGNED
56
from botocore.client import Config
@@ -10,7 +11,11 @@
1011

1112
class PythonPredictor:
1213
def __init__(self, config):
13-
s3 = boto3.client("s3", config=Config(signature_version=UNSIGNED))
14+
if os.environ.get("AWS_ACCESS_KEY_ID"):
15+
s3 = boto3.client("s3") # client will use your credentials if available
16+
else:
17+
s3 = boto3.client("s3", config=Config(signature_version=UNSIGNED)) # anonymous client
18+
1419
s3.download_file(config["bucket"], config["key"], "/tmp/model.pkl")
1520
self.model = pickle.load(open("/tmp/model.pkl", "rb"))
1621

examples/sklearn/iris-classifier/predictor.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# WARNING: you are on the master branch, please refer to the examples on the branch that matches your `cortex version`
22

3+
import os
34
import boto3
45
from botocore import UNSIGNED
56
from botocore.client import Config
@@ -10,7 +11,11 @@
1011

1112
class PythonPredictor:
1213
def __init__(self, config):
13-
s3 = boto3.client("s3", config=Config(signature_version=UNSIGNED))
14+
if os.environ.get("AWS_ACCESS_KEY_ID"):
15+
s3 = boto3.client("s3") # client will use your credentials if available
16+
else:
17+
s3 = boto3.client("s3", config=Config(signature_version=UNSIGNED)) # anonymous client
18+
1419
s3.download_file(config["bucket"], config["key"], "/tmp/model.pkl")
1520
self.model = pickle.load(open("/tmp/model.pkl", "rb"))
1621

examples/sklearn/mpg-estimator/predictor.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,12 @@ def __init__(self, config):
1414
model_path = "/tmp/model"
1515
os.makedirs(model_path, exist_ok=True)
1616

17-
# download mlflow model folder from S3 using unsigned (anonymous) aws credentials
18-
s3 = boto3.client("s3", config=Config(signature_version=UNSIGNED))
17+
if os.environ.get("AWS_ACCESS_KEY_ID"):
18+
s3 = boto3.client("s3") # client will use your credentials if available
19+
else:
20+
s3 = boto3.client("s3", config=Config(signature_version=UNSIGNED)) # anonymous client
21+
22+
# download mlflow model folder from S3
1923
bucket, prefix = re.match("s3://(.+?)/(.+)", config["model"]).groups()
2024
response = s3.list_objects_v2(Bucket=bucket, Prefix=prefix)
2125
for s3_obj in response["Contents"]:

examples/tensorflow/license-plate-reader/predictor_lite.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,12 @@ class PythonPredictor:
1515
def __init__(self, config):
1616
# download yolov3 model
1717
bucket, key = re.match("s3://(.+?)/(.+)", config["yolov3"]).groups()
18-
s3 = boto3.client("s3", config=Config(signature_version=UNSIGNED))
18+
19+
if os.environ.get("AWS_ACCESS_KEY_ID"):
20+
s3 = boto3.client("s3") # client will use your credentials if available
21+
else:
22+
s3 = boto3.client("s3", config=Config(signature_version=UNSIGNED)) # anonymous client
23+
1924
model_path = "/tmp/model.h5"
2025
s3.download_file(bucket, key, model_path)
2126

examples/tensorflow/text-generator/predictor.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# WARNING: you are on the master branch, please refer to the examples on the branch that matches your `cortex version`
22

3+
import os
34
import boto3
45
from botocore import UNSIGNED
56
from botocore.client import Config
@@ -9,8 +10,13 @@
910
class TensorFlowPredictor:
1011
def __init__(self, tensorflow_client, config):
1112
self.client = tensorflow_client
12-
s3_client = boto3.client("s3", config=Config(signature_version=UNSIGNED))
13-
self.encoder = get_encoder(s3_client)
13+
14+
if os.environ.get("AWS_ACCESS_KEY_ID"):
15+
s3 = boto3.client("s3") # client will use your credentials if available
16+
else:
17+
s3 = boto3.client("s3", config=Config(signature_version=UNSIGNED)) # anonymous client
18+
19+
self.encoder = get_encoder(s3)
1420

1521
def predict(self, payload):
1622
model_input = {"context": [self.encoder.encode(payload["text"])]}

0 commit comments

Comments
 (0)