Reft
Reft

Reputation: 2433

Azure ZIP multiple files using Shared Access Signature and download from stream

Is it somehow possible to zip multiple files directly to the output stream using Shared access signature and then send it to user?


I'm using Shared Access Signatures for downloading blobs from my blobstorage and DotNetZip for zipping the blobs, (or trying atleast).

   var images = _fileService.GetImages(companyID, model.ImageIDs).ToList();

        if (images != null && images.Count > 0)
        {
            DateTime currentTime = TimeZoneExtensions.GetCurrentDate();

            using (ZipFile zip = new ZipFile())
            {
                MemoryStream output = new MemoryStream();

                foreach (var image in images)
                {
                    if (image.ImageLinks != null)
                    {
                        //ImageLinks contains SAS uri for each image format..
                        List<ImageLinkModel> imageLinks = image.ImageLinks.Where(m => m.FormatID == model.FormatID).ToList();

                        if (imageLinks != null && imageLinks.Count > 0)
                        {
                            foreach (var imageLink in imageLinks)
                            {
                                if (String.IsNullOrEmpty(imageLink.DownloadURL) || imageLink.DateExpiresDownloadURL < currentTime)
                                {
                                    int hours = 1;

                                    imageLink.DateExpiresDownloadURL = currentTime.AddHours(hours);

                                    //Create new shared access signature
                                    imageLink.DownloadURL = _fileService.GetDownloadImageURL(imageLink.BlobName, image.ImageName, hours);

                                    //Update imagelink
                                    _fileService.UpdateImageLink(companyID, imageLink);

                                    //Exception here
                                    zip.AddDirectory(imageLink.DownloadURL);
                                }
                                else
                                {

                                    //Use existing SAS download url

                                    Exception here
                                    zip.AddDirectory(imageLink.DownloadURL);
                                }
                            }

                            zip.Save(output);
                        }
                    }
                }

                return File(output, "application/zip", "sample.zip");
            }
        }

This is what I have come up with so far.

When running this method and adding a file to the zip I get the exception: System.ArgumentException URI format not supported. I assume you can only add local path to the zip which makes sense, so now I'm out of ideas.

This is one of my SAS URLs:

http://mystorage.blob.core.windows.net/images/22201642127PM625F493A7418BA855265516DF8038?sv=2015-04-05&sr=b&sig=%2FdNy6l6qyn7H1fh4D8T1Rd7emzgs%2B4GgQFlPoCOwBW4%3D&st=2016-02-05T09%3A35%3A04Z&se=2016-02-05T10%3A50%3A04Z&sp=r&rscd=Attachment%3B%20f...

It's nothing wrong with the URL, (I can download without zipping).

Am I trying to do the impossible here? Am I forced to download the blobs the normal way like this and after that zip them?

Thanks

Upvotes: 0

Views: 954

Answers (1)

Michael B
Michael B

Reputation: 12228

As far as Azure sees blob storage is just a boundary with some bits and bytes in it. It has no concept of file types, or handlers to do anything with them. (and neither does HTTP)

You are hoping for intelligence in the storage that doesn't exist. The only thing you can do with the URI is to download it,

So yes you need to download it, then zip it then send it wherever its going.

Upvotes: 1

Related Questions