The Spanish claimed the entire Western Hemisphere, as defined in the successive revisions of the Treaty of Tordesillas, as theirs. But they didn't expand northwestward out of Mexico at any rapid rate, leaving their theoretical claim on OTL California and points north for future generations.
It was the provocation of the Russian fur trading venture expanding out of Siberian coasts into Alaska that stirred the authorities of New Spain into taking belated action to concretely secure their claim northward. I'd have to look up just when and why they agreed to limit this to the OTL latitude of the current border between California and Oregon, but even foregoing claims on all points northward, the Russians looked to be in danger of coming uncomfortably far south, pretty soon, around the end of the 18th century.
One reason the Russians could move fast versus Spanish practices of expansion was that the Russians followed a practice similar to the French strategy of power projection; secure relations with local Native people and integrate them into a fur-gathering system via trade, rather than seek primarily to secure land for settlement on the English/British model. The Spanish strategy was mainly to locate highly developed and relatively rich Native civilizations and conquer them wholesale, then exploit them. It could be seen as being between the British and French/Russian models on the less rich peripheries, where the strategy was to bring in missionaries under army protection and by a combination of persuasion and coercion secure the religious conversion of the natives and thus their political subordination too.
However the mission effort sought to expand fairly rapidly after centuries of neglect, and worked out in practice to pretty nearly exterminate the Native Californian population via the Missions, where they died in droves. This however would serve their political purposes quite as well as gaining their loyalty, and far better than leaving the Natives alone to be recruited by the Russians.
This is why the timing of major efforts to explore California waited until the late 18th century, which sets the limit on how early Spanish authorities could possibly find out about the gold to be found on the west slopes of California's Sierra Nevada mountains.
Now, if the initial discovery were by Spanish expeditions sent to explore San Francisco Bay and secure its entrance in say the 1770s, perhaps the Spanish crown could keep control of gold mining. But even this is a long shot; ship crews and soldiers are not very well paid and not all deeply invested in the interests of their noble paymasters, and would be liable to gossip. This would draw attention among political rivals throughout Europe to consider whether they might seize this prize for themselves.
It would be quite difficult of course. I actually would guess that in the 1770s and "80s, aside from the Spanish the best situated to try to bid for control of San Francisco Bay would be the Dutch, specifically the Dutch East India Company, though the English Company was pulling up to parity at this time--DEIC aka "Jan Company" was based closer in Batavia, but the English could leapfrog them by seizing Manila, which they had done earlier in the 18th century. Neither is exactly close; perhaps a side effect is the conquest of Hawaii early on (Cook was just "discovering" the "Sandwich Islands" and news might not have worked its way from his return through Europe and back to Batavia or the English holdings in India, but the rival powers attempting to find a quick route across the Pacific might conceivably stumble on the archipelago before learning Cook had already been there).
Meanwhile the Spanish have bases as close as San Diego and larger, older ports farther south on the Mexican and Central American Pacific coast, plus those on the South American coast. If suitably alarmed, Spain has the advantage in having manpower and sea power that can preemptively be deployed to secure the Bay. I suppose the usual problems New Spain would have in recruiting New Spanish peasantry to settle a new Pueblo in the far north might be much alleviated by the rumor of gold lying around for the taking; once those rumors get out the real trick is to prevent wildcat immigration. I would think that the Spanish system had enough control of ships entering and leaving Pacific coast ports to impede such invasions by foreign powers or unauthorized colonization by Spanish subjects. An enlightened policy of permitting Spanish subjects to keep a major part of whatever gold they gather for themselves if they will hand over a portion of it to the authorities would enable the Viceroys to regulate the flow of immigration toward desirable levels--I am callously writing off the chances of Native Americans to come out of this in any good shape, though it is hard to beat how terrible their fate was OTL.
The upshot--as others note, the flow of gold into Spanish coffers would not be nearly as beneficial to Spanish finances as the lords would assume. The sheer mass of gold in private hands in the Spanish-occupied San Francisco Bay Area would not buy the commoner class settlers anywhere near what the same gold in hand in Europe would--but to these humble settlers, the fortune, reduced though it is, is clearly a huge gain, and would tend to buy a lot of stuff that the Spanish government might not think of as developmental necessities, but would nonetheless spur the growth of the Bay Area settlements.
Spain then does not gain tremendously more strength, but perhaps, recognizing the Bay Area as a valuable asset, might develop some major naval presence there and substantial fortifications; it would then be difficult for any rival power--Britain, DEI, Russian--to land counter-settlers or even rabid gold rushers. So Spain keeps control and the province grows rapidly, as the more tightly the Spanish seek to limit settlement from Mexico or any other part of the Spanish system, the more the regional resources will appeal to those allowed to settle and the more rapid the natural increase of these settlers would be.
If we assume Spanish gold does not butterfly things much, the basic causes of the ARW and the logic of its political protagonists lining up on one side or the other would be similar, so I see no reason for its outcomes to be much different, nor the French Revolution or rise of Napoleon Bonaparte. Therefore Spain's control of its colonies would be interrupted; there is little reason to think California's settlers would be more loyal to the Spanish crown (or less, in the right classes) than those in the Spanish Main or Mexico, so either the Bay Area settlement spins itself off as an independent republic (or principality or duchy or outright kingdom perhaps) or it remains under Mexican administration as in integral part of Mexico.
Unlike OTL, however, it would take a lot more effort to seize it from Mexico, and the USA would be hard pressed in 1847 to project that kind of power that far west. Give it a few more decades and sure, Uncle Sam ought to be able to take it eventually, but then be plagued by problems of a native Catholic-Mexican population. It might be possible as OTL to mobilize a segment of that population as pro-Yankee takeover, but it would be orders of magnitude larger than the OTL Mexican population and breaking promises to them would have stronger political consequences. Meanwhile OTL the task of securing California to the US system was overall greatly aided by the OTL 1849 Gold Rush, whereas here the easy gold will have been gathered long ago. Some gold mining will be going on and expand greatly under American rule, but keeping control of central Alta California will be much more a matter of negotiation in the ATL.