Police in upstate New York shot and critically wounded a man who attacked a female cop with a baseball bat, according to officials.

The Buffalo shooting comes on the heels of unrest in nearby Rochester over the police-custody homicide of a mentally unstable man that cops had been called to check on.

Several Buffalo cops — including a specialist crisis-intervention officer a — were called to a “mental health crisis” just before 3 p.m. Saturday as the unidentified man threatened people in the street, the force said.

Several officers followed the “agitated” man and repeatedly pleaded with him to put down the baseball bat as he “made a striking motion toward officers,” police spokesman Jeffrey Rinaldo told a press conference, according to The Buffalo News.

Officers “attempted to deploy pepper spray on at least a couple of occasions” but it “did not appear to have any effect,” Rinaldo said.

Video posted to social media showed officers, some with weapons drawn, chasing after the man, with a woman in a car shouting, “Tackle him — don’t shoot him, though!”

After he was trailed for five blocks, he finally swung at a female officer and hit her with the bat, the police spokesperson said.

“When he attempted to swing the bat a second time at her, that’s when her partner did fire two rounds, striking the suspect one time in the abdomen,” Rinaldo told the press conference, according to the paper.

The confrontation was also caught in video by a couple in a vehicle who watched in horror from afar.

“Oh he’s swinging at cops!” a woman was heard gasping — seconds before two gunshots went off.

“They f—ing shot him — holy s–t,” a man with her said, as the woman said that “he was swinging at them.”

“Oh yeah they shot his ass,” the man said as the camera zoomed in to show several officers standing around a man on the ground.

The female officer “sustained a small injury to her arm” but did not suffer any serious injuries, Rinaldo said.

After officers on the scene rendered aid, the shot suspect was taken to Erie County Medical Center, where he was listed Saturday evening in critical but stable condition, police said.

His condition was upgraded to stable on Sunday afternoon.

Although he was not identified, he was a 60-year-old homeless man who was a regular at Buffalo City Mission who took the bat with him everywhere, sources told The Buffalo News.

He was “very quiet, very peaceful,” those who interacted with him at the mission told the paper.

Officers wore body cameras, with footage now being reviewed, the spokesman said.

“Part of our investigation will be to determine what brought the officers there; how they handled it; attempts at de-escalation and what led to the eventual shooting of the suspect,” Rinaldo said.

News of the shooting sparked a small protest, with a group of about two dozen, including several who have taken part in recent protests against police brutality, gathering at the scene, the Buffalo paper said.

During a press conference outside City Hall Sunday, De’Jon Hall, a member of Free the People Western New York, called for Buffalo’s mayor, Byron Brown, and police Commissioner, Byron Lockwood, to both resign.

“I don’t see how that can be viewed as de-escalation,” Hall said of the officers’ “very armed and scary” presence caught on videos.

“If that is what Crisis Intervention Team members are taught, then we have a problem.”

John Evans, president of the Buffalo Police Benevolent Association, told the paper that they believed “officers followed departmental protocols.”

“This type of interaction is extremely dangerous and can quickly turn into a life or death situation,” Evans said, noting how the officers had “pleaded with the individual to drop the bat” before he attacked the female cop.

With Post wires