Below is the helper class to connect to the Azure blob storage to read, upload and delete the files from the blob. We required Azure connection string for that storage account and the storage container, and the proper file path should be specified in the parameters.
using Microsoft.Azure;
using Microsoft.WindowsAzure.Storage;
using Microsoft.WindowsAzure.Storage.Blob;
using Microsoft.WindowsAzure.Storage.File;
public class TestFileUploadToBlob
{
#File
#define.delimiterField(',')
CloudStorageAccount storageAccount;
PersonnelIntegrationStorageAccountConnectionString azureStorageKey;
public void initilizedAzureConnection()
{
azureStorageKey = VendParameters::find().StorageConnection;
StorageAccount = Microsoft.WindowsAzure.Storage.CloudStorageAccount::Parse(azureStorageKey);
}
public void moveFileToBlob(System.IO.Stream _memStream, Filename _filename, Str _path)
{
#OCCRetryCount
CloudBlobClient blobClient;
CloudBlobContainer blobContainer;
CloudBlockBlob blockblob;
str containerName, dirPath;
VendParameters vendparmeters = VendParameters::find();
containerName = vendparmeters.PRO_StorageContainer;
dirPath = _path;
azureStorageKey = vendparmeters.StorageConnection;
if(storageAccount && containerName)
{
blobClient = storageAccount.CreateCloudBlobClient();
blobContainer = blobClient.GetContainerReference(containerName);
blockblob = blobContainer.GetBlockBlobReference(dirPath + _filename);
if (blockBlob)
{
if (_memStream)
{
_memStream.Position = 0;
blockBlob.UploadFromStream(_memStream,null,null,null);
blockBlob.FetchAttributes(null, null, null);
}
}
else
{
throw error("error uploading file");
}
}
}
public container getFileFromBlob(str _dirpath)
{
System.IO.MemoryStream memoryStream;
CloudBlobClient cloudBlobClient;
CloudBlobContainer cloudBlobContainer;
CloudBlobDirectory cbDir;
container fileNameCon, memoryStreamCon;
str containerName, dirPath;
VendParameters vendParameters = VendParameters::find();
dirPath = _dirpath;
containerName = vendParameters.StorageContainer;
if (StorageAccount)
{
cloudBlobClient = StorageAccount.CreateCloudBlobClient();
cloudBlobContainer = cloudBlobClient.GetContainerReference(containerName);
//file path where files are stored
cbDir = cloudBlobContainer.GetDirectoryReference(dirPath);
System.Collections.IEnumerable lstbolbEnumarable = cbDir.ListBlobs(false,0,null,null);
System.Collections.IEnumerator lstbolbEnumarator = lstbolbEnumarable.GetEnumerator();
//loop through all the files from azure directory path
while(lstbolbEnumarator.MoveNext())
{
IListBlobItem item = lstbolbEnumarator.Current;
if(item is CloudBlockBlob)
{
CloudBlockBlob blob = item;
container filecon = str2con(blob.Name, '/');
str filName = conPeek(filecon, conLen(filecon));
//stored only .csf file and their file name
//if (strScan(filName, '.csv', 1, strLen(filName)))
//{
memoryStream = new System.IO.MemoryStream();
blob.DownloadToStream(memoryStream, null, null, null);
fileNameCon += [filName];
memoryStreamCon += [memoryStream];
// }
}
}
}
else
{
throw error("error reading file from blob");
}
return [memoryStreamCon, fileNameCon];
}
public void deleteFileFromBlob(Filename _filename, str _reversefilepath)
{
CloudBlobClient cloudBlobClient;
CloudBlobContainer cloudBlobContainer;
str containerName, dirPath;
CloudBlockBlob blockblob;
VendParameters vendParameters = VendParameters::find();
dirPath = _reversefilepath;
containerName = vendParameters.StorageContainer;
if (StorageAccount)
{
cloudBlobClient = StorageAccount.CreateCloudBlobClient();
cloudBlobContainer = cloudBlobClient.GetContainerReference(containerName);
//file path where files are stored
blockblob = cloudBlobContainer.GetBlockBlobReference(dirPath + _filename);
// delete file from blob if it exist
if (blockBlob && blockBlob.Exists(null, null))
{
blockBlob.DeleteIfExistsAsync();
}
}
else
{
throw error("error deleting file from blob");
}
}
}
No comments:
Post a Comment