6

I have the following CORS configuration for S3 in order to use one of my buckets as a static website hosting:

<?xml version="1.0" encoding="UTF-8"?>
<CORSConfiguration xmlns="http://s3.amazonaws.com/doc/2006-03-01/">
    <CORSRule>
        <AllowedOrigin>*</AllowedOrigin>
        <AllowedMethod>GET</AllowedMethod>
        <AllowedMethod>POST</AllowedMethod>
        <MaxAgeSeconds>3000</MaxAgeSeconds>
    </CORSRule>
</CORSConfiguration>

Then I have the following Edit Redirection Rules:

<RoutingRules>
    <RoutingRule>
    <Condition>
        <KeyPrefixEquals>abc</KeyPrefixEquals>
    </Condition>
    <Redirect>
        <HostName>myec2instance.com</HostName>
    </Redirect>
</RoutingRule>

What I want to do is when S3 receives a POST to /abc redirects the request and request body to my ec2 instance. The redirection rule is working properly (I was able to test this by switching POST to a GET request) but for any reason S3 is returning HTTPResponse 405 when the request is a POST. Any ideas why?

user1126167
  • 606
  • 1
  • 6
  • 11
  • The references I can find to this error following a redirect seem to indicate that it happens if you redirect either to a folder instead of a file, or a folder that doesn't have a default document set. Is it either of those? –  Feb 19 '15 at 00:55
  • Hi @monkeymatrix thanks for your answer! My problem is not the redirect operation (at least I think so). The problem for me is that S3 as a static website host does not allow me to send a HTTP POST from the javascript that I have inside the webpage. What I'm trying to accomplish is to create a landing page where users can leave their contact then the webpage send their contact to S3 by POST and finally S3 redirects to an ec2 instance that saves the json data sent. – user1126167 Feb 19 '15 at 09:51

2 Answers2

12

This isn't CORS related -- it's S3 itself. The S3 website endpoints are only equipped for GET and HEAD. Anything else should be denied before the redirection rules are checked.

Website Endpoint

Supports only GET and HEAD requests on objects.

http://docs.aws.amazon.com/AmazonS3/latest/dev/WebsiteEndpoints.html

Community
  • 1
  • 1
Michael - sqlbot
  • 139,456
  • 21
  • 252
  • 328
  • So does this mean you can't use S3 website hosting to forward POST requests? – Jake Mar 17 '15 at 03:57
  • @Jake - correct... although I'm not sure why you'd want to "forward" (redirect?) a `POST` because that would mean re-sending the request body to the new location, costing time and bandwidth at the browser side. You may want to create a new question, here, describing your use case and what problem you're trying to solve. `POST` requests to the S3 web site endpoints, even in the presence of a matching redirect rule, is met with `405 Method Not Allowed`. – Michael - sqlbot Mar 17 '15 at 10:21
  • Thanks Michael. I'm hitting a backend REST API from a frontend app hosted on S3. I realized the right way to do it is hitting the backend directly with CORS, without needing a redirect. – Jake Mar 18 '15 at 17:06
  • U can use API gateway, Lambda. – zuyao88 Dec 27 '18 at 04:55
1

If you want to use POST request to S3, you need to use root object in your browser by creating POSTPolicy to get S3 authentication

you can refer Browser-Based Upload using HTTP POST

Krishna Vyas
  • 860
  • 5
  • 21
Sasra S
  • 21
  • 1