0
votes

I am trying to use azure function to zip all files inside a blob container using System.IO.Compression. I could list all files inside the container using the below CloudBlob code

CloudStorageAccount storageAccount = CloudStorageAccount.Parse (storageConn);

CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
CloudBlobContainer container = blobClient.GetContainerReference("<container>");

BlobContinuationToken blobToken = null;
var blobs = await container.ListBlobsSegmentedAsync(blobToken);
var fileList = new List<string>();
var blobpath1 = @"https://<pathtocontainer>/test.zip";

foreach (var blbitem in blobs.Results)
{
     if (blbitem is CloudBlockBlob)
    {
        var blobFileName = blbitem.Uri.Segments.Last().Replace("%20", " ");

        var blobFilePath = blbitem.Uri.AbsolutePath.Replace(blbitem.Container.Uri.AbsolutePath + "/", "").Replace("%20", " ");
        var blobPath = blobFilePath.Replace("/" + blobFileName, "");
        log.LogInformation("blob path : " + blbitem.Uri.ToString()); 
        fileList.Add(blbitem.Uri.ToString());
        string rootpath = @"D:\home\site\wwwroot\ZipandSendFile\temp\";
        string path = rootpath + blobPath;
        log.LogInformation("saving in " + path); 
        //Add to zip
        /*
        CloudBlobContainer container = cloudBlobClient.GetContainerReference("<container>");
        CloudBlockBlob blob = container.GetBlockBlobReference(blobName);
        using (FileStream fs = new FileStream(rootpath, FileMode.Create))
        {
            blob. DownloadToStream(fs);
        }
        */
    }        
}
</code>

After getting each file details inside the blob , I am trying to add them into zip archive using the below System.IO.Compression package

My attempt to download file

<code>

    public static void AddFilesToZip(string zipPath, string[] files,ILogger log)
{
    if (files == null || files.Length == 0)
    {
        return;
    }
    log.LogInformation("Executing add files to zip");
    log.LogInformation(zipPath);


    using (var zipArchive = ZipFile.Open(zipPath, ZipArchiveMode.Update))
    {
        log.LogInformation("in Zip archive");
        foreach (var file in files)
        {
            var fileInfo = new FileInfo(file);
            log.LogInformation(fileInfo.FullName);
            zipArchive.CreateEntryFromFile(fileInfo.FullName,  fileInfo.Name);

        }
    }
}
</code>

But I am getting access denied. Any pointers on this ?

1

1 Answers

0
votes

Resolved issue by logging into kudo cmdshell and cd into directory and change file attribute with attrib +A .