Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Introduction
This article is used for getting memory stream of zip file, uncompress memory stream and load stream into Azure blob.
I am using Ionic.Zip namespace to uncompress the memory stream.I am reading storage credential from app.config
I am using CloudStorageAccount class to configure storage account settings, CloudBlobClient class to reference of
Azure blob and CloudBlobContainer class to reference of Azure blob container.
C# Code to Get file stream of a Azure file share file
public Stream GetFileStream(string connectionstring,string fileshare,string filename)
{
// Parse the connection string and return a reference to the storage account.
CloudStorageAccount storageAccount = CloudStorageAccount.Parse(connectionstring);
// Create a CloudFileClient object for credential access to File storage.
CloudFileClient fileClient = storageAccount.CreateCloudFileClient();
// Get a reference to the file share.
CloudFileShare share = fileClient.GetShareReference(fileshare);
if (share.Exists())
{
// Get a reference to the root directory for the share.
CloudFileDirectory rootDir = share.GetRootDirectoryReference();
foreach (CloudFile file in rootDir.ListFilesAndDirectories())
{
if (file.Name==filename)
{
Stream mem = new MemoryStream();
file.DownloadToStream(mem);
return mem;
}
}
}
return null;
}
Unzipping memory stream of a zip file
public byte[] UnzipFileInMemory(Stream Zipmem)
{
byte[] buffer = new byte[16 * 1024];
Zipmem.Position = 0;
using (MemoryStream ms = new MemoryStream())
{
int read;
while ((read = Zipmem.Read(buffer, 0, buffer.Length)) > 0)
{
ms.Write(buffer, 0, read);
}
using (var outputStream = new MemoryStream())
using (var inputStream = new MemoryStream(ms.ToArray()))
{
using (var zipInputStream = new ZipInputStream(inputStream))
{
zipInputStream.GetNextEntry();
zipInputStream.CopyTo(outputStream);
}
return outputStream.ToArray();
}
}
}
Load memory stream to Azure blob container
public bool UploadByteArrayToBlob(byte[] memdata,string connectionstring ,string blobcontainer,string blobname)
{
CloudStorageAccount storageAccount = CloudStorageAccount.Parse(connectionstring);
CloudBlobClient blobClient = csa_storageAccount.CreateCloudBlobClient();
CloudBlobContainer blobContainer = blobClient.GetContainerReference(blobcontainer);
blobContainer.CreateIfNotExists();
blobContainer.SetPermissions(new BlobContainerPermissions
{
PublicAccess = BlobContainerPublicAccessType.Blob
});
CloudBlockBlob blob = blobContainer.GetBlockBlobReference(blobname);
blob.UploadFromByteArray(memdata, 0, memdata.Length);
return true;
}
Required Namespace to Create Blob Container
using Microsoft.Azure; // Namespace for Azure Configuration Manager
using Microsoft.WindowsAzure.Storage; // Namespace for Storage Client Library
using Microsoft.WindowsAzure.Storage.Blob; // Namespace for Blob storage
using Microsoft.WindowsAzure.Storage.File; // Namespace for File storage
using Microsoft.WindowsAzure.Storage.Auth;
Calling of method
string strconnection = CloudConfigurationManager.GetSetting("StorageConnectionString");
Console.WriteLine("Blob container ready to create ");
Console.WriteLine(CreateBlobContainer(strconnection, "myblobcontainer"));
Points of Interest
This article explain each step to unzip memory stream and load memory stream to Azure blob container
History
No updates available