AWS SageMaker - Request has Invalid image format - node.js

I'm testing Amazon SageMaker service with NodeJS + AWS SDK and after create a new model and endpoint based on this example (everything works well in the notebook, including the request to the endpoint), I'm trying to create requests from my Express application, but I'm getting the following error:
Error during recognition: { InvalidImageFormatException: Request has Invalid image format
at Request.extractError (/Users/pdonaire/Documents/workspaceNode/trsps-controller/node_modules/aws-sdk/lib/protocol/json.js:48:27)
at Request.callListeners (/Users/pdonaire/Documents/workspaceNode/trsps-controller/node_modules/aws-sdk/lib/sequential_executor.js:109:20)
at Request.emit (/Users/pdonaire/Documents/workspaceNode/trsps-controller/node_modules/aws-sdk/lib/sequential_executor.js:81:10)
at Request.emit (/Users/pdonaire/Documents/workspaceNode/trsps-controller/node_modules/aws-sdk/lib/request.js:683:14)
at Request.transition (/Users/pdonaire/Documents/workspaceNode/trsps-controller/node_modules/aws-sdk/lib/request.js:22:10)
at AcceptorStateMachine.runTo (/Users/pdonaire/Documents/workspaceNode/trsps-controller/node_modules/aws-sdk/lib/state_machine.js:14:12)
at /Users/pdonaire/Documents/workspaceNode/trsps-controller/node_modules/aws-sdk/lib/state_machine.js:26:10
at Request.<anonymous> (/Users/pdonaire/Documents/workspaceNode/trsps-controller/node_modules/aws-sdk/lib/request.js:38:9)
at Request.<anonymous> (/Users/pdonaire/Documents/workspaceNode/trsps-controller/node_modules/aws-sdk/lib/request.js:685:12)
at Request.callListeners (/Users/pdonaire/Documents/workspaceNode/trsps-controller/node_modules/aws-sdk/lib/sequential_executor.js:119:18)
at Request.emit (/Users/pdonaire/Documents/workspaceNode/trsps-controller/node_modules/aws-sdk/lib/sequential_executor.js:81:10)
at Request.emit (/Users/pdonaire/Documents/workspaceNode/trsps-controller/node_modules/aws-sdk/lib/request.js:683:14)
at Request.transition (/Users/pdonaire/Documents/workspaceNode/trsps-controller/node_modules/aws-sdk/lib/request.js:22:10)
at AcceptorStateMachine.runTo (/Users/pdonaire/Documents/workspaceNode/trsps-controller/node_modules/aws-sdk/lib/state_machine.js:14:12)
at /Users/pdonaire/Documents/workspaceNode/trsps-controller/node_modules/aws-sdk/lib/state_machine.js:26:10
at Request.<anonymous> (/Users/pdonaire/Documents/workspaceNode/trsps-controller/node_modules/aws-sdk/lib/request.js:38:9)
message: 'Request has Invalid image format',
code: 'InvalidImageFormatException',
time: 2018-09-10T04:42:07.530Z,
requestId: 'de3a04ff-b4b3-11e8-9bd8-8b88f803570c',
statusCode: 400,
retryable: false,
retryDelay: 55.860720412209794 }
My code is as follows:
export function sendRequestToSageMaker(base64image) {
const params = {
Body: new Buffer(base64image, 'base64') , /* Strings will be Base-64 encoded on your behalf */ /* required */
EndpointName: 'DEMO-imageclassification-ep--XXXX', /* required */
Accept: 'application/json',
ContentType: 'application/x-image'
};
sagemakerruntime.invokeEndpoint(params, function(err, data) {
if (err)
console.error(err, err.stack); // an error occurred
else
console.log(data); // successful response
});
return null;
}
base64image is req.body.photo from a request that I'm doing with Postman with a JSON and just a single photo property with a base64 string that I've made with base64-image.de website.
Any help will be helpful! Thank you so much! :-)

the SageMaker image classification algorithm only supports images as payload and does not support base64 encoded payload and that is why you see the InvalidImageFormatException.

Related

How to use MFA in Javascript aws-sdk?

