📜  在节点 js azure 函数上上传文件 - Javascript 代码示例

📅  最后修改于: 2022-03-11 15:04:27.054000             🧑  作者: Mango

代码示例1
const streamifier = require('streamifier');
const multipart = require('parse-multipart');
const azure = require('azure-storage');

module.exports =  function (context, req) {
  var bodyBuffer = Buffer.from(req.body);
  var boundary = multipart.getBoundary(req.headers['content-type']);
  var parts = multipart.Parse(bodyBuffer, boundary);

  var filedata = parts[0].data;         // Image buffer data
  var filename = parts[0].filename;     // testImage.png
  console.log("parts[0].data");
  console.log(parts[0].data);
  console.log("parts[0].filename");
  console.log(parts[0].filename);
  const accountname ="blobstorage0516";
  const key = "eGier5YJBzr5z3xgOJUb+sn***7Csvgx75NwiOZE7kYLJfLqWBg==";
  const containerName="test1";
  const blobClient  =azure.createBlobService(accountname,key);
 # set blob content type
  var options = {
    contentSettings:{contentType: parts[0].type}
  };
  blobClient.createBlockBlobFromStream(containerName,filename,streamifier.createReadStream(new Buffer(filedata)), filedata.length,options,(err,result)=>{

    if (!err) {
        console.log("Image upload successful", result);

    } else{

      throw err;
    }
  })
  context.res = {
    // status defaults to 200 */
    body: "ok"
};
context.done();
};