Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AWS S3 RoutingRule with wildcard condition

I have a set of user folders with the same folder structure, but different content, stored in S3.

/user/userA/application/folder/structure/file.xml
/user/userB/application/folder/structure/file.xml

I would like to redirect the user to a fallback folder, (with fallback content) if the s3 folder structure don't exist for that user yet.

/user-fallback/application/folder/structure/file.xml

I've tried to add a wildcard parameter to my Redirection Rules, but S3 is reading * as a literal

<RoutingRules>
    <RoutingRule>
        <Condition>
            <KeyPrefixEquals>user/*/</KeyPrefixEquals>
            <HttpErrorCodeReturnedEquals>404</HttpErrorCodeReturnedEquals>
        </Condition>
        <Redirect>
            <ReplaceKeyPrefixWith>user-fallback/</ReplaceKeyPrefixWith>
        </Redirect>
    </RoutingRule>
</RoutingRules>

So only urls starting with /user/*/ are being correctly redirected to the user-fallback folder structure.

I have more than 20 users, so creating individual RoutingRules wouldn't work either (S3 has a routing rule limit)

Any ideas?

like image 213
Jason Kulatunga Avatar asked Jul 10 '14 20:07

Jason Kulatunga


People also ask

What is wildcard in AWS?

In this example, wildcards are used in aws:userid to include all names that are passed by the calling process. For example, the wildcards are used for an application, service, or instance ID when calls are made to obtain temporary credentials. For more information, see Information available in all requests.

How do I restrict Amazon S3 Bucket access to a specific IAM role?

You can use the NotPrincipal element of an IAM or S3 bucket policy to limit resource access to a specific set of users. This element allows you to block all users who are not defined in its value array, even if they have an Allow in their own IAM user policies.


1 Answers

This should work:

<RoutingRules>
    <RoutingRule>
        <Condition>
            <KeyPrefixEquals>user</KeyPrefixEquals>
            <HttpErrorCodeReturnedEquals>404</HttpErrorCodeReturnedEquals>
        </Condition>
        <Redirect>
            <ReplaceKeyPrefixWith>user-fallback/</ReplaceKeyPrefixWith>
        </Redirect>
    </RoutingRule>
</RoutingRules>
like image 162
Greg Sansom Avatar answered Oct 04 '22 01:10

Greg Sansom