API Gateway Integration with S3 || Problem in setting Path Parameter

0

Problem statement:

I am working on integrating S3 bucket with API Gateway to put objects in S3. The set-up i have:

I have created the {proxy+} to create folder like structure in s3. In Integration Request setting i have configured AWS Region, AWS Service (S3), Http Method (PUT), Action Type: Use path override Path override : {bucket}/{object} Execution role: arn:aws:iam::12345667987:role/api-gateway-upload-to-s3

I am facing the challenge in mapping the {object} value. I know i can map it directly using URL Path Parameter setting. But the problem is I'll have to compute this value using method.request.path.param and method.request.header.param . for instance, my path parameter is "/v1/document/12345678987/page" and my request header is Filename : Samplefile.txt. So, after computation my {object} value should look like this /v1/document/12345678987/page/Samplefile.txt.

Is there a way to achieve this using either URL Path Parameter or Mapping Templates ??

I am trying to achieve this using mapping template:

#set($path = "$input.params('objectkey')")  # Extract DocumentId from path
#set($resourceName = "$input.headers('Filename')")  # Extract header parameter

#set($computedObjectName = "$path/$resourceName")  # Concatenate the object name

{
  "bucket": "s2notificationeventtriggerlambdafunction",
  "key": "$computedObjectName"
}
1 Answer
0

Yes, you can achieve this using a mapping template in the Integration Request settings of your API Gateway. Your approach is on the right track, but needs a few adjustments to work correctly.

Here's an improved version of the mapping template that should solve your problem:

#set($path = $input.params().path.get('proxy'))
#set($filename = $input.params().header.get('Filename'))
#set($computedObjectName = "$path/$filename")

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

This template does the following:

  1. It extracts the full path from the 'proxy' parameter, which in your case would be "v1/document/12345678987/page".
  2. It gets the filename from the 'Filename' header.
  3. It combines these two to create the computed object name.

To use this template:

  1. In the Integration Request settings, set the "Content-Type" header to "application/json".
  2. In the "Mapping Templates" section, add a mapping for "application/json" content-type and paste the above template.

This approach allows you to compute the object key dynamically based on both the path parameters and the header, which meets your requirement of having the final object key as "/v1/document/12345678987/page/Samplefile.txt".

Remember to ensure that your IAM role (api-gateway-upload-to-s3) has the necessary permissions to put objects in the specified S3 bucket.

Also, make sure your API method is configured to receive the 'Filename' header, and that your client is sending this header with the request.

This solution provides flexibility and allows you to construct complex object keys based on various inputs from the API request.
Sources
Integration - Amazon API Gateway
Basic tasks of an API integration request - Amazon API Gateway
PutIntegration - Amazon API Gateway
x-amazon-apigateway-integration.requestParameters object - Amazon API Gateway
Tutorial: Create a REST API as an Amazon S3 proxy - Amazon API Gateway

profile picture
answered 2 months ago
  • Not Working, getting an error.

    Updated the Mapping Template beacuse my proxy path is {objectkey+} and header is X-IntegrationServer-Resource-Name

    #set($path = $input.params().path.get('objectkey'))
    #set($filename = $input.params().header.get('X-IntegrationServer-Resource-Name'))
    #set($computedObjectName = "$path/$filename")
    
    {
        "bucket": "s2notificationeventtriggerlambdafunction",
        "key": "$computedObjectName"
    }
    

    The error i am getting: (3a3959d6-a182-446c-930b-bb4192223872) Execution failed: URI/URL syntax error encountered in signing process (3a3959d6-a182-446c-930b-bb4192223872) Execution failed due to configuration error: Illegal character in path at index 35: https://s3.us-west-2.amazonaws.com/{bucket}/{key}

    Can you please help what could be wrong?

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