I have an infrastructure where AWS login happens on a Role basis requires MFA from MS authenticator which pops up on Mobile device for Accept or Decline. Normally on CLI, we use saml2aws and it generates temporary access key secret key with assume role.
I am trying to use aws-sdk in my nodejs and trying to validate the credentials but not getting any popup and gets below error.
Please suggest the method or way to achieve this.
const AWS = require('aws-sdk');
var eks = new AWS.EKS({ accessKeyId: "ABCDEFGHIJKLMNOPQRSTUVWXYZ", secretAccessKey : "gsdhjfkjbkbfvfkqbvfhvqhfvqhfv", region: "us-west-2"});
var params = { name: "testing" };
eks.describeCluster(params, function(err, data) {
if (err) console.log(err, err.stack); // an error occurred
else console.log(data); // successful response
});
After executing the above code: I am receiving the below error:
UnrecognizedClientException: The security token included in the request is invalid.
at Object.extractError (D:\Prashant\kubernetes-app\node_modules\aws-sdk\lib\protocol\json.js:52:27)
at Request.extractError (D:\Prashant\kubernetes-app\node_modules\aws-sdk\lib\protocol\rest_json.js:55:8)
at Request.callListeners (D:\Prashant\kubernetes-app\node_modules\aws-sdk\lib\sequential_executor.js:106:20)
at Request.emit (D:\Prashant\kubernetes-app\node_modules\aws-sdk\lib\sequential_executor.js:78:10)
at Request.emit (D:\Prashant\kubernetes-app\node_modules\aws-sdk\lib\request.js:688:14)
at Request.transition (D:\Prashant\kubernetes-app\node_modules\aws-sdk\lib\request.js:22:10)
at AcceptorStateMachine.runTo (D:\Prashant\kubernetes-app\node_modules\aws-sdk\lib\state_machine.js:14:12)
at D:\Prashant\kubernetes-app\node_modules\aws-sdk\lib\state_machine.js:26:10
at Request.<anonymous> (D:\Prashant\kubernetes-app\node_modules\aws-sdk\lib\request.js:38:9)
at Request.<anonymous> (D:\Prashant\kubernetes-app\node_modules\aws-sdk\lib\request.js:690:12) {
code: 'UnrecognizedClientException',
time: 2020-11-03T22:36:19.516Z,
requestId: '2e3486d0-3219-410c-8963-970958c5c14d',
statusCode: 403,
retryable: false,
retryDelay: 91.55836550904752
}
This request is completed. Was a bit tricky but done. Comment if anyone else facing the issue will let you know the solution.

dynamodb TransactWriteItems error: An unknown operation was requested

I'm trying to update multiple items using TransactWriteItems, But I have got the following error:
{
UnknownOperationException: An unknown operation was requested.
at Request.extractError (project-dir\node_modules\aws-sdk\lib\protocol\json.js:51:27)
at Request.callListeners (project-dir\node_modules\aws-sdk\lib\sequential_executor.js:106:20)
at Request.emit (project-dir\node_modules\aws-sdk\lib\sequential_executor.js:78:10)
at Request.emit (project-dir\node_modules\aws-sdk\lib\request.js:683:14)
at Request.transition (project-dir\node_modules\aws-sdk\lib\request.js:22:10)
at AcceptorStateMachine.runTo (project-dir\node_modules\aws-sdk\lib\state_machine.js:14:12)
at project-dir\node_modules\aws-sdk\lib\state_machine.js:26:10
at Request.<anonymous> (project-dir\node_modules\aws-sdk\lib\request.js:38:9)
at Request.<anonymous> (project-dir\node_modules\aws-sdk\lib\request.js:685:12)
at Request.callListeners (project-dir\node_modules\aws-sdk\lib\sequential_executor.js:116:18)
message: 'An unknown operation was requested.',
code: 'UnknownOperationException',
time: 2019-06-21T18:28:46.776Z,
requestId: '',
statusCode: 400,
retryable: false,
retryDelay: 17.98291928629798
}
My Code is given below:
const dynamodb = new aws.DynamoDB({ endpoint: "http://localhost:8000" });
const result = await dynamodb
.transactWriteItems({
TransactItems: [{
"Update":{
"TableName":"dbTable1",
"Key":{
"id": { "S":"table-primary-key-id-01" }
},
"ConditionExpression": "#id = :id",
"UpdateExpression":"set #orderNo = :orderNo",
"ExpressionAttributeNames": {
"#id": "id",
"#orderNo":"orderNo"
},
"ExpressionAttributeValues":{
":id":{"S":"table-primary-key-id-01"},
":orderNo":{"N":"9"}
}
}
}]
})
.promise();
Any help would be very much appreciable. Thanks in advance.
I see you are running the TransactWriteItems operation on a local dynamodb instance. Unfortunately AWS has not implemented support for Transactions API call for dynamodb local instances.

