Created
November 16, 2020 06:46
-
-
Save Dhaneshmonds/9db68fadfa5868e1bfdf8a46cbac8e46 to your computer and use it in GitHub Desktop.
Upload and Read Azure Blob Storage MVC Web API2
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Microsoft.WindowsAzure.Storage; | |
using Microsoft.WindowsAzure.Storage.Blob; | |
using System; | |
using System.IO; | |
using System.Security.Cryptography; | |
namespace AzureBlobStorage.Helpers | |
{ | |
public class BlobFileStore | |
{ | |
private CloudStorageAccount _storageAccount; | |
private CloudBlobClient _blobClient; | |
private CloudBlobContainer _blobContainer; | |
private static MD5CryptoServiceProvider MD5CryptoService = new MD5CryptoServiceProvider(); | |
public BlobFileStore(string connectionString, string containerName) | |
{ | |
_storageAccount = CloudStorageAccount.Parse(connectionString); | |
_blobClient = _storageAccount.CreateCloudBlobClient(); | |
_blobContainer = _blobClient.GetContainerReference(containerName); | |
if (_blobContainer.CreateIfNotExists()) | |
{ | |
_blobContainer.SetPermissionsAsync(new BlobContainerPermissions { PublicAccess = BlobContainerPublicAccessType.Blob }); | |
} | |
} | |
public string StoreStream(string fileName, byte[] fileStream) | |
{ | |
if (string.IsNullOrWhiteSpace(fileName)) | |
{ | |
throw new ApplicationException("Attempt to store document with no name"); | |
} | |
MemoryStream stream = new MemoryStream(fileStream); | |
bool uploadBlob = true; | |
var blob = _blobContainer.GetBlockBlobReference(fileName); | |
if (blob.ExistsAsync().GetAwaiter().GetResult()) | |
{ | |
var streamHash = Convert.ToBase64String(MD5CryptoService.ComputeHash(fileStream)); | |
if (streamHash == blob.Properties.ContentMD5) | |
{ | |
uploadBlob = false; | |
} | |
} | |
if (uploadBlob) | |
{ | |
stream.Position = 0; | |
blob.UploadFromStreamAsync(stream).GetAwaiter().GetResult(); | |
} | |
return blob.Uri.ToString(); | |
} | |
public Stream GetStream(string docName) | |
{ | |
var blob = _blobContainer.GetBlockBlobReference(docName); | |
if (blob.ExistsAsync().GetAwaiter().GetResult()) | |
{ | |
try | |
{ | |
var ms = new MemoryStream(); | |
blob.DownloadToStreamAsync(ms).GetAwaiter().GetResult(); | |
return ms; | |
} | |
catch (Exception ex) | |
{ | |
return null; | |
} | |
} | |
else | |
{ | |
return null; | |
} | |
} | |
public CloudBlockBlob GetBlob(string blobName) | |
{ | |
return _blobContainer.GetBlockBlobReference(blobName); | |
} | |
public void DeleteBlob(string docName) | |
{ | |
CloudBlockBlob _blockBlob = GetBlob(docName); | |
_blockBlob.DeleteIfExistsAsync(); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using AzureBlobStorage.Helpers; | |
using AzureBlobStorage.Models; | |
using System.Configuration; | |
using System.Linq; | |
using System.Web.Http; | |
namespace AzureBlobStorage.Controllers | |
{ | |
[Authorize] | |
public class MyDataController : ApiController | |
{ | |
[HttpPost, Route("api/MyData/add")] | |
public dynamic PostMyData([FromBody] MyData objMyDataModel) | |
{ | |
//AttachmentContent is byte[] | |
if(objMyDataModel.AttachmentContent != null) | |
{ | |
var _cloudConnectionString = ConfigurationManager.AppSettings["ConnectionString"]; | |
var _container = ConfigurationManager.AppSettings["ContainerName"]; | |
var ingestFileStore = new BlobFileStore(_cloudConnectionString, _container); | |
var imgResult = ingestFileStore.StoreStream(objMyDataModel.AttachmentName, objMyDataModel.AttachmentContent); | |
objMyDataModel.SharepointPath = imgResult; | |
if(imgResult.Length>0) | |
{ | |
//Save to Database | |
} | |
} | |
return true; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment