简体   繁体   English

multipart/form-data 到 azure blob

[英]multipart/form-data to azure blob

Currently parsing the multipart/form-data has been successful except for the images.目前除图像外,已成功解析 multipart/form-data。

The tech being used:使用的技术:

NodeJS w/ Azure Function 2.0 (tried with both binary and stream datatypes) NodeJS w/ Azure Function 2.0(尝试使用二进制和流数据类型)

Sendgrid Inbound Parser ( https://sendgrid.com/docs/for-developers/parsing-email/setting-up-the-inbound-parse-webhook/#example-default-payload ) Sendgrid 入站解析器( https://sendgrid.com/docs/for-developers/parsing-email/setting-up-the-inbound-parse-webhook/#example-default-payload

Azure Blob天蓝色斑点

I'm able to parse out the images using Buffer leading to the following format for the attachments我能够使用 Buffer 解析出图像,导致附件的格式如下

{ filename: 'cb.jpg',
       name: 'cb.jpg',
       type: 'image/jpeg',
       content:
        <Buffer 0d 0a ff d8 ff e0 00 10 4a 46 49 46 00 01 01 01 00 48 00 48 00 00 ff e2 0c 58 49 43 43 5f 50 52 4f 46 49 4c 45 00 01 01 00 00 0c 48 4c 69 6e 6f 02 10 ... > },

The buffer I try to feed into the azure blob as such我尝试输入天蓝色 blob 的缓冲区

const blockBlobURL = BlockBlobURL.fromContainerURL(containerURL, recordId + path.sep + attachment.filename);
var bufferStream = new stream.PassThrough();
bufferStream.end(Buffer.from(attachment.content.toString('base64')));

const aborter = Aborter.timeout(NINE_MINUTES);

return uploadStreamToBlockBlob(aborter, bufferStream, blockBlobURL, EIGHT_MEGABYTES, 5);

I've tried with and without the toString and the base64 but no luck.我试过使用和不使用 toString 和 base64,但没有运气。 The file uploads but the content i'm sending over is malformed in some way when i try to view it in the storage blob.文件上传但我发送的内容在我尝试在存储 blob 中查看时以某种方式格式错误。 Any thoughts on what transformation steps i'm missing?关于我缺少哪些转换步骤的任何想法?

According to my test, we can use the following code:根据我的测试,我们可以使用如下代码:

 const fs = require('fs')
    const {
      Aborter,
      BlobURL,
      BlockBlobURL,
      ContainerURL,
      ServiceURL,
      StorageURL,
      SharedKeyCredential,
      uploadStreamToBlockBlob,
    } = require('@azure/storage-blob');

    const stream = require('stream');
    const ONE_MEGABYTE = 1024 * 1024;
    const uploadOptions = { bufferSize: 4 * ONE_MEGABYTE, maxBuffers: 20 };
    const ONE_MINUTE = 60 * 1000;
    const aborter = Aborter.timeout(30 * ONE_MINUTE);;
    const accountname ="blobstorage0516";
    const key = "";
    const containerName="test";
    const sharedKeyCredential = new SharedKeyCredential(
      accountname,
      key);
    const pipeline = StorageURL.newPipeline(sharedKeyCredential);
    const serviceURL = new ServiceURL(
      `https://${accountname}.blob.core.windows.net`,
      pipeline
    );
    async function upload() {




      console.log("----")
      // read jpg to buffer
      const str = fs.readFileSync("D:\\test.jpg")

       //upload  
      const containerURL = ContainerURL.fromServiceURL(serviceURL, containerName);
      const blobURL = BlobURL.fromContainerURL(containerURL, "test.jpg");
      const blockBlobURL = BlockBlobURL.fromBlobURL(blobURL);
      const bufferStream = new stream.PassThrough();
      bufferStream.end(str);


        await uploadStreamToBlockBlob(aborter, bufferStream,
           blockBlobURL, uploadOptions.bufferSize, uploadOptions.maxBuffers);

        // download
        const baseLineImage = await blockBlobURL.download(aborter, 0)

         const datastream =  new stream.PassThrough();
    const readableStream = baseLineImage.readableStreamBody
    readableStream.on("data", data => {
      datastream.push(data);

    });
    readableStream.on("end" , () => {
      fs.writeFileSync('D:\\test1.jpg', datastream.read())
      console.log("download successfully")
      datastream.destroy();

  });
  readableStream.on("error" , (error) => {

    datastream.destroy();
    throw error;

});




    }

    upload()
      .then(() => {
        console.log("Successfully executed sample.");
      })
      .catch((err) => {
        console.log(err.message);
      });

在此处输入图片说明 在此处输入图片说明 在此处输入图片说明

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

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