ValueError:从S3存储桶加载pkl文件时,“嵌入null字节”

时间:2019-07-13 23:13:11

标签: python-3.x amazon-s3 scikit-learn aws-lambda joblib

我已经将训练有素的模型从sklearn导出到.pkl文件中。现在,我想从s3存储桶中读取文件并将其加载到我的API中,这样就不会在线进行模型训练,而在计费执行时就吃光了。

def load_model():
    s3 = boto3.resource('s3')

    bucket = 'bucket-name'
    key = 'model.pkl'

    file_obj = s3.Bucket(bucket).Object(key).get()['Body']
    model_str = file_obj.read()

    model = joblib.load(model_str)
    return model

运行代码时,我得到以下错误结果。

{
  "errorMessage": "embedded null byte",
  "errorType": "ValueError",
  "stackTrace": [
    "  File \"/var/task/lambda_function.py\", line 10, in lambda_handler\n    
load_model()\n",
    "  File \"/var/task/lambda_function.py\", line 27, in load_model\n    
model = joblib.load(model_str)\n",
"  File \"/opt/python/lib/python3.7/site- 
packages/sklearn/externals/joblib/numpy_pickle.py\", line 590, in load\n    
with open(filename, 'rb') as f:\n"
  ]
}

0 个答案:

没有答案