Is there any way to preprocess Amazon S3 request by executing some logic in some programming logic (e. g. if I need to restrict access to resources by some dynamic condition)?
There is a very limited set of logic you can apply with S3's Access Policy Language. If you can't accomplish what you need using that, you may need to set up your own "front end" server in EC2 to do your business logic.
Another option would be to enable CloudFront on your S3 bucket, and then use the dynamic signed URLs feature. Some other system would have to generate the dynamic signed URL for people to follow, with CloudFront granting or denying access based on the validity of the dynamic signed URL.