AWS S3 parse URI error with docker containers

On my local environment, I develop a file upload feature with a express/node backend and an AWS S3 server (using scality/S3). Both services are dockerized in their own container, and the communication works fine.
My problem is that the S3 server seems not to understand the upload request. Here is the code I try to use :
const s3 = new S3({
accessKeyId: 'accessKey1',
secretAccessKey: 'verySecretKey1',
endpoint: 's3server:8000',
sslEnabled: false,
s3ForcePathStyle: true,
});
function uploadFile(file) {
const params = {
Body: file,
Bucket: 'testbucket',
Key: 'testkey',
ACL: 'public-read',
};
s3.upload(params, (err, data) => {
if (err) console.log(err, err.stack); // an error occurred
else console.log(data); // successful response
});
}
The S3 server receives the request but send back the error:
{"name":"S3","clientIP":"::ffff:172.18.0.5","clientPort":45066,"httpMethod":"PUT","httpURL":"/testbucket/testkey","time":1502458550488,"req_id":"7f4fac280644b5cf203c","level":"info","message":"received request","hostname":"faf8cb0b47d4","pid":103}
{"name":"S3","bytesSent":192,"clientIP":"::ffff:172.18.0.5","clientPort":45066,"httpMethod":"PUT","httpURL":"/testbucket/testkey","httpCode":400,"time":1502458550491,"req_id":"7f4fac280644b5cf203c","elapsed_ms":2.607924,"level":"info","message":"responded with error XML","hostname":"faf8cb0b47d4","pid":103}
And the node backend logs the error:
{ InvalidURI: Couldn't parse the specified URI.
at Request.extractError (/usr/src/api/node_modules/aws-sdk/lib/services/s3.js:577:35)
at Request.callListeners (/usr/src/api/node_modules/aws-sdk/lib/sequential_executor.js:105:20)
at Request.emit (/usr/src/api/node_modules/aws-sdk/lib/sequential_executor.js:77:10)
at Request.emit (/usr/src/api/node_modules/aws-sdk/lib/request.js:683:14)
at Request.transition (/usr/src/api/node_modules/aws-sdk/lib/request.js:22:10)
at AcceptorStateMachine.runTo (/usr/src/api/node_modules/aws-sdk/lib/state_machine.js:14:12)
at /usr/src/api/node_modules/aws-sdk/lib/state_machine.js:26:10
at Request.<anonymous> (/usr/src/api/node_modules/aws-sdk/lib/request.js:38:9)
at Request.<anonymous> (/usr/src/api/node_modules/aws-sdk/lib/request.js:685:12)
at Request.callListeners (/usr/src/api/node_modules/aws-sdk/lib/sequential_executor.js:115:18)
at Request.emit (/usr/src/api/node_modules/aws-sdk/lib/sequential_executor.js:77:10)
at Request.emit (/usr/src/api/node_modules/aws-sdk/lib/request.js:683:14)
at Request.transition (/usr/src/api/node_modules/aws-sdk/lib/request.js:22:10)
at AcceptorStateMachine.runTo (/usr/src/api/node_modules/aws-sdk/lib/state_machine.js:14:12)
at /usr/src/api/node_modules/aws-sdk/lib/state_machine.js:26:10
at Request.<anonymous> (/usr/src/api/node_modules/aws-sdk/lib/request.js:38:9)
message: 'Couldn\'t parse the specified URI.',
code: 'InvalidURI',
region: null,
time: 2017-08-11T13:35:50.510Z,
requestId: '7f4fac280644b5cf203c',
extendedRequestId: '7f4fac280644b5cf203c',
cfId: undefined,
statusCode: 400,
retryable: false,
retryDelay: 57.08331622136704 }
I saw some answers about utf-8 encoding problems but it didn't work on my case :/
Does anyone has an idea about why it can't parse the URI ?
Thank you for your time !
You can fix this by providing your own config.json using volumes, as suggested here.
Copy config.json.
Add "s3server": "us-east-1", to "restEndpoints".
docker run -v $(pwd)/config.json:/usr/src/app/config.json ...
or if you're using docker compose, add something like the following to your docker-compose.yaml:
s3server:
image: scality/s3server
restart: always
expose: [8000]
stdin_open: true
tty: true
container_name: s3server
volumes:
- "${PWD}/s3config.json:/usr/src/app/config.json"

