How do I use API Gateway to create files, folders, and subfolders in Amazon S3?

3 minute read
2

I want to use Amazon API Gateway to create files, folders, and subfolders in an Amazon Simple Storage Service (Amazon S3) bucket. I don't want to use an intermediate AWS Lambda function.

Short description

To reduce the latency of API requests, use API Gateway without a Lambda function to create Amazon S3 bucket resources. This method can also reduce costs and make your system easier to maintain and troubleshoot.

To create resources, you must have an API Gateway with the required permissions and settings.

Resolution

Create a REST API

Create a REST API in Amazon API Gateway.

Create a resource

To create a resource in API Gateway, complete the following steps:

  1. Open the API Gateway console, and then choose your API.
  2. In the Resources section, choose Create Resource.
  3. For Resource path, choose /.
  4. For Resource name, enter {rest}.
  5. Choose Create resource.

Integrate your API to Amazon S3

To create a method to integrate your API with Amazon S3, complete the following steps:

  1. Open the API Gateway console, and then choose your resource.
  2. Choose Create method.
  3. For Method type, choose PUT.
  4. For Integration type, choose AWS Service.
  5. For AWS Service, choose Simple Storage Service (S3).
  6. For HTTP method, choose PUT.
  7. (Optional) Configure the other settings.
  8. Choose Create method.

Define URL path parameter

To add a path parameter to your resource, complete the following steps:

  1. Open the API Gateway console.
  2. In the Resource section, select the PUT method.
  3. On the Integration request tab, choose Edit.
  4. In the URL Path Parameters section, choose Add Path Parameter.
  5. For Name, enter rest.
  6. For Mapped from, enter method.request.path.rest.
  7. (Optional) Configure the other caching settings.
  8. Choose Save.

Test your API Gateway

To test if your API Gateway can successfully create sources in your bucket, complete the following steps: 

  1. Open the API Gateway console.
  2. In the Resource section, select your API, and then select your PUT method.
  3. In the rest field, enter a name for one of the following. The rest field might be titled differently based on the name of your method.
    To create a file in your Amazon S3 bucket, enter the file name.
    To create a folder in your Amazon S3 bucket, enter a folder name that ends with %2F. For example, to create a folder named testfolder, enter testfolder%2F.
    To create a subfolder in your Amazon S3 bucket, enter the folder name, and then the subfolder name. For example, enter testfolder%2Ftestfolder1%2F to create a subfolder within a folder named testfolder.
  4. Choose Test.

A successful test displays an "HTTP 200 OK" response.

Related information

What is Amazon API Gateway?

What is Amazon S3?

AWS OFFICIAL
AWS OFFICIALUpdated 3 months ago