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?

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
답변함 한 달 전
  • 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.

로그인하지 않았습니다. 로그인해야 답변을 게시할 수 있습니다.

좋은 답변은 질문에 명확하게 답하고 건설적인 피드백을 제공하며 질문자의 전문적인 성장을 장려합니다.

질문 답변하기에 대한 가이드라인

관련 콘텐츠