How to integrate API Gateway to S3 Service using Action Type as Action Name PutObject

0

I am trying to integrate API Gateway to S3 using ActionType.

The resource for api gateway is /upload. I want to know how to set up the integration request. Integration Uri and how to pass bucket and object information in integration request.

2 回答
1

You can try these steps:

  1. Create an IAM Role for the API Gateway:

    • Create an IAM role that grants the necessary permissions for API Gateway to access the S3 bucket.
    • Attach a policy that allows the s3:PutObject action on the desired S3 bucket.
  2. Configure the API Gateway Integration Request:

    • In the API Gateway console, create a new API or navigate to an existing one.
    • Add a new resource with the path /upload.
    • Create a new method for the /upload resource, and select POST as the HTTP method.
    • In the method setup, choose Integration Type as AWS.
    • For AWS Service, select S3.
    • For AWS Action, select PutObject.
    • In the Integration Request section:
      • Integration URI: Set the Integration URI to the following format: arn:aws:apigateway:{region}:s3:path/{bucket}/{object_key}
        • Replace {region} with the AWS region where your S3 bucket is located.
        • Replace {bucket} with the name of your S3 bucket.
        • {object_key} will be the placeholder for the object key that you'll pass from the client.
      • Mapping Templates: Add a mapping template for the application/json content type. In the template, you can define the object key that will be used for the PutObject action. For example:
        {
          "key": "$input.path('$.objectKey')"
        }
        This maps the objectKey property from the client's request body to the key parameter in the PutObject action.
  3. Configure the API Gateway Method Request:

    • In the Method Request section, add a new Request Body parameter with the name objectKey (or any other name you prefer).
    • Set the Model to Method Request and the Required field to true.
  4. Deploy the API Gateway:

    • After configuring the integration, you need to deploy the API Gateway.
    • In the API Gateway console, go to the Stages section and create a new stage (e.g., dev, prod) or use an existing one.
    • Deploy the API to the selected stage.

Now, when a client sends a POST request to the /upload resource, the API Gateway will forward the request to the S3 service, using the PutObject action and the objectKey value from the request body.

Here's an example of how the client can make a request to the API Gateway:

{
  "objectKey": "example-file.txt"
}

The API Gateway will then use the provided objectKey value to construct the PutObject request and upload the file to the specified S3 bucket.

profile picture
专家
已回答 1 个月前
profile picture
专家
已审核 1 个月前
  • HI @Osvaldo. Thanks for your answer. In the documentation there is a reference to use different URI for action based S3 services.

    arn:aws:apigateway:api-region:s3:action/GetBucket

    https://docs.aws.amazon.com/apigateway/latest/developerguide/integration-request-basic-setup.html

    Is it not possible to use this URI for PutObject ?

  • I think it is possible to use the URI. For the PutObject action, the integration request URI (action-based integration) in API Gateway would look like this:

    arn:aws:apigateway:{api-region}:s3:action/PutObject
    

    Since the bucket name and object key are not in the URI, you need to specify them in the integration request body, along with any other required parameters for the PutObject action. For example:

    {
      "Bucket": "{bucket}",
      "Key": "{objectKey}",
      "Body": "{objectData}"
    }

    You can use a mapping template to create a JSON payload that includes these details.

0

Hi,

There is a great knowledge article to help you upload files to S3 using API Gateway, I am sure, you would find an approach from that.

Hope this helps!

Thanks, Rama

profile pictureAWS
Rama
已回答 1 个月前
  • Hi @Rama

    That article uses path override approach creating resource like \{folder}\{item}. This is mentioned in AWS documentation tutorial and in all the examples in repost and internet. But there is no single example using S3 for with action type as action name = PutObject. In documentation it is mentioned that it is possible but no example given.

您未登录。 登录 发布回答。

一个好的回答可以清楚地解答问题和提供建设性反馈,并能促进提问者的职业发展。

回答问题的准则