Cannot execute preview generation

When I try to run
sudo php /var/www/html/occ preview:pre-generate

I get the response:

Console has to be executed with the user that owns the file config/config.php
Current user: root
Owner of config.php: www-data

But it is not allowed to execute things as www-data by doing sudo su - www-data because of security reasons.

Am I doing something completely wrong?