繁体   English   中英

使用流不工作将文件保存到 azure 存储

[英]Save file to azure storage using streams not working

我想使用azure-storage package从 stream 将文件保存到 stream 容器中

这是我如何获得 stream

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)

}

结果:

在此处输入图像描述 在此处输入图像描述

如果您还有其他问题,请告诉我。

如果您的意思是将 stream 上传到 Azure 存储 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');
        }
    });

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM