jofftiquez
jofftiquez

Reputation: 7708

Nodejs uploading base64 image to azure blob storage results to "ResourceNotFound" error

Here is the post request JSON :

{
    "name":"images.jpg",
    "file":"data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAAAQABAAD/2wCEAAkGBxQTEhIUEhIUFBUV…K9rk8hCAEkjFMUYiEAI+nHIpsQh0AkisDYRTOiCAbWVtgCtI6IlkHh7LDTQXLH0EIQBj//2Q=="
}

And here's my node controller under the request /upload route, I am using createBlockBlobFromText() from azure-storage.

var azure = require('azure-storage');
var blobSvc = azure.createBlobService('myblob.blob.core.windows.net/mycontainer', THE_KEY);

controllers.upload = function (req, res, next){

    var startDate = new Date();
    var expiryDate = new Date(startDate);

    var sharedAccessPolicy = {
        AccessPolicy: {
            Permissions: azure.BlobUtilities.SharedAccessPermissions.WRITE,
            Start: startDate,
            Expiry: expiryDate
        }
    };

    var sharedAccessSignatureToken = blobSvc.generateSharedAccessSignature('resources', req.body.file, sharedAccessPolicy);
    var sharedBlobService = azure.createBlobServiceWithSas(blobSvc.host.primaryHost + '/' + 'mycontainer' + '?' + sharedAccessSignatureToken);

    sharedBlobService.createBlockBlobFromText('mycontainer', req.body.name, req.body.file, function(error, result, response) {
        if (error) {
            res.send(error);
            return;
        }
        res.send(result);
    });

} 

However I am getting this error.

{
    "code": "ResourceNotFound",
    "statusCode": 404,
    "requestId": "bffa6099-0001-000b-53f9-80d18a000000"
}

Upvotes: 1

Views: 1530

Answers (1)

Couple of changes needed:

  • expiration date should be greater than start date.
  • shared access signature should use same container name.
  • shared access signature should be passed in file name, not file content.

After these changes, the code would be similar to this:

var sharedAccessSignatureToken = blobSvc.generateSharedAccessSignature('mycontainer', req.body.name, sharedAccessPolicy);
var sharedBlobService = azure.createBlobServiceWithSas(blobSvc.host, sharedAccessSignatureToken);

sharedBlobService.createBlockBlobFromText('mycontainer', req.body.name, req.body.file, function(error, result, response) {
        if (error) {
            res.send(error);
            return;
        }
        res.send(result);
    });

Upvotes: 4

Related Questions