Removing unused imports in python scripts
[demo.git] / vnfs / DAaaS / sample-apps / sample-minio-save-model / build / save-model-to-minio.py
1 #!/usr/bin/env python
2 #
3 # -------------------------------------------------------------------------
4 #   Copyright (c) 2019 Intel Corporation Intellectual Property
5 #
6 #   Licensed under the Apache License, Version 2.0 (the "License");
7 #   you may not use this file except in compliance with the License.
8 #   You may obtain a copy of the License at
9 #
10 #       http://www.apache.org/licenses/LICENSE-2.0
11 #
12 #   Unless required by applicable law or agreed to in writing, software
13 #   distributed under the License is distributed on an "AS IS" BASIS,
14 #   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 #   See the License for the specific language governing permissions and
16 #   limitations under the License.
17 #
18 # -------------------------------------------------------------------------
19 #
20
21 '''Sample Python application to save model to Minio'''
22
23 # python imports
24
25 import os
26 from minio import Minio
27 from minio.error import ResponseError
28
29
30 client = Minio(os.environ['S3_ENDPOINT'],
31                access_key=os.environ['AWS_ACCESS_KEY_ID'],
32                secret_key=os.environ['AWS_SECRET_ACCESS_KEY'],
33                secure=False)
34
35
36 def load_model(bucket_name, object_name, filepath):
37     try:
38         client.fget_object(bucket_name, object_name, filepath)
39     except ResponseError as err:
40         print(err)
41
42 if __name__ == "__main__":
43     bucket_name = os.environ['MODEL_BUCKET']
44     location = os.environ['S3_REGION']
45     model = os.environ['MODEL_FILE']
46     filepath = "/app/" + model
47     if not client.bucket_exists(bucket_name):
48         client.make_bucket(bucket_name, location=location)
49
50     found = False
51     try:
52         client.stat_object(bucket_name, model);
53         found = True
54     except Exception as err:
55         found = False
56
57     metadata = {
58         "X-Amz-Meta-model-description": "Sample numpy model",
59         "X-Amz-Meta-model-type": "scikit-learn/numpy",
60         "X-Amz-Meta-model-version": "v1.0.0",
61     }
62     if not found:
63         try:
64             client.fput_object(bucket_name, model, filepath, metadata=metadata)
65         except expression as identifier:
66             print(err)