The ESP32 MCU doesn't have as much driving power on its pins as Arduino UNO's 328P MCU. If you wish to use a buzzer, you need to drive it with some transistors. Even if you don't want transistors, you should use some resistors such as 500 ohm in series with the buzzer. Directly connecting the buzzer to an IO pin may cause damage to the pin. I have a similar looking buzzer. Whenever I turn it on, there is the order of hundreds of mA of current through my circuit. If you disconnect that buzzer and run your code without modification and it runs without reboot, you know it's the buzzer. Adding the resistor will make the sound very soft. Best is to use a transistor to drive it. Better than best is if you are already familiar with transistors and use two transistors and drive the buzzer with 5V.