@@ -59,6 +59,7 @@ $ python3 trainer.py
5959``` python
6060# predictor.py
6161
62+ import os
6263import boto3
6364from botocore import UNSIGNED
6465from botocore.client import Config
@@ -69,7 +70,11 @@ labels = ["setosa", "versicolor", "virginica"]
6970
7071class 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
346352import boto3
347353from botocore import UNSIGNED
348354from botocore.client import Config
@@ -353,7 +359,11 @@ labels = ["setosa", "versicolor", "virginica"]
353359
354360class 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
0 commit comments