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?