如何解决使用不起作用的流将文件保存到 Azure 存储
从流中将文件保存到流容器中这是我如何获得流
let request = rp(options).on("response",async (response) => {
if (response.statusCode === 200) {
await insertFile("TTSOutput.wav",request,request.length);
}
这是我将它保存到 Azure 的方法
var insertFile = async function (blobName,stream) {
try {
blobService = await azure.createBlobService(connStr);
blobService.createContainerIfNotExists(
containerName,{
publicAccessLevel: "blob",},(err,result,response) => {
if (!err) {
let resultstream = blobService.createWriteStreamToBlockBlob(
containerName,blobName
);
stream.pipe(resultstream);
}
}
);
}
实际上只创建了容器,但没有上传文件
解决方法
尝试使用以下代码从 Azure TTS API 上传 .wav 文件:
let request = rp(options)
.on('response',(response) => {
if (response.statusCode === 200) {
tempFilePath = 'd:/temp.wav'
request.pipe(fs.createWriteStream(tempFilePath));
insertFile('TTSOutput.wav',fs.createReadStream(tempFilePath)).then(function(){
fs.unlinkSync(tempFilePath)
console.log("uploaded")})
}
});
insertFile
函数:
const {
BlobServiceClient
} = require('@azure/storage-blob');
var conn_str = '<storage account connection string>'
var container = '<your container name>'
async function insertFile(blobName,stream) {
BlobServiceClient.fromConnectionString(conn_str).getContainerClient(container).getBlockBlobClient(blobName).uploadStream(stream)
}
结果:
如果您还有其他问题,请告诉我。
,如果您的意思是将流上传到 Azure Storage Blob,请尝试使用 createBlockBlobFromStream
方法。
var myStream = getSomeStream(); // Assume getSomeStream() returns a readable stream
var myStreamLength = getSomeStreamLength(); // return the length of stream
blobService.createBlockBlobFromStream(
containerName,'stream-blob-name',myStream,myStreamLength,function(error,result,response){
if(error){
console.log("Couldn't upload stream");
console.error(error);
} else {
console.log('Stream uploaded successfully');
}
});
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。