documentation的fs.createReadStream(path)
表示它可以读取URL:
path <string> | <Buffer> | <URL>
文档显示path
周围的引号:
fs.createReadStream('/dev/input/event0');
但是当我输入此Firebase Storage下载URL时
fs.createReadStream('https://firebasestorage.googleapis.com/v0/b/languagetwo-cd94d.appspot.com/o/Users%2FbcmrZDO0X5N6kB38MqhUJZ11OzA3%2Faudio-file.flac?alt=media&token=871b9401-c6af-4c38-aaf3-889bb5952d0e'),
我收到错误消息ENOENT: no such file or directory
。您可以单击该URL并听到它起作用:
我也尝试过Google Storage URI:
fs.createReadStream('gs://languagetwo-cd94d.appspot.com/Users/bcmrZDO0X5N6kB38MqhUJZ11OzA3/audio-file.flac'),
这没用。我必须使用https.request
发出HTTP GET请求吗?
这是我的完整代码,复制自IBM Cloud Speech-to-Text API docs:
const fs = require('fs');
const SpeechToTextV1 = require('ibm-watson/speech-to-text/v1');
const { IamAuthenticator } = require('ibm-watson/auth');
const speechToText = new SpeechToTextV1({
authenticator: new IamAuthenticator({
apikey: 'my-api-key',
}),
url: 'https://api.us-south.speech-to-text.watson.cloud.ibm.com/instances/01010101',
});
var params = {
audio: fs.createReadStream('https://firebasestorage.googleapis.com/v0/b/languagetwo-cd94d.appspot.com/o/Users%2FbcmrZDO0X5N6kB38MqhUJZ11OzA3%2Faudio-file.flac?alt=media&token=871b9401-c6af-4c38-aaf3-889bb5952d0e'),
contentType: 'audio/flac',
wordAlternativesThreshold: 0.9,
keywords: ['colorado', 'tornado', 'tornadoes'],
keywordsThreshold: 0.5,
};
speechToText.recognize(params)
.then(results => {
console.log(JSON.stringify(results, null, 2));
})
.catch(err => {
console.log('error:', err);
});
如果看到here,则明确提到它仅支持file://
协议。 http/https
不支持。
我不建议使用request
或request-promise
,因为它们已被弃用。我建议使用一些现代库,例如got
,axios
等。>