I was tasked with creating a mod where we need to attach files related to products which were coming from Azure Blob, so I created a C# library class to perform this activity. I attached the DLL as a reference to my project and I am calling each method accordingly as per the requirement.
ConnectionString is nothing but AccountKey which we can get it from Azure Portal where we create the new Blob folder.
Using below code you can Upload,Download, View, Delete files from Azure blob.
using System;
using System.Collections.Generic;
using System.Linq;
//using System.Text;
using System.Threading.Tasks;
using Microsoft.Azure;
using Microsoft.WindowsAzure. Storage;
using Microsoft.WindowsAzure. Storage.Blob;
using System.IO;
//using Microsoft.WindowsAzure. Storage.Auth;
//using Microsoft.WindowsAzure. Storage.Core;
//using Microsoft.Azure.Management. DataFactories.Models;
//using Microsoft.Azure.Management. DataFactories.Runtime;
namespace BlobsStorageAssembly
{
using Dynamics.AX.Application;
public class AccessImageBlob
{
public List<string> ListBlobWithStorageClientLibra ry(String _subFolder, String _product, String _rootFolder, String _connectionString)
{
CloudStorageAccount storageAccount = CloudStorageAccount.Parse(_ connectionString);
CloudBlobClient blobClient = storageAccount. CreateCloudBlobClient();
var backupContainer = blobClient. GetContainerReference(_ rootFolder);
var listOfFileNames = new List<string>();
var list = backupContainer.ListBlobs(_ subFolder + "/" + _product, useFlatBlobListing: true);
foreach (var blob in list)
{
listOfFileNames.Add(blob.Uri. ToString());
}
return listOfFileNames;
}
public void deleteImageFile(String _subFolder, String _product, String _rootFolder, String _connectionString)
{
CloudStorageAccount storageAccount = CloudStorageAccount.Parse(_ connectionString);
CloudBlobClient blobClient = storageAccount. CreateCloudBlobClient();
var backupContainer = blobClient. GetContainerReference(_ rootFolder);
var list = backupContainer.ListBlobs(_ subFolder + "/" + _product, useFlatBlobListing: true);
if (list == null || list.Count() == 0)
{
return;
}
foreach (IListBlobItem blob in list)
{
String fileName = Path.GetFileName(blob.Uri. ToString());
ICloudBlob cloudBlob = backupContainer. GetBlobReferenceFromServer(_ subFolder + "\\" +fileName,null,null,null);
cloudBlob.FetchAttributes();
cloudBlob.DeleteIfExists();
}
}
public void UploadToAzureStorage(System. IO.Stream streams,String _fileNamePath, String _rootFolder, String _subFolder, String _connectionString)
{
Task t = this. UploadToAzureStorageInner( streams, _fileNamePath, _rootFolder, _subFolder, _connectionString);
}
public async Task UploadToAzureStorageInner( System.IO.Stream streams,String _fileNamePath, String _rootFolder, String _subFolder, String _connectionString)
{
string filename;
CloudStorageAccount storageAccount =
CloudStorageAccount.Parse(_ connectionString);
CloudBlobClient blobClient = storageAccount. CreateCloudBlobClient();
var backupContainer = blobClient. GetContainerReference(_ rootFolder);
Uri uri = new Uri(_fileNamePath);
filename = System.IO.Path.GetFileName( uri.LocalPath);
CloudBlockBlob blockBlob = backupContainer. GetBlockBlobReference(_ subFolder + "\\" + filename);
await blockBlob. UploadFromStreamAsync(streams) ;
}
}
}
Marvelous! your blog is outstanding, it really contains rich content which is helpful in microsoft dynamics ax. If you are interested in Microsoft Dynamics Ax Jobs in USA, then please follow the link above.
ReplyDeletevery informative blog and useful article thank you for sharing with us , keep posting learn more
ReplyDeleteAzure Online Training Bangalore
THIS BLOG IS USEFUL TO ME IAM EXPECTING MORE LIKE THIS Azure Training in Chennai | Certification | Azure Online Training Course | Azure Training in Bangalore | Certification | Azure Online Training Course | Azure Training in Hyderabad | Certification | Azure Online Training Course | Azure Training in Pune | Certification | Azure Online Training Course | Azure Training | microsoft azure certification | Azure Online Training Course
ReplyDelete