S3 presigned url doesn't work when generated with a lambda function

Viewed 1000

The following code, I got from this Boto3 S3 Example, works properly both if launched locally and from a lambda function executed with a call to the API Gateway:

   s3_client = boto3.client('s3')
   response = s3_client.generate_presigned_post("mybucket",
                                                "myfile.jpg"
                                                ExpiresIn=3600)

In both cases it returns a response like the following:

{
    "url": "https://mybucket.s3.amazonaws.com/",
    "fields": {
        "key": "myfile.jpg",
        "AWSAccessKeyId": "-omissis-",
        "policy": "eyJle...dfQ==",
        "signature": "ruboM...P3R4c="
    }
}

The only difference between the two is that the remote one (generated by the lambda function) includes also the field x-amz-security-token.

...
        "x-amz-security-token": "IQoJb...ZAQ==",
...

When I try to POST the file (using the html example reported in the page linked above or postman), everything works properly when I use the presigned url generated locally (whitout x-amz-security-token) but it fails when I try the one generated remotely.

It returns the following error message when x-amz-security-token is sent as form's field:

403 Forbidden

<?xml version="1.0" encoding="UTF-8"?>
<Error>
    <Code>InvalidAccessKeyId</Code>
    <Message>The AWS Access Key Id you provided does not exist in our records.</Message>
    <AWSAccessKeyId>-omissis-</AWSAccessKeyId>
    <RequestId>360...BE2</RequestId>
    <HostId>y04bl...DWhqY=</HostId>
</Error>

and the following as header's field:

403 Forbidden

<?xml version="1.0" encoding="UTF-8"?>
<Error>
    <Code>AccessDenied</Code>
    <Message>No AWSAccessKey was presented.</Message>
    <RequestId>A7592...6681</RequestId>
    <HostId>stc/+...eeUU=</HostId>
</Error>

How should I properly make the POST request when the presigned url includes x-amz-security-token?

0 Answers
Related