AWS SNS publish 404 error

I have a lambda function where I am trying to publish a message to SNS topic:
Here is the code:
var params = {
Message: origmessage,
Subject: "Retrying posting URL",
TopicArn: "arn:aws:sns:us-west-2:<acctid>:SampleTopic"
};
sns.publish(params, function(err, data) {
if (err) {
console.log("SNS publish error")
console.log(err, err.stack)
console.log(data, data)
console.log("RETRY PARAMS:" + params);
context.fail(err);
}
});
SNS publish error is cryptic as I am unable to find the root cause. Appreciate if someone can help me show a way to debug this.
Here is the error:
2017-01-19T00:41:13.947Z d440064d-dddf-11e6-abad-8de6d2739d36 {
[404: null]
message: null,
code: 404,
time: Thu Jan 19 2017 00:41:13 GMT+0000 (UTC),
requestId: '0ENTKJ09RT82VE0SFEUJKT75NVVV4KQNSO5AEMVJF66Q9ASUAAJG',
statusCode: 404,
retryable: false,
retryDelay: 26.97333029936999
}
'404: null
at Request.extractError (/var/task/node_modules/aws-sdk/lib/protocol/query.js:45:29)
at Request.callListeners (/var/task/node_modules/aws-sdk/lib/sequential_executor.js:105:20)
at Request.emit (/var/task/node_modules/aws-sdk/lib/sequential_executor.js:77:10)
at Request.emit (/var/task/node_modules/aws-sdk/lib/request.js:668:14)
at Request.transition (/var/task/node_modules/aws-sdk/lib/request.js:22:10)
at AcceptorStateMachine.runTo (/var/task/node_modules/aws-sdk/lib/state_machine.js:14:12)
at /var/task/node_modules/aws-sdk/lib/state_machine.js:26:10
at Request.<anonymous> (/var/task/node_modules/aws-sdk/lib/request.js:38:9)
at Request.<anonymous> (/var/task/node_modules/aws-sdk/lib/request.js:670:12)
at Request.callListeners (/var/task/node_modules/aws-sdk/lib/sequential_executor.js:115:18)'
As per your comments, when you use AWS.config.update, it affects all services.
By setting the endpoint parameter to the dynamodb endpoint, you are setting that endpoint for all services, including SNS.
This is an offending example where it says to specify the endpoint for dynamodb:
http://docs.aws.amazon.com/amazondynamodb/latest/gettingstartedguide/GettingStarted.NodeJs.Summary.html
Instead, the endpoint should be taken out of AWS.config.update and somehow specified in each service's constructor.
Matt - That was it. Dynamodb config was what was causing this issue. Appreciate all your help.
Thanks

call and send data to an aws-lambda function using a node.js script

I have a lambda function running on amazon aws cloud. Now I want to make a node.js script to send data from my local system to aws lambda and use callback function to print the same value sent from my node.js code.
Now, to trigger my lambda function from my node.js code, I'm using the following code:
var AWS = require('aws-sdk');
// you shouldn't hardcode your keys in production! See http://docs.aws.amazon.com/AWSJavaScriptSDK/guide/node-configuring.html
AWS.config.update({accessKeyId: 'myaccessKeyId', secretAccessKey: 'mysecretAccessKey',region:'region',correctClockSkew: true});
var lambda = new AWS.Lambda({apiVersion: '2015-03-31'});
var params = {
FunctionName: 'myLambdaFunction', /* required */
Payload: 'true',
};
lambda.invoke(params, function(err, data) {
if (err) console.log(err, err.stack); // an error occurred
else console.log(data); // successful response
});
I am new to this lambda function concept, so can anyone help me by telling how to send data to the required lambda function from the above mentioned code? Using the above code I am able to trigger my lambda function and I am getting in my node app whatever I am printing in it's payload.
I am getting the following error, when I am using a custom string value ( other than 'true', 'false' or 'null') in payload and the api version which I am using is: apiVersion: '2015-03-31' , and the aws-sdk node module was recently installed, so I guess it's up-to-date.
Error message:
{ InvalidRequestContentException: Could not parse request body into json: Unrecognized token 'custom_data': was expecting ('true', 'false' or 'null')
at [Source: [B#7d2214ec; line: 1, column: 23]
at Object.extractError (/usr/lib/node_modules/aws-sdk/lib/protocol/json.js:43:27)
at Request.extractError (/usr/lib/node_modules/aws-sdk/lib/protocol/rest_json.js:37:8)
at Request.callListeners (/usr/lib/node_modules/aws-sdk/lib/sequential_executor.js:105:20)
at Request.emit (/usr/lib/node_modules/aws-sdk/lib/sequential_executor.js:77:10)
at Request.emit (/usr/lib/node_modules/aws-sdk/lib/request.js:668:14)
at Request.transition (/usr/lib/node_modules/aws-sdk/lib/request.js:22:10)
at AcceptorStateMachine.runTo (/usr/lib/node_modules/aws-sdk/lib/state_machine.js:14:12)
at /usr/lib/node_modules/aws-sdk/lib/state_machine.js:26:10
at Request.<anonymous> (/usr/lib/node_modules/aws-sdk/lib/request.js:38:9)
at Request.<anonymous> (/usr/lib/node_modules/aws-sdk/lib/request.js:670:12)
message: 'Could not parse request body into json: Unrecognized token \'custom_data\': was expecting (\'true\', \'false\' or \'null\')\n at [Source: [B#7d2214ec; line: 1, column: 23]',
code: 'InvalidRequestContentException',
time: 2017-01-16T16:48:38.514Z,
requestId: '3bee0e2c-dd39-11e6-9df3-5f7a24f73b9d',
statusCode: 400,
retryable: false,
retryDelay: 26.112914258191733 } 'InvalidRequestContentException: Could not parse request body into json: Unrecognized token \'custom_data\': was expecting (\'true\', \'false\' or \'null\')\n at [Source: [B#7d2214ec; line: 1, column: 23]\n at Object.extractError (/usr/lib/node_modules/aws-sdk/lib/protocol/json.js:43:27)\n at Request.extractError (/usr/lib/node_modules/aws-sdk/lib/protocol/rest_json.js:37:8)\n at Request.callListeners (/usr/lib/node_modules/aws-sdk/lib/sequential_executor.js:105:20)\n at Request.emit (/usr/lib/node_modules/aws-sdk/lib/sequential_executor.js:77:10)\n at Request.emit (/usr/lib/node_modules/aws-sdk/lib/request.js:668:14)\n at Request.transition (/usr/lib/node_modules/aws-sdk/lib/request.js:22:10)\n at AcceptorStateMachine.runTo (/usr/lib/node_modules/aws-sdk/lib/state_machine.js:14:12)\n at /usr/lib/node_modules/aws-sdk/lib/state_machine.js:26:10\n at Request.<anonymous> (/usr/lib/node_modules/aws-sdk/lib/request.js:38:9)\n at Request.<anonymous> (/usr/lib/node_modules/aws-sdk/lib/request.js:670:12)'
Kindly help.
To extend the the answer provided by Mark B, have the data that will be loaded into payload constructed as:
var data = {
key: value,
key: value,
key: value
}
var datapayload = JSON.stringify(data);
var datalambda= {
FunctionName: 'LambdaFunctionName',
InvocationType: 'RequestResponse',
Payload: datapayload,
LogType: 'None'
};
// create variable to hold data returned by the Lambda function
var returndata;
lambda.invoke(datalambda, function(error, data) {
if (error) {
console.log(error);
}
else {
returndata = JSON.parse(data.Payload);
var log = JSON.stringify(returndata);
console.log(log);
}
You are currently sending data to the Lambda function. The data you are sending is the string 'true'. You send data via the Payload property. From the documentation:
Payload — (Buffer, Typed Array, Blob, String)
JSON that you want to provide to your Lambda function as input.

Resources