如何将二进制视频数据保存到 azure blob?

How to save binary video data to azure blob?

我目前正在使用此代码 select 从本地磁盘到我的 api:

<script language="javascript" type="text/javascript">
$(document).ready(function(){

$(':file').on('change', function () {
  var file = this.files[0];

    if (file.type !== "video/mp4" && file.type!== "video/quicktime") {
        alert("Content must be video .mp4 or .mov")
    }

$(':button').on('click', function () {
    if (file.type == "video/mp4" || file.type == "video/quicktime"){
  $.ajax({
    // Your server script to process the upload
    url: 'azureAPI',
    type: 'POST',

    // Form data
    data: new FormData($('form')[0]),

    // Tell jQuery not to process data or worry about content-type
    // You *must* include these options!
    cache: false,
    contentType: false,
    processData: false,

    // Custom XMLHttpRequest
    xhr: function () {
      var myXhr = $.ajaxSettings.xhr();
      if (myXhr.upload) {
        // For handling the progress of the upload
        myXhr.upload.addEventListener('progress', function (e) {
          if (e.lengthComputable) {
            $('progress').attr({
              value: e.loaded,
              max: e.total,
            });
          }
        }, false);
      }
      return myXhr;
    }
  });
} else {
    alert ("File type must be .mp4 or .mov")
}
});
});
});
</script>

这会以这种形式发送(我假设是)二进制数据:

���
1!QAa"q2B���R�#3br��u�����S6C$%��5�cts�T&D4��U��d���e!1AQa2"q�#����3��B���X"��?��!=��W�u�ٗ�-2���?����ۯ�Կ�i���t����M���Y�-��-Vdϊ�P�<�<U#TY]K��dW
���

我正在使用 Azure,现在正尝试将其发送到 Microsoft Video Indexer,它表示将数据作为正文中的 multipart/form-data 发送。 (见 https://api-portal.videoindexer.ai/docs/services/Operations/operations/Upload-Video?)

我尝试发送正文中的二进制数据,但它说需要 string/buffer。

然后我尝试将正文中的二进制数据发送为 var body = Buffer.from(req.body,'binary')

已发送,但 VI 回复说索引数据时出现问题,可能是因为我发送的编码错误?

为了解决这个问题,我现在尝试先将该二进制数据保存到一个块 ​​blob,然后我将调用它 url,但是我无法使用以下方法将二进制数据保存到 Azure 块 blob :

var buf = Buffer.from(req.body, 'binary');
blobService.createBlockBlobFromText(container, 'fileName.mp4', buf, {contentSettings: {contentType: 'video/mp4', contentEncoding: 'binary'}}, function (error, result, response) {
    if(!error){
        callback('uploaded');
    } else {
        callback('nope');
    }

});

我尝试了这个,一开始没有 contentSettings,但将数据保存为 contentType: application/octet-stream,它没有作为视频打开。然后我添加了 contentType,并最后尝试添加 contentEncoding

这保存了正确的 contentType 但视频仍然无法打开。

有谁知道如何正确编码数据以首先直接发送到视频索引器,或者其次将二进制数据保存到 Azure blob 存储?

感谢您的指点,如有遗漏,敬请谅解。

根据我的测试,如果你想使用Azure函数上传文件到Azure blob,请参考下面的代码。

前端

<!DOCTYPE html>
<html>
  <script type="text/javascript"
 src="https://ajax.aspnetcdn.com/ajax/jQuery/jquery-3.4.1.js">
</script>
<body>

 <form enctype="multipart/form-data">
    <input name="file" type="file" accept="video/*"/>
    <input type="button" value="Upload" />
</form>
<progress></progress>

<script language="javascript" type="text/javascript">
$(document).ready(function(){

$(':file').on('change', function () {
  var file = this.files[0];
    if (file.type !== "video/mp4" && file.type!== "video/quicktime") {
        alert("Content must be video .mp4 or .mov")
    }

$(':button').on('click', function () {

  if (file.type == "video/mp4" || file.type == "video/quicktime"){

    $.ajax({
    // Your server script to process the upload
    url: '',
    type: 'POST',
    crossDomain: true,
    enctype: 'multipart/form-data',
    // Form data
    data: new FormData($('form')[0]),

    // Tell jQuery not to process data or worry about content-type
    // You *must* include these options!
    cache: false,
    contentType: false,
    processData: false,

    success :  function(data){console.log(data);},

    // Custom XMLHttpRequest
    xhr: function () {
      var myXhr = $.ajaxSettings.xhr();
      if (myXhr.upload) {
        // For handling the progress of the upload

        myXhr.upload.addEventListener('progress', function (e) {
          if (e.lengthComputable) {
            $('progress').attr({
              value: e.loaded,
              max: e.total,
            });
          }
        }, false);
      }
      return myXhr;
    }
  });
} else {
    alert ("File type must be .mp4 or .mov")
}
});





});



});

</script>

</body>
</html>

Azure 函数

var multipart = require('parse-multipart')
var azure = require('azure-storage');
var getStream = require('into-stream')
module.exports = async function (context, request) {
    context.log('JavaScript HTTP trigger function processed a request.');
    // encode body to base64 string
    var bodyBuffer = Buffer.from(request.body);

    var boundary = multipart.getBoundary(request.headers['content-type']);
    // parse the body
    var parts = multipart.Parse(bodyBuffer, boundary);

    const accountname ="blobstorage0516";
            const key = "key";
            const containerName="test";
            var retryOperations = new azure.ExponentialRetryPolicyFilter();
            const blobClient  =azure.createBlobService(accountname,key).withFilter(retryOperations);
            blobClient.createContainerIfNotExists(containerName, function (error) {
                if (error) {
                  context.log(error);
                }
            });

            var options = {
                contentSettings:{contentType: parts[0].type},
                metadata: { fileName: parts[0].filename },
                blockSize:8*1024*1024,
                parallelOperationThreadCount:20,
                timeoutIntervalInMs:30*60*1000
              };
              var stream =getStream(parts[0].data)
              context.log("start")

              var result="ok"
            var speedsummary= blobClient.createBlockBlobFromStream(containerName,parts[0].filename,stream,parts[0].data.length,options,function (error) {


              if (error != null) {
               result=error
              } else {



              }})

               context.res = { body : { results : result}};
              context.done(); 


};

您还可以从 bloburl

访问视频