public async Task <ActionResult> SelectSampleDataSource(MainViewModel main) { main.SelectedDataSourceName = main.SelectedSampleDataSourceName; string fileName = main.SelectedDataSourceName.Split('/').Last(); CloudStorageAccount storageAccount = CloudStorageAccount.Parse(_configuration.GetConnectionString("StorageConnectionString")); // Create a CloudFileClient object for credentialed access to Azure Files. CloudFileClient fileClient = storageAccount.CreateCloudFileClient(); // Get a reference to the file share we created previously. CloudFileShare cloudFileShare = fileClient.GetShareReference("samples"); await cloudFileShare.CreateIfNotExistsAsync(); CloudFileDirectory cloudFileDirectory = cloudFileShare.GetRootDirectoryReference(); CloudFile cloudFile = cloudFileDirectory.GetFileReference(fileName); MemoryStream stream = new MemoryStream(); await cloudFile.DownloadToStreamAsync(stream); stream.Position = 0; StreamReader reader = new StreamReader(stream); main.RawData = reader.ReadToEnd(); //Copied raw data to processed data in case user skips the pre-processing/cleansing step main.ProcessedData = main.RawData; return(PartialView("_DisplayDataSource", main)); }
public static async Task <IActionResult> Run([HttpTrigger(AuthorizationLevel.Function, "post")] PostData data, ILogger log) { log.LogInformation($"C# Blob trigger function Processed blob\n Name:{data.name}\n StorageAccount: {data.storageAccount}\n SourceFileShare: {data.sourceFileShare}\n DestinationFolder: {data.destinationFolder}"); if (data.name.Split('.').Last().ToLower() == "zip") { try{ CloudStorageAccount storageAccount = CloudStorageAccount.Parse(data.storageAccount); CloudFileClient fileClient = storageAccount.CreateCloudFileClient(); CloudFileShare fileShare = fileClient.GetShareReference(data.sourceFileShare); CloudFile zipFile = fileShare.GetRootDirectoryReference().GetFileReference(data.name); MemoryStream blobMemStream = new MemoryStream(); await zipFile.DownloadToStreamAsync(blobMemStream); extract(blobMemStream, log, fileShare, data); } catch (Exception ex) { log.LogInformation($"Error! Something went wrong: {ex.Message}"); return(new ExceptionResult(ex, true)); } } return(new OkObjectResult("Unzip succesfull")); }
public async Task Test_30_SAS() { var share = _client.GetShareReference("photos"); var rootDirectory = share.GetRootDirectoryReference(); var file = rootDirectory.GetFileReference("Joins.png"); // Get a Shared Access Signature for that file that is for read access for that file var sasToken = file.GetSharedAccessSignature( new SharedAccessFilePolicy { Permissions = SharedAccessFilePermissions.Read, SharedAccessExpiryTime = new DateTimeOffset(DateTime.UtcNow.AddMinutes(5)) } ); var sasCreds = new StorageCredentials(sasToken); var sasCloudFile = new CloudFile( new Uri("https://omazurestoragecli.file.core.windows.net/photos/Joins.png"), sasCreds ); bool fileExists = await sasCloudFile.ExistsAsync(); Check.That(fileExists).IsTrue(); using (var ms = new MemoryStream()) { await sasCloudFile.DownloadToStreamAsync(ms); Check.That(ms.Length).IsStrictlyGreaterThan(0); } }
public override async Task <FileData> GetAsync(FileGetOptions fileGetOptions) { FileData file = new FileData(); CloudStorageAccount storageAccount = Authorized(fileGetOptions); CloudFileClient fileClient = storageAccount.CreateCloudFileClient(); CloudFileShare fileshare = fileClient.GetShareReference(fileGetOptions.Folder); if (fileshare.ExistsAsync().Result) { CloudFileDirectory cFileDir = fileshare.GetRootDirectoryReference(); CloudFile cFile = cFileDir.GetFileReference(fileGetOptions.Key); if (cFile.ExistsAsync().Result) { file.Stream.Position = 0; await cFile.DownloadToStreamAsync(file.Stream); } } file.Type = "Azure File Storage"; return(file); }
public async Task <string> GetLogContent() { try { CloudFileDirectory root = Share.GetRootDirectoryReference(); File = root.GetFileReference($"log_{DateTime.Now.ToString("dd_MM_yyyy")}.log"); string content; if (!await File.ExistsAsync()) { content = ""; } else { using (var memoryStream = new MemoryStream()) { await File.DownloadToStreamAsync(memoryStream); content = Encoding.UTF8.GetString(memoryStream.ToArray()); } } return(content); } catch (Exception ex) { throw ex; } }
/// <summary> /// DownloadFileAsyncStream /// </summary> /// <param name="shareName"></param> /// <param name="sourceFolder"></param> /// <param name="fileName"></param> /// <returns></returns> public async Task <MemoryStream> DownloadFileAsyncStream(string shareName, string sourceFolder, string fileName) { CloudStorageAccount storageAccount = CloudStorageAccount.Parse(settings.ConnectionString); CloudFileClient fileClient = storageAccount.CreateCloudFileClient(); CloudFileShare fileShare = fileClient.GetShareReference(shareName); if (await fileShare.ExistsAsync()) { CloudFileDirectory rootDirectory = fileShare.GetRootDirectoryReference(); if (await rootDirectory.ExistsAsync()) { CloudFileDirectory customDirectory = rootDirectory.GetDirectoryReference(sourceFolder); if (await customDirectory.ExistsAsync()) { CloudFile cloudfile = customDirectory.GetFileReference(fileName); if (await cloudfile.ExistsAsync()) { using (var stream = new MemoryStream()) { await cloudfile.DownloadToStreamAsync(stream); return(stream); } } } } } return(null); }
public async Task <Stream> GetFileContentStreamAsync(IFile file) { var cloudFile = new CloudFile(new Uri(file.Id), fileShare.ServiceClient.Credentials); Stream memoryStream = new MemoryStream(); await cloudFile.DownloadToStreamAsync(memoryStream); memoryStream.Seek(0, SeekOrigin.Begin); return(memoryStream); }
public async Task DownloadStream(Stream stream, string name) { CloudFile originalFile = baseDir.GetFileReference(name); if (!await originalFile.ExistsAsync()) { return; } await originalFile.DownloadToStreamAsync(stream); }
/// <summary> /// Gets the file. /// </summary> /// <returns>The file.</returns> /// <param name="type">What type of File is being retrieved</param> /// <param name="guid">The GUID for the File</param> public async Task <Stream> GetFileAsStream(FileType type, string guid) { CloudFileShare fileShare = cloud.CreateCloudFileClient().GetShareReference(type.ToString().ToLower()); CloudFileDirectory root = fileShare.GetRootDirectoryReference(); CloudFile file = root.GetFileReference(guid); Stream memstream = new MemoryStream(); await file.DownloadToStreamAsync(memstream); return(await file.OpenReadAsync()); }
private async Task <FileContent> ParseFileContent(CloudFile cloudFile) { using (var memoryStream = new MemoryStream()) { await cloudFile.DownloadToStreamAsync(memoryStream); return(new FileContent { FileName = cloudFile.Name, Content = memoryStream.ToArray() }); } }
public async Task <MemoryStream> GetFile(string clientId, string filename) { this.Init(); CloudFileShare share = this._cloudFileClient.GetShareReference(this._fileStorageOptions.ShareName); bool shareExist = await share.ExistsAsync(); if (shareExist != true) { throw new ServiceOperationException("No such share."); } //Get root directory CloudFileDirectory rootDirectory = share.GetRootDirectoryReference(); bool rootDirExist = await rootDirectory.ExistsAsync(); if (rootDirExist != true) { throw new ServiceOperationException("No such root dir."); } //Get clients directory CloudFileDirectory clientsFolder = rootDirectory.GetDirectoryReference(clientId); bool clientsDirExist = await clientsFolder.ExistsAsync(); if (clientsDirExist != true) { throw new ServiceOperationException("No such client dir."); } //Get reference to file //If file already exists it will be overwritten CloudFile file = clientsFolder.GetFileReference(filename); bool fileExists = await file.ExistsAsync(); if (fileExists != true) { throw new ServiceOperationException("No such file"); } MemoryStream ms = new MemoryStream(); await file.DownloadToStreamAsync(ms); ms.Position = 0; return(ms); }
private async Task <Stream> CopyCloudFileWithStreamOutputAsync(CloudFile source, CloudFile destination) { MemoryStream downloadStream = new MemoryStream(); await source.DownloadToStreamAsync(downloadStream).ConfigureAwait(false); downloadStream.Position = 0; using (MemoryStream upload = new MemoryStream()) { downloadStream.CopyTo(upload); upload.Position = 0; await destination.UploadFromStreamAsync(upload).ConfigureAwait(false); } downloadStream.Position = 0; return(downloadStream); }
public async Task <byte[]> DownloadFile(string shareName, string folder, string fileName) { byte[] toReturn = null; try { CloudFile cloudFile = await this.GetFileReference(shareName, folder, fileName); MemoryStream ms = new MemoryStream(); await cloudFile.DownloadToStreamAsync(ms); toReturn = ms.ToArray(); } catch (Exception ex) { this.ErrorMessage = ex.ToString(); } return(toReturn); }
public async Task <FileResult> GetPdf(string fileName) { CloudStorageAccount storageAccount = CloudStorageAccount.Parse(CloudConfigurationManager.GetSetting("StorageConnectionString")); // Create a CloudFileClient object for credentialed access to Azure Files. CloudFileClient fileClient = storageAccount.CreateCloudFileClient(); // Get a reference to the file share we created previously. CloudFileShare share = fileClient.GetShareReference("ankerhpdf"); // Ensure that the share exists. if (share.Exists()) { // Get a reference to the root directory for the share. CloudFileDirectory rootDir = share.GetRootDirectoryReference(); // Get a reference to the directory we created previously. CloudFileDirectory sampleDir = rootDir.GetDirectoryReference("Faktura"); // Ensure that the directory exists. if (sampleDir.Exists()) { // Get a reference to the file we created previously. CloudFile file = sampleDir.GetFileReference(fileName); // Ensure that the file exists. if (file.Exists()) { MemoryStream ms = new MemoryStream(); await file.DownloadToStreamAsync(ms); ms.Seek(0, SeekOrigin.Begin); return(File(ms, "application/pdf")); } } } return(null); }
/// <summary> /// Downloads a file from the Microsoft Azure File service by calling <see cref="CloudFile.DownloadToStreamAsync(Stream, AccessCondition, FileRequestOptions, OperationContext, CancellationToken)"/>. /// </summary> /// <param name="cancellationToken">The token used to signal cancellation request.</param> public override async Task RunAsync(CancellationToken cancellationToken) { await _cloudFile.DownloadToStreamAsync(Stream.Null, cancellationToken); }
/// <summary> /// Download data to the stream. /// </summary> /// <param name="file">The cloud file.</param> /// <param name="stream">The stream to write to.</param> /// <param name="cancellationToken">The cancellation token.</param> /// <returns>The async task.</returns> public async Task DownloadAsync(CloudFile file, Stream stream, CancellationToken cancellationToken) { await file.DownloadToStreamAsync(stream, cancellationToken); }
public async Task <Stream> ReadFileAsync(string share, string filename, Stream stream, CancellationToken token = default(CancellationToken)) { if (string.IsNullOrEmpty(share)) { throw new ArgumentNullException("share"); } if (string.IsNullOrEmpty("filename")) { throw new ArgumentNullException("filename"); } if (stream == null) { throw new ArgumentNullException("stream"); } Exception error = null; Stopwatch watch = new Stopwatch(); watch.Start(); double time = watch.Elapsed.TotalMilliseconds; long bytesTransferred = 0; try { CloudFileShare choudShare = client.GetShareReference(share); CloudFileDirectory dir = choudShare.GetRootDirectoryReference(); CloudFile file = dir.GetFileReference(filename); IProgress <StorageProgress> progressHandler = new Progress <StorageProgress>( progress => { bytesTransferred = bytesTransferred < progress.BytesTransferred ? progress.BytesTransferred : bytesTransferred; if (watch.Elapsed.TotalMilliseconds > time + 1000.0 && bytesTransferred <= progress.BytesTransferred) { OnDownloadBytesTransferred?.Invoke(this, new BytesTransferredEventArgs(share, filename, bytesTransferred, file.Properties.Length)); } }); await file.DownloadToStreamAsync(stream, default(AccessCondition), default(FileRequestOptions), default(OperationContext), progressHandler, token); } catch (Exception ex) { if (ex.InnerException is TaskCanceledException) { stream = null; } else { error = ex; throw ex; } } finally { watch.Stop(); OnDownloadCompleted?.Invoke(this, new BlobCompleteEventArgs(share, filename, token.IsCancellationRequested, error)); } return(stream); }
/// <summary> /// Download data to the stream. /// </summary> /// <param name="file">The cloud file.</param> /// <param name="stream">The stream to write to.</param> /// <returns>The async task.</returns> public async Task DownloadAsync(CloudFile file, Stream stream) { await file.DownloadToStreamAsync(stream); }
public static void DownloadToStream(this CloudFile file, Stream target, AccessCondition accessCondition = null, FileRequestOptions options = null, OperationContext operationContext = null) { file.DownloadToStreamAsync(target, accessCondition, options, operationContext).GetAwaiter().GetResult(); }