public async Task <BlobDownloadModel> GetByBlobName(string containerName, string blobName) { try { CloudBlobClient blobClient = _cloudStorageAccount.CreateCloudBlobClient(); CloudBlobContainer container = blobClient.GetContainerReference(containerName); await container.CreateIfNotExistsAsync(); CloudBlockBlob blockBlob = container.GetBlockBlobReference(blobName); var ms = new MemoryStream(); await blockBlob.DownloadToStreamAsync(ms); var blobDownload = new BlobDownloadModel { BlobStream = ms, BlobFileName = blobName, BlobLength = blockBlob.Properties.Length, BlobContentType = blockBlob.Properties.ContentType }; return(blobDownload); } catch (Exception ex) { throw ex; } }
/// <summary> /// Returns a BlobDownloadModel object /// </summary> /// <param name="filename"></param> /// <returns></returns> public async Task <BlobDownloadModel> GetFile(string filename) { if (!string.IsNullOrEmpty(filename)) { var container = BlobHelper.GetRootContainer(); var blob = await container.GetBlobReferenceFromServerAsync(filename); if (blob != null) { var ms = new MemoryStream(); await blob.DownloadToStreamAsync(ms); // Strip off any folder structure to get the fil ename var lastPost = blob.Name.LastIndexOf('/'); var fileName = blob.Name.Substring(lastPost + 1, blob.Name.Length - lastPost - 1); // Build and return the download model with the blob stream and its relevant info var download = new BlobDownloadModel { BlobStream = ms, BlobFileName = fileName, BlobLength = blob.Properties.Length, BlobContentType = blob.Properties.ContentType }; return(download); } } return(null); }
public async Task <BlobDownloadModel> DownloadToStreamAsync(string name, string internalId) { string path = Path.Combine(_directory, internalId); if (!File.Exists(path)) { return(null); } using (Stream sourceStream = File.Open(path, FileMode.Open)) { byte[] result = new byte[sourceStream.Length]; await sourceStream.ReadAsync(result, 0, (int)sourceStream.Length); // Build and return the download model with the blob stream and its relevant info var download = new BlobDownloadModel { BlobStream = new MemoryStream(result), BlobFileName = name, BlobLength = result.Length, BlobContentType = MimeMapping.GetMimeMapping(name) }; return(download); } }
public async Task <BlobDownloadModel> DownloadBlob(String blobName, BlobHelper.Repository repo) { var ipAddress = GetIPAddress(); var isDev = ipAddress.StartsWith("192.168"); if (isDev) { blobName = "dev-" + blobName; } // TODO: You must implement this helper method. It should retrieve blob info // from your database, based on the blobId. The record should contain the // blobName, which you should return as the result of this helper method. if (!String.IsNullOrEmpty(blobName)) { var container = BlobHelper.GetBlobContainer(repo); var blob = container.GetBlockBlobReference(blobName); // Download the blob into a memory stream. Notice that we’re not putting the memory // stream in a using statement. This is because we need the stream to be open for the // API controller in order for the file to actually be downloadable. The closing and // disposing of the stream is handled by the Web API framework. var ms = new MemoryStream(); await blob.DownloadToStreamAsync(ms); // Strip off any folder structure so the file name is just the file name var lastPos = blob.Name.LastIndexOf('/'); var fileName = blob.Name.Substring(lastPos + 1, blob.Name.Length - lastPos - 1); // Build and return the download model with the blob stream and its relevant info var download = new BlobDownloadModel { BlobStream = ms, BlobFileName = fileName, BlobLength = blob.Properties.Length, BlobContentType = blob.Properties.ContentType }; return(download); } // Otherwise return(null); }