1

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))
              }
          })
      });
    });
});
mcappato
  • 15
  • 1
  • 4
  • 1
    What's the behavior you're seeing with the above code? What error are you getting? Please edit your question to be more clear. – David Makogon Dec 29 '16 at 19:20

1 Answers1

0

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>
Aaron Chen
  • 9,835
  • 1
  • 16
  • 28
  • Thank you very much for your answer. I used a lot. I use your code and it works just like mine. I suspect that the portal azure admin will have blocked overwriting permissions by some option on the portal. He comes in a few weeks. Anyway your code was used to verify and confirm that the problem is another. thank you very much! – mcappato Dec 30 '16 at 13:40
  • or may be it is a frontend cache problem im using angular2 – mcappato Dec 30 '16 at 15:51