Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating a zip file in situ within azure blob storage

I have files stored in one container within a blob storage account. I need to create a zip file in a second container containing the files from the first container.

I have a solution that works using a worker role and DotNetZip but because the zip file could end up being 1GB in size I am concerned that doing all the work in-process, using MemoryStream objects etc. is not the best way of doing this. My biggest concern is that of memory usage and freeing up resources given that this process could happen several times a day.

Below is some very stripped down code showing the basic process in the worker role:

using (ZipFile zipFile = new ZipFile())
{
    foreach (var uri in uriCollection)
    {
        var blob = new CloudBlob(uri);

        byte[] fileBytes = blob.DownloadByteArray();

        using (var fileStream = new MemoryStream(fileBytes))
        {
            fileStream.Seek(0, SeekOrigin.Begin);

            byte[] bytes = CryptoHelp.EncryptAsBytes(fileStream, "password", null);

            zipFile.AddEntry("entry name", bytes);
        }
    }

    using (var zipStream = new MemoryStream())
    {
        zipFile.Save(zipStream);
        zipStream.Seek(0, SeekOrigin.Begin);

        var blobRef = ContainerDirectory.GetBlobReference("output uri");
        blobRef.UploadFromStream(zipStream);
    }

}

Can someone suggest a better approach please?

like image 692
Digbyswift Avatar asked Dec 21 '11 09:12

Digbyswift


People also ask

Can I upload ZIP file to azure blob storage?

Just prepend the folder path to the blob name. For example, if you want to upload abc. zip in xyz/pqr folder, just change the name of the blob to xyz/pqr/abc.

Can I store files in azure blob storage?

Blob Storage is designed for: Serving images or documents directly to a browser. Storing files for distributed access. Streaming video and audio.

Can I create folder in blob container?

You may create mappings between groups of artifacts and the virtual directory structure for them to be put under in the blob storage using the "::" notation. As an example you can follow the following steps to understand how to use this feature. Create a folder inside another one with one file inside each one.


2 Answers

At the time of writing this question, I was unaware of the LocalStorage options available in Azure. I was able to write files individually to this and the work with them within the LocalStorage and then write them back to blob storage.

like image 154
Digbyswift Avatar answered Sep 22 '22 01:09

Digbyswift


If all you are worried aobut is your memorysteam taking up too much memory then what you can do is implement your own stream and as your stream is being read, you you add your zip files to the stream and remove already read files from the stream. This will keep your memory stream size to the size of one file.

like image 36
RyanFishman Avatar answered Sep 25 '22 01:09

RyanFishman