DEV Community

Cover image for Failed to convert ‘Body’ to string S3.InvalidContent arn:aws:states:::aws-sdk:s3:getObject in step function
Olawale Adepoju for AWS Community Builders

Posted on • Originally published at dev.classmethod.jp

Failed to convert ‘Body’ to string S3.InvalidContent arn:aws:states:::aws-sdk:s3:getObject in step function

Problem

I wrote a step function state machine that uses the AWS SDK to retrieve a file from S3. I input the bucket name and file name directly as the parameter.

{
"Comment": "A description of my state machine",
"StartAt": "GetAudioFromS3",
"States": {
"GetAudioFromS3": {
"Type": "Task",
"Resource": "arn:aws:states:::aws-sdk:s3:getObject"
"Parameters": {
"Bucket": "BucketName",
"Key": "FileName.wav"
},
"End": true
}
}
}
Enter fullscreen mode Exit fullscreen mode

When I run the task to get the file from S3, it fails with

S3.InvalidContent in step: Get Audio from S3

CAUSE

Failed to convert 'Body' to string After expanding and trying to check the root cause I got this TaskFailed event.

{
"resourceType": "aws-sdk:s3",
"resource": "getObject",
"error": "S3.InvalidContent",
"cause": "Failed to convert 'Body' to string"
}
Enter fullscreen mode Exit fullscreen mode

How I fixed it

I change the format of my step-function code and called the bucket using variable.

{
"Comment": "A description of my state machine",
"StartAt": "GetAudioFromS3",
"States": {
"GetAudioFromS3": {
"Type": "Task",
"Resource": "arn:aws:states:::aws-sdk:s3:getObject"
"Parameters": {
"Bucket.$": "$.S3BucketName",
"Key.$": "$.InputKey"
},
"End": true
}
}
}
Enter fullscreen mode Exit fullscreen mode

This is the JSON definition of variable

{
"S3BucketName": "<name of the bucket where the original file is>",
"InputKey": "<name of the original file>"
}
Enter fullscreen mode Exit fullscreen mode

When you are about to start the execution of the step function, it gives an option of inputting the JSON statement.

Image description

NOTE: This requires inputting every time you want to run an execution, so I used lambda for automation. Once the S3 bucket details are used in lambda.

Image description

{
"Comment": "A State Machine that process a video file",
"StartAt": "GetInput",
"States": {
"GetInput": {
"Type": "Task",
"Resource": "arn:aws:lambda:ap-northeast-1:********:function:FunctionName",
"Next": "GetAudioFromS3"
},
"GetAudioFromS3": {
"Type": "Task",
"Resource": "arn:aws:states:::aws-sdk:s3:getObject"
"Parameters": {
"Bucket.$": "$.S3BucketName",
"Key.$": "$.InputKey"
},
"End": true
}
}
}
Enter fullscreen mode Exit fullscreen mode

Top comments (0)