AccessDenied Invalid date (should be seconds since epoch" /> AccessDenied Invalid date (should be seconds since epoch" /> AccessDenied Invalid date (should be seconds since epoch"/>

AWS Presigned URL Lambda to S3 Bucket - Invalid Date Epoch Error

24 views Asked by At

Getting a 403 forbibben:

<?xml version="1.0" encoding="UTF-8"?>
<Error>
    <Code>AccessDenied</Code>
    <Message>Invalid date (should be seconds since epoch): 1711412800\</Message>
    <RequestId>35SE9FRYXSRKAGW5</RequestId>
    <HostId>BzzCAT3EsosPc3MXi0S9XT5S5sWdt7RlaxV9wszSJdpZwGwXXfCDGtoYSH+I9FX92N0FK0+mUuU=</HostId>
</Error>

After attempting to send a file through my presigned URL. I can confirm the epoch time is 25 minutes in the future after creating.

The lambda function that creates the presign is (python):

import json
import boto3
import uuid
import time

def generate_unique_object_key(prefix='object-'):
    timestamp = int(time.time())
    unique_id = str(uuid.uuid4())
    object_key = f'{prefix}{timestamp}-{unique_id}'
    return object_key

def lambda_handler(event, context):
    # Define your S3 bucket name
    bucket_name = 'storyattach'

    # Generate unique object key
    object_key = generate_unique_object_key()

    # Generate presigned URL
    s3_client = boto3.client('s3')
    presigned_url = s3_client.generate_presigned_url(
        'put_object',
        Params={'Bucket': bucket_name, 'Key': object_key},
        ExpiresIn=1800  # URL expires in 1 hour (you can adjust this as needed)
    )

    # Return the presigned URL
    return {
        'statusCode': 200,
        'body': json.dumps({'presigned_url': presigned_url})
    }

It's result is

{
  "statusCode": 200,
  "body": "{\"presigned_url\": \"https://storyattach.s3.amazonaws.com/object-1711410997-f59b966a-7b58-4a60-9444-46c5d2ee7836?AWSAccessKeyId=ASIAV3FUWUD4KXMCGTCK&Signature=CSOo7fw%2F1mdEC3LmHWwRUOplB6Q%3D&x-amz-security-token=IQoJb3JpZ2luX2VjEID%2F%2F%2F%2F%2F%2F%2F%2F%2F%2FwEaCXVzLWVhc3QtMSJHMEUCIQDd9bTZs1xMRyXSAkFtlQiiDJq8RkADSvXudemy0P8ksgIgXjVrQePT6zfnYuj23iqhoBIi3sNDUqDtfnXQ36ZnMOMq%2BwIImf%2F%2F%2F%2F%2F%2F%2F%2F%2F%2FARAAGgw0MDE5NTg0MTI1MzYiDJbYNRz9LB7tAX7K0SrPAn1kZ0Gx%2BrHbOOE5iLhK%2F%2F9RsdxPsF14zWd8GvqnEWhLWylz%2FY9F%2BZN6RHUjtbm%2BHpPPjX2TaFI0R7%2FNI6OEFMwcKvBboaOo8AOHYokxYNeh81uOc%2FQugkoPzQz6p5%2BCsg1tGFHoHyt5jK10GEZOaidNwyNMo5PDkisbFmlH7r9ruGUfCTiMBvp4y8PG%2FQI%2BEns6FVjGj1vyWc1MVQKcQ7jEPMWtjhktV8m3YzofrKGbgn3VRu6ZxdpA%2FxVP5EGeuTlSXsd0%2B8XohQ9sqNdJ0WYMVBsU9FuaxEgrRlmB%2Bt8dpXHg2jMiTZM5UnIAKPRskkUPsBU73eJ1X4Ca1bIcW0s9ciOOhLIt5cX4CZr4EXeigqTx7MIfh7xYUQ0W0zxQb%2Btc%2Fu0cXbOzaqPHsee%2BI6cjYAEFAN6t8mGLAd4XTTqNwv0jdczcZVLoeqiiskD6MLWeiLAGOp4BAIReG7hlkWrPdHVsEOSZTjMHhVjb674NC3ssdWuyJxEVkD0JUmE8%2FYZtuYE7MW7cFq8ypWkoJnhXqR1hyReo%2BKA7lt9Db19nroqY8ehZeyrOsaxkNUNJYyQsVS0zWaurRY3hSvIqhk1hNEpeFQ8pwZIecY%2B5emDSpyGZfoF15yKrZygJrXKuHwg8goBpM59ETUNYWGLoSaIclRMhC5E%3D&Expires=1711412800\"}"
}

I allowed the Lambda function to have full access to the s3 bucket. After getting the presigned URL I'm taking it over to postman and gettting the above error no matter what I change.

I also tried creating a fresh bucket and same thing happened. Although I might have repeated the same mistake, really not sure.

Any answers or wild guesses would be helpful

1

There are 1 answers

0
ChaseLowen On

Comment above was correct. I included an extra \ on the back of my presign...many many times. I wanted to provide an additonal answer, as my original presign had an issue. I needed to include a file extention in the key and a contentType in the parameters. Working version is below (working with text files but could be changed to any file type or a variable)

import json
import boto3
import uuid
import time

def generate_unique_object_key(prefix='trial-', file_extension='.txt'):
    # You can modify the file_extension parameter as per your requirement
    fixed_key = 'fixed-key-for-testing'
    object_key = f'{prefix}{fixed_key}{file_extension}'
    return object_key


def lambda_handler(event, context):
    # Define your S3 bucket name
    bucket_name = 'storyattach'

    # Generate unique object key
    object_key = generate_unique_object_key()

    # Generate presigned URL
    s3_client = boto3.client('s3')
    presigned_url = s3_client.generate_presigned_url(
        'put_object',
        Params={
            'Bucket': bucket_name,
            'Key': object_key,
            'ContentType': 'text/plain'
        },
        ExpiresIn=1800
    )


    # Return the presigned URL
    return {
        'statusCode': 200,
        'body': json.dumps({'presigned_url': presigned_url})
    }