Amazon SageMaker: Invoke endpoint with file as multipart/form-data

Question:

After setting up an endpoint for my model on Amazon SageMaker, I am trying to invoke it with a POST request which contains a file with a key image & content type as multipart/form-data.

My AWS CLI command is like this:

which should be an equivalent of:

After running the aws command, the file is not transferred via the request, and my model is receiving the request without any file in it.

Can someone please tell me what should be the correct format of the aws command in order to achieve this?

Answer:

The first problem is that you’re using ‘http’ for your CURL request. Virtually all AWS services strictly use ‘https’ as their protocol, SageMaker included. https://docs.aws.amazon.com/general/latest/gr/rande.html. I’m going to assume this was a typo though.

You can check the verbose output of the AWS CLI by passing the ‘–debug’ argument to your call. I re-ran a similar experiment with my favorite duck.jpg image:

Looking at the output, I see:

It looks like the AWS CLI is using the string literal ‘@/duck.jpg’, not the file contents.

Trying again with curl and the “–verbose” flag:

I see the following:

Much better. The ‘@’ operator is a CURL specific feature. The AWS CLI does have a way to pass files though:

There is also a ‘file’ for non-binary files such as JSON. Unfortunately you cannot have the prefix. That is, you cannot say:

You can prepend the string ‘image=’ to your file with a command such as the following. (You’ll probably need to be more clever if your images are really big; this is really inefficient.)

Your final command would then be:

Another note: Using raw curl with AWS services is extremely difficult due to the AWS Auth signing requirements – https://docs.aws.amazon.com/AmazonS3/latest/API/sig-v4-authenticating-requests.html

It can be done, but you’ll likely be more productive by using the AWS CLI or a pre-existing tool such as Postman – https://docs.aws.amazon.com/apigateway/latest/developerguide/how-to-use-postman-to-call-api.html

Leave a Reply