Search code examples
node.jsazureazure-storage

Is it possible to download only a certain part of an append blob?


I upload audio files to my azure blob, and I would like to know if it is possible to download only the parts I want of the audio ? by the way, I'm using nodeJs
Thank you !


Solution

  • I upload audio files to my azure blob, and I would like to know if it is possible to download only the parts I want of the audio?

    Yes, it is certainly possible to download a portion of a blob. Azure Blobs support reading a range of bytes. For example, let's say you want to download only first 1KB of data from a file. This is how you would download that data:

    import azure from 'azure-storage';
    const ms = require('memory-streams');
    
    const chunkStart = 0;
    const chunkEnd = 1023;
    const connectionString = 'your-azure-storage-connection-string';
    const blobService = azure.createBlobService(connectionString);
    
    const writableStream = new ms.WritableStream({
      highWaterMark: (chunk.end - chunk.start) * 2,
      writableHighWaterMark: (chunk.end - chunk.start) * 2,
    });
    
    const requestOptions = {
      rangeStart: chunkStart,
      rangeEnd: chunkEnd
    };
    
    blobService.getBlobToStream('container-name', 'blob-name', writableStream, requestOptions, (error, result, response) => {
      if (error) {
        console.log('Error occurred while downloading chunk!');
      } else {
        const dataBuffer = writableStream.toBuffer();
        console.log('Blob chunk downloaded!');
      }
    });
    

    Considering you mentioned that you're storing an audio file, please note that you can't instruct Azure Storage to download "x" duration of audio (e.g. download first 30 seconds of audio) as Azure Storage treats all blobs as a collection of bytes and has no idea if the file is an audio file or something else.