0
votes

I have the URL to a blob which I'm trying to upload to azure storage, there doesn't seem to be an obvious way of doing this as none of the APIs handle uploading a blob url directly.

I'm trying to do something like this:

            blobService.createBlockBlobFromLocalFile('taskcontainer', 'myfile.png', blobUrl, (error, result, response) => {

            });

Which doesn't work, I've tried to find ways to read the blob url to a readable stream and upload that but haven't gotten very far either.

I basically have a file selected by the user using react-dropzone which provides me with a blob url (which can look like this: blob:http://localhost:3000/cd8ba70e-5877-4112-8131-91c594be8f1e) pointing to the local file. My goal is to now upload that blob url to an azure container.

Firebase storage has a 'put' function which allows you to upload the blob from a url: https://firebase.google.com/docs/storage/web/upload-files

This is the closest I have gotten:

            var blobUrl = acceptedFiles[0].preview;

            var xhr = new XMLHttpRequest();
            xhr.open("GET", blobUrl);
            xhr.responseType = "text";//force the HTTP response, response-type header to be blob
            xhr.onload = function () {
                const Stream = require('stream')
                const readable = new Stream.Readable()

                readable.push(xhr.responseText);
                readable.push(null);

                blobService.createBlockBlobFromStream('taskcontainer', 'myblob.png', readable, xhr.responseText.length, (error, result, response)=>{
                    var ok = 0;
                })
            }
            xhr.send();

The file (or parts of it?) seem to get uploaded but the end result is the file type is lost and I can't view the png uploaded..

1
If I understand correctly you want to create a new blob from a blob URL. Is my understanding correct?Gaurav Mantri
nono, I have a blob url, I can get the blob from it if I need to but I'm trying to upload that blob to azure storage. Azure storage does not allow me to upload blobs through its api, it allows uploading through a readable stream, local file, text or browser file and I need to find a way to turn the blob url into something I can upload..meds
I'm confused :). So you have a blob URL like myaccount.blob.core.windows.net/mycontainer/myblob.png and you want to upload a file from your local computer that gets saved as myblob.png. Can you please edit your question and provide more details?Gaurav Mantri
oh dear ok I updated with a bit more detail, hope that clarifies my intentions :)meds
Aah....When you say blob, you actually mean blob representing local file and not Azure Blob. Can you share what this blob URL looks like?Gaurav Mantri

1 Answers

0
votes

You could try the following

var azure = require('azure-storage');
var blobService = azure.createBlobService('', '');

blobService.createBlockBlobFromLocalFile('nodecontainer', 'AzureDC', 'azure_center.png', function(error, result, response) {
  publicAccessLevel: 'blob'
}, function(error, result, response) {
  if (!error) {
    console.log(response);
  } else {
    console.log(error);
  }
});

EDIT

Check this code snippet to upload blob to azure storage