已使用全球MS Azure Cognitive Services SDK的Web应用切换中国区Speech-to-Text的技术咨询
Hey there! Let's walk through everything you need to know to switch your JavaScript Speech-to-Text setup from global Azure to Azure China, plus how to integrate both regions seamlessly in your web app. I've broken this down into clear, actionable sections below:
First, let's cover the core distinctions that matter for your implementation:
- Data Residency & Compliance: Azure China operates as an independent instance, with all data stored within China's borders and subject to local regulations (like the Cybersecurity Law of China). This doesn't change how Speech-to-Text works, but you'll need to ensure your use case aligns with these rules.
- Service Endpoints: This is the biggest technical gap—Azure China uses entirely separate endpoints from global Azure; there's no cross-region compatibility here.
- Authentication Credentials: Azure China subscription keys or Azure AD credentials won't work with global Azure (and vice versa). You'll need to use credentials specifically generated for your Azure China Speech resource.
For Speech-to-Text over WebSocket, the endpoint structure is different between the two environments:
- Global Azure typically uses endpoints like
wss://<region>.stt.speech.microsoft.com/speech/universal/v2 - Azure China uses a modified base domain, following this format:
wss://<region>.stt.speech.azure.cn/speech/universal/v2
Valid region codes for Azure China Speech Services are:
chinaeast2chinanorthchinanorth2
When setting up the WebSocket connection, remember to:
- Use the endpoint that matches your Azure China Speech resource's region
- Pass your Azure China subscription key (or a token generated from it) in the
Authorizationheader—same auth pattern as global, but with China-specific credentials
Good news: You don't need a separate "China-only" JavaScript SDK. The official Microsoft Cognitive Services Speech SDK works for both regions—you just need to explicitly configure it to target Azure China's endpoints.
Here's a quick example of initializing the SDK for Azure China:
import { SpeechConfig, AudioConfig, SpeechRecognizer, ResultReason } from 'microsoft-cognitiveservices-speech-sdk'; // Initialize with your Azure China key and region const speechConfig = SpeechConfig.fromSubscription('YOUR_AZURE_CHINA_SUBSCRIPTION_KEY', 'chinaeast2'); // Override the endpoint to point to Azure China's Speech Service speechConfig.setServiceProperty('SpeechServiceConnectionEndpoint', 'wss://chinaeast2.stt.speech.azure.cn/speech/universal/v2'); // Rest of your code stays nearly identical to global setup const audioConfig = AudioConfig.fromDefaultMicrophoneInput(); const recognizer = new SpeechRecognizer(speechConfig, audioConfig); recognizer.recognized = (s, e) => { if (e.result.reason === ResultReason.RecognizedSpeech) { console.log(`China Region Recognized Text: ${e.result.text}`); } }; recognizer.startContinuousRecognitionAsync();
To support both regions in a single app, create a helper function that handles region-specific configuration:
function createSpeechConfig(region, subscriptionKey, isChinaRegion = false) { const speechConfig = SpeechConfig.fromSubscription(subscriptionKey, region); if (isChinaRegion) { // Set China-specific endpoint const chinaEndpoint = `wss://${region}.stt.speech.azure.cn/speech/universal/v2`; speechConfig.setServiceProperty('SpeechServiceConnectionEndpoint', chinaEndpoint); } return speechConfig; } // Usage for Global Azure const globalSpeechConfig = createSpeechConfig('eastus', 'YOUR_GLOBAL_SUBSCRIPTION_KEY'); // Usage for Azure China const chinaSpeechConfig = createSpeechConfig('chinaeast2', 'YOUR_CHINA_SUBSCRIPTION_KEY', true);
- Token Authentication: If you prefer token-based auth over direct subscription keys, generate tokens using Azure China's token endpoint:
https://<region>.api.azure.cn/sts/v1.0/issueToken(instead of the globalhttps://<region>.api.cognitive.microsoft.com/sts/v1.0/issueToken). - Network Testing: If you're testing from outside China, you might encounter network restrictions—try testing from a China-based environment if possible to validate connectivity.
- Feature Parity: Most core Speech-to-Text features are identical between global and China regions, but always verify any niche features you use against Azure China's service documentation.
内容的提问来源于stack exchange,提问作者Анастасия Яцура

