0

I am implementing API that will ahve to handel request - one of them is POST tha will store in it image file (example: jpg) that i will have to upload to azure storage.

I found module, that will alllow putting fiels into storage (seems so, I couldn't try it) if presented as stream or text (so, probably going with stream)

azure-storage

I then used Postman to send request with file: ( Tool for sending multipart/form-data request )

but now, in request's body there is just big mass of incomprehensible characers (seems ok, since it is image), but I stucked now - I do not know how to get that file from reuqest and pass it to module's function?

Some more code:

route.route('/photo')
    .post(
    (req, res) => {
            upload(req);
    })

and part for upload(request) function (now implemented with fucntion useing local file, so I could test if it even worked with connection given - It did!):

process.env.AZURE_STORAGE_CONNECTION_STRING = 'MyConnectionString'
upload(request) {

        blobService.createBlockBlobFromLocalFile(
            ContaienrName,
            FileNameInContainer,
            'D:\\My\\Path\\To\\image.jpg', 
            function (error, result, response) {
                if (!error) {
                    console.log(' >> File uploaded!');
                }
                else {
                    console.log(' >> File NOT uploaded!');
                }
                console.log(' >> --------------------');
                console.log(' >> ERROR : ');
                console.log(error);
                console.log(' >> --------------------');
                console.log(' >> RESULT : ');
                console.log(result);
                console.log(' >> --------------------');
                console.log(' >> RESPONSE : ');
                console.log(response);
                console.log(' >> --------------------');

                resolve(resultReturning);
            }
        );
}

EDIT #1: I managed to get "part" of requst that i will ha ve to handel i nacciuality:

Content-Disposition: form-data; name="image"; filename="71127.jpeg"
Content-Type: image/jpeg
Community
  • 1
  • 1
Asker
  • 89
  • 8

1 Answers1

0

First, you'll need to parse incoming multipart/form-data to a stream. You can use connect-busbody to do that. For more infomation, please refer to Mick Cullen's answer.

Second, use createWriteStreamToBlockBlob instead of createBlockBlobFromLocalFile to provide a stream to write to a block blob. The sample code for node.js with azure-storage you can check out my earlier post.

Community
  • 1
  • 1
Aaron Chen
  • 9,835
  • 1
  • 16
  • 28