Overwrite an existing blob in Azure storage does not work with NODE

3.8k views Asked by At

New question

When I overwrite a blob and then update the browser is still caching the main image and not the new one. I have read that there is a cache-control property but I can not implement it. I need to clean the blob cache that has just been uploaded

Old question

I am trying to overwrite an existing blob using connect-busboy middleware and the following method, but the file is not overwritten and I do not understand why.

createBlockBlobFromStream(container, blob, (Stream), streamLength,
options, callback) → {SpeedSummary}

Uploads a block blob from a stream. If the blob already exists on the service, it will be overwritten.

app.post('/upload', function(req, res, params) {
    var name;
    req.busboy.on('field', function (fieldname, val) {
      name = val+'.jpg';
    });

 req.busboy.on('file', function (fieldname, file, filename, encoding, mimetype) {
      file.on('data', function (data) {
        console.log(name);
         console.log(data);
        var bufferStream = new stream.PassThrough();
          bufferStream.end(data);
                var blobSvc = azure.createBlobService(accountName, accountKey);
          blobSvc.createBlockBlobFromStream('images', name, bufferStream, data.length, function (error, result, response){
              if (!error) {
                  res.send(200,'upload succeeded')
              } else {
                  res.send(500,JSON.stringify(error))
              }
          })
      });
    });
});
1

There are 1 answers

2
Aaron Chen On BEST ANSWER

Per Azure Storage SDK for Node API Reference,

createBlockBlobFromStream(container, blob, (Stream), streamLength, options, callback) → {SpeedSummary}

Uploads a block blob from a stream. If the blob already exists on the service, it will be overwritten. To avoid overwriting and instead throw an error if the blob exists, please pass in an accessConditions parameter in the options object.

So you are doing right to overwrite the existing blob file with Azure Storage SDK for Node.

Edit:

Seems you have encountered upload issue. I would recommend you to try using createWriteStreamToBlockBlob instead of createBlockBlobFromStream to upload blob to Azure storage. The following example works by using connect-busboy middleware. create /public folders. Use the folder structure:

\index.js

\public\index.html

INDEX.JS

var express = require('express')
var app = express()
var busboy = require('connect-busboy')
var azure = require('azure-storage')

var accountName = "<acountName>"
var accountKey = "<accountKey>"

app.use(busboy())
app.use(express.static('public'))

app.get('/', function(req, res) {
  res.sendFile('index.html')
})

app.post('/upload', function(req, res, params) {
  req.pipe(req.busboy);

  req.busboy.on('file', function(fieldname, file, filename) {
    var blobSvc = azure.createBlobService(accountName, accountKey)
    file.pipe(blobSvc.createWriteStreamToBlockBlob('mycontainer', filename, function(error) {
      if (!error) {
        res.send(200, 'upload succeeded')
      } else {
        res.send(500, JSON.stringify(error))
      }
    }))

  })
})

app.listen(process.env.PORT || 3000, function() {
  console.log('Example app listening on port 3000!')
})

INDEX.HTML

<!DOCTYPE html>
<html>
<head>
    <title>Sample</title>
</head>

<body>
    <form action="/upload" method="post" enctype="multipart/form-data">
        Select image to upload:
        <input type="file" name="fileToUpload" id="fileToUpload">
        <input type="submit" value="Upload Image" name="submit">
    </form>
</body>
</html>