GET S3 Presigned URL SignatureDoesNotMatch

0

Hi, I am trying to make some side projects to generate url for an object using lambda.

This is my python code in my lambda :


BUCKET_NAME = os.environ['BUCKET_NAME'] 
s3_client = boto3.client('s3', region_name='eu-central-1')

def lambda_handler(event, context):
    object_key = json.loads(event['body'])['key']
    print(generate_presigned_url(BUCKET_NAME, object_key, 3600))

def generate_presigned_url(bucket_name, object_name, expiration=3600):
    """
    Generate a presigned URL to share an S3 object.

    :param bucket_name: string, name of the bucket.
    :param object_name: string, name of the object.
    :param expiration: Time in seconds for the presigned URL to remain valid, default is 1 hour.
    :return: Presigned URL as string, None if error.
    """
    # Create an S3 client
    
    try:
        response = s3_client.generate_presigned_url('get_object',
                                                    Params={'Bucket': bucket_name,
                                                            'Key': object_name},
                                                    ExpiresIn=expiration)
    except ClientError as e:
        print(f"Error generating presigned URL: {e}")
        return None

    return response

And the signature gives error: Firstly it generates a url that redirects me and adds the region : https://bucket.s3.amazonaws.com/object_key to https://bucket.eu-central-1.s3.amazonaws.com/object_key. And responds with : <Code>SignatureDoesNotMatch</Code> <Message>The request signature we calculated does not match the signature you provided. Check your key and signing method.</Message>

I did this using my aws iam user , with boto3 sdk and aws cli. With aws cli works, with boto3 s3 client does not work!

2 days ago did not work , today it worked . I redeployed the same infrastructure with aws cdk. And it does not work!!!

  • What region is the bucket in? eu-central-1?

Ronaldo
已提問 2 個月前檢視次數 340 次
1 個回答
1

I am back, i retried it ! And now it goes successful , it generates the presigned url ,and it gets status 200 , not redirected to s3.eu-central-1. And i did nothing , i just waited some days!! What is going on here??

Ronaldo
已回答 1 個月前
  • Maybe I'm wrong, but that could have been cached at various levels like API Gateway, CDN and local browser cache that might cause the behavior to appear inconsistent until the cache is refreshed.

您尚未登入。 登入 去張貼答案。

一個好的回答可以清楚地回答問題並提供建設性的意見回饋,同時有助於提問者的專業成長。

回答問題指南