Skip to content

Apigateway S3 Proxy Integration -- How to handle keys that don't exist?

0

Goal

I have set up an apigateway rest api that has a s3 proxy using an aws integration.

Currently it is mostly working.

Problem

If I pass in a path that is not a key in the s3 bucket, I get an xml page that displays xml with a NoSuchKey error code. For context, this s3 bucket is set up for static web hosting & it does have an index and error document specified, so ideally the index.html page should be returned rather than the error code xml.

Questions

  1. Is it possible to return a default html page when aws integration response is a 404 error?
  2. Do I need to change anything in the aws integration settings to get the s3 bucket to return it's error page instead of the nosuchkey xml?
  3. Is there any resources/ recommendations for how to handle 404 errors with s3 integrations?
1 Answer
0

Yes, it is possible to return a default HTML page when the AWS integration response is a 404 error. To do this, you will need to set up an error handling mechanism in your API Gateway REST API.

One way to handle 404 errors is to use an HTTP integration with a mock integration to return a default HTML page. To do this, you can create a mock integration in your API Gateway REST API and specify an HTTP status code of 404 and a response body that contains the HTML for the default page that you want to return.

Then, you can set up a catch-all route in your API Gateway REST API that routes all requests to the mock integration. This catch-all route can be set up using a wildcard path parameter, such as {proxy+}. This will catch any request that does not match any of the other routes in your API, including requests for non-existent keys in your S3 bucket.

Alternatively, you can configure your S3 bucket to return a default page for 404 errors. To do this, you can specify an error document in the S3 bucket's static website hosting configuration. When a request is made for a non-existent key, S3 will return the error document instead of the NoSuchKey error code.

answered 3 years ago
  • I should have clarified, but the way that the apigateway aws integration s3 proxy works is:

    • I have the following route structure:
      • {default} - GET method is an aws integration that gets the s3 object from a bucket using the path override bucketname/default
      • {default}/{proxy+} - GET method is an aws integration that gets the s3 object from a bucket using the path override bucketname/default/proxy

    I am confused by your answer-- if I got a 404 error from the aws integration, how would I then route back to the mock http integration to return a 404 response?

    Also as mentioned in my post, I have already set the error document in the s3 buckets static website hosting config, but I still get the nosuchkey xml instead of the error page. Is there anything else that I would need to configure on the s3 bucket to ensure that the bucket returns the error page instead?

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.