简体   繁体   中英

azure blob download via SAS

All,

I have a blob container with nested filenames (simulating a folder).

Is it possible to download via code (bypassing the webserver) through SAS URI for a bunch of files beginning with a prefix? Right now, i am zipping these files and sending it to a stream below..

  CloudBlobContainer container = GetRootContainer();
  CloudBlockBlob caseBlob = container.GetBlockBlobReference(folderPrefix);
  await caseBlob.DownloadToStreamAsync(zipStream);

This works and i can download the set of files beginning with that prefix to a client machine. However this is dependent on the webserver's speed and its comparatively slow.

Is there an example on how to download using SAS by providing a URI for the folder? Here is an example i found from another post in stackoverflow

var sasConstraints = new SharedAccessBlobPolicy();
sasConstraints.SharedAccessStartTime = DateTime.UtcNow.AddMinutes(-5);
sasConstraints.SharedAccessExpiryTime = DateTime.UtcNow.AddMinutes(10);
sasConstraints.Permissions = SharedAccessBlobPermissions.Read;

var sasBlobToken = blob.GetSharedAccessSignature(sasConstraints);

return blob.Uri + sasBlobToken;
//Here can the URI point to the prefix??

Can i use something like this?

Thanks!

It is certainly doable however it won't be a single step process.

First, you can't have a SAS Token for a virtual folder. You will need to get a SAS token for the blob container with both List (for listing blobs) and Read (for downloading blob) permissions.

Next you will list the blobs in the virtual folder inside the blob container. In order to do so, you will need to specify the virtual folder path as the prefix. This will give you a list of blobs inside that virtual folder. Please ensure that you specify empty string as delimiter so that all blobs inside that virtual folders are listed.

Once you have the list of blobs, you will need to read (download) the blobs and store the blob content somewhere in the browser.

After you have read the blob contents, you could use a JS based zip library to dynamically create zip file contents and once all the blobs are added to the zip file, you can force download of that zip file. A quick search for JS based zip library landed me here: https://stuk.github.io/jszip/ . When I implemented this functionality in a product I built, I used ZipJS library but unfortunately I am not able to find it online though now.

Bits and pieces of the code I wrote many years ago (it was part of a much larger application + there was no JS Storage SDK when I wrote it so apologies if the code does not much sense to you. Please use it for general guidance only).

    zip.workerScriptsPath = '/Scripts/ZipJS/';
    zipWriter = new zip.BlobWriter();
    zip.createWriter(zipWriter, function (zipWriter) {
        startZippingFiles(zipWriter);
    }, function () {

    }, true);


function startZippingFiles(writer) {
    if (downloadedContent.length > 0) {//downloadedContent is an array containing downloaded blobs
        var downloadedContentItem = downloadedContent.shift();//read first item
        var cloudBlob = downloadedContentItem.Blob;//get the cloud blob object
        var blobContents = downloadedContentItem.Content;//get the blob's content
        var status = downloadedContentItem.Status;//status to track blob's download status
        if (status === 'Completed') {
            writer.add(cloudBlob.name,
                new zip.BlobReader(new Blob([blobContents], { type: cloudBlob.properties.contentType })), function () {
                    console.log(cloudBlob.name + ' added to zip...')
                    downloadedBlobsCount += 1;
                    startZippingFiles(writer);
                }, function (o) {
                    console.log('Adding ' + cloudBlob.name + ' to zip file. ' + parseFloat((o * 100) / cloudBlob.size).toFixed(2) + '% done...');
                });
        }
    } else {
        writer.close(function (blob) {//Finally save the zipped data as download.zip
            saveAs(blob, "download.zip");
            zipWriter = null;
        });
        console.log("Download successful!");
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM