The error Illegal character in path at index 35: https://s3.us-west-2.amazonaws.com/{bucket}/{key}

0

Problem Statement:

I am trying to integrate AWS Service S3 with API Gateway to put objects to S3, the object i am trying to put in s3 is a document of any type(e.g., .txt, .pdf etc etc). In the Integration request, i have defined my path override as {bucket}/{key} Content- handling is Passthrough I am trying to map the bucket and key variable of integration request url using Mapping template. Mapping template configuration: Content-type : application/octet-stream Mapping Template :

#set($path = $input.params().path.get('objectkey'))
#set($filename = $input.params().header.get('X-IntegrationServer-Resource-Name'))
#set($computedObjectName = "$path/$filename")
#set($encodedObjectKey = $util.urlEncode($computedObjectName))

{
    "bucket": "s2notificationeventtriggerlambdafunction",
    "key": "$encodedObjectKey"
}

But i am getting an error - The error Illegal character in path at index 35: https://s3.us-west-2.amazonaws.com/{bucket}/{key} which suggests that the placeholders {bucket} and {key} are not being replaced with actual values. Instead, they remain as literal text, resulting in an invalid URL path for the S3 request.

Can someone please suggests what could be the possible reason causing this issue?

2 Answers
1

Hi,

You may have special characters in the content of variable for the object key that may create such issue leading to an illegal URL

If it's the case, you may need to escape those chars as explained on: https://docs.aws.amazon.com/apigateway/latest/developerguide/api-gateway-mapping-template-reference.html with the example below

{
    "name" : "$input.params('name')",
    "body" : $util.escapeJavaScript($input.json('$'))
}

I'd also advise to log your input in details via logging templates to easily spot those errors when they happen during production: see https://docs.aws.amazon.com/apigateway/latest/developerguide/api-gateway-mapping-template-reference.html#context-variable-reference

Best,

Didier

profile pictureAWS
EXPERT
answered 2 months ago
0

The error you're encountering suggests that the placeholders {bucket} and {key} in your S3 URL are not being properly replaced with actual values. This issue is likely caused by a problem in how the integration request is set up or how the mapping template is being applied. Here are a few potential reasons and solutions to consider:

  1. Integration Request Configuration: Make sure that in your API Gateway integration request, you have correctly set up the path parameters. The path override should be something like /{bucket}/{key} rather than {bucket}/{key}. The leading slash is important.

  2. Mapping Template: Your mapping template looks mostly correct, but there might be an issue with how it's being applied. Ensure that the Content-Type header in your request matches exactly what you've set in the mapping template configuration (application/octet-stream in this case).

  3. Variable Names: Verify that the variable names in your mapping template ($path, $filename, etc.) match exactly with the names of the path parameters and headers you're sending in the request.

  4. URL Encoding: While you're using $util.urlEncode() for the key, make sure this encoding is being applied correctly. You might want to try encoding the entire path, including the bucket name.

  5. API Gateway Stage Variables: If you're using stage variables for the bucket name, ensure they are properly set and referenced.

  6. Permissions: Although not directly related to this specific error, ensure that your API Gateway has the necessary permissions to interact with the S3 bucket.

To troubleshoot, you can try the following:

  1. Use $input.json('The error you're encountering suggests that the placeholders {bucket} and {key} in your S3 URL are not being properly replaced with actual values. This issue is likely caused by a problem in how the integration request is set up or how the mapping template is being applied. Here are a few potential reasons and solutions to consider:

  2. Integration Request Configuration: Make sure that in your API Gateway integration request, you have correctly set up the path parameters. The path override should be something like /{bucket}/{key} rather than {bucket}/{key}. The leading slash is important.

  3. Mapping Template: Your mapping template looks mostly correct, but there might be an issue with how it's being applied. Ensure that the Content-Type header in your request matches exactly what you've set in the mapping template configuration (application/octet-stream in this case).

  4. Variable Names: Verify that the variable names in your mapping template ($path, $filename, etc.) match exactly with the names of the path parameters and headers you're sending in the request.

  5. URL Encoding: While you're using $util.urlEncode() for the key, make sure this encoding is being applied correctly. You might want to try encoding the entire path, including the bucket name.

  6. API Gateway Stage Variables: If you're using stage variables for the bucket name, ensure they are properly set and referenced.

  7. Permissions: Although not directly related to this specific error, ensure that your API Gateway has the necessary permissions to interact with the S3 bucket.

To troubleshoot, you can try the following:

  1. Use $input.json('$') in your mapping template to log the entire input, which can help you verify the incoming data.
  2. Double-check all your API Gateway settings, especially in the Integration Request section.
  3. Test your API using the API Gateway console to isolate whether the issue is with the API configuration or with how you're calling the API.

If the issue persists after checking these points, you may need to review your overall API Gateway configuration and how it's interacting with S3.#39;) in your mapping template to log the entire input, which can help you verify the incoming data. 2. Double-check all your API Gateway settings, especially in the Integration Request section. 3. Test your API using the API Gateway console to isolate whether the issue is with the API configuration or with how you're calling the API.

If the issue persists after checking these points, you may need to review your overall API Gateway configuration and how it's interacting with S3.
Sources
Configure CORS in Amazon S3 | AWS re:Post
CreateStage - Amazon API Gateway

profile picture
answered 2 months ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions