China signed a long-awaited, 30-year deal Wednesday to buy Russian natural gas worth $400 billion in a financial and diplomatic boost to diplomatically isolated President Vladimir Putin.

Negotiations on the price for the gas had continued into the final hours of a two-day visit by Putin to China, during which the two sides had said they hoped to sign a deal. Putin was in Shanghai for an Asian security conference.

The agreement calls for Russia's government-controlled Gazprom to supply state-owned China National Petroleum Corp. with 38 billion cubic meters of gas annually, Gazprom spokesman Sergey Kupriyanov told The Associated Press.

Gazprom CEO Alexei Miller told Russian news agencies that the contract was worth a total of $400 billion.

CNPC announced it had signed a deal but it gave no details.

The sale will help Russia to diversify its markets for gas exports, which now go mostly to Europe. China gets supplies that could help to ease gas shortages in the world's second-largest economy.

China and Russia have been negotiating the deal for more than a decade but had been hung up over the gas price.