As it says in the title, I am trying to upload a model.pkl and save it in an API made in django. I manage to save the model.pkl correctly in the API, however the file is uploaded in a corrupt way, because I cannot read the pickle file.
Im open to any solutions that can make the pickle file be uploaded, stored and readed in the API.
class FileUploadView(APIView):
parser_classes = (FileUploadParser,)
def put(self, request):
file = request.data.get('file', None)
file_name = f'path/{file.name}'
path = default_storage.save(name=file_name, content=ContentFile(file.read()))
tmp_file = os.path.join(settings.MEDIA_ROOT, path)
if file is not None:
return Response(f'File: {file.name} successfully uploaded and saved!', status=HTTP_200_OK)
else:
return Response(f'File not found!', status=HTTP_400_BAD_REQUEST)
def read_PickleModel(path_to_PickleModel):
with open(path_to_PickleModel, 'rb') as f:
pickle_model = pickle.load(f)
return pickle_model
read_PickleModel(path_to_PickleModel)
Traceback:
DEBUG: An exception has ocurred: invalid load key, '-'.
When using FileUploadParser the entire body of the request should be the file contents, don't send the file as form data.
To do this in Postman, select "binary" as the data type instead of "form-data" and select your file there
To set the filename you need to set a header "Content-Disposition" and pass attachment; filename=<your_filename.ext>