/// <summary> /// Comprueba si un fichero existe en la ruta seleccionada /// </summary> /// <param name="filePath">ruta completa al fichero</param> /// <returns></returns> public async Task <bool> CheckFileExistsAsync(string filePath) { var azureFile = AzureFilePath.FromFilePath(filePath); var containerService = blobServiceClient.GetBlobContainerClient(azureFile.Container); return(await containerService.GetBlobClient(azureFile.FileName).ExistsAsync()); }
/// <summary> /// Escribe un fichero en la ruta especfificada /// </summary> /// <param name="filePath">Ruta completa al fichero junto al nombre del archivo y su extensión</param> /// <param name="content">Contenido del fichero</param> public async Task WriteFileAsync(string filePath, byte[] content) { var azureFile = AzureFilePath.FromFilePath(filePath); var containerService = blobServiceClient.GetBlobContainerClient(azureFile.Container); BlobClient blobClient = containerService.GetBlobClient(azureFile.FileName); using (MemoryStream stream = new MemoryStream(content)) { stream.Position = 0; await blobClient.UploadAsync(stream, true); } }
private async Task <T> AccessToFileAsync <T>(string filePath, Func <CloudFile, Task <T> > delFunction) { var azureFile = AzureFilePath.FromFilePath(filePath); // Get a reference to the file share we created previously. CloudFileShare share = fileClient.GetShareReference(azureFile.ShareReference); // Ensure that the share exists. if (true || await share.ExistsAsync().ConfigureAwait(false)) //Obviamos esta comprobación porque puede que no se tenga privilegios suficientes { // Get a reference to the root directory for the share. CloudFileDirectory rootDir = share.GetRootDirectoryReference(); // Get a reference to the directory we created previously. if (azureFile.Folders.Any()) { CloudFileDirectory sampleDir = rootDir.GetDirectoryReference(azureFile.Folders[0]); if (!sampleDir.Exists()) { throw new Exception("Incorrect route path."); } for (int i = 1; i < azureFile.Folders.Count; i++) { sampleDir = sampleDir.GetDirectoryReference(azureFile.Folders[i]); if (!sampleDir.Exists()) { throw new Exception("Incorrect route path."); } } CloudFile file = sampleDir.GetFileReference(azureFile.FileName); // Ensure that the file exists. return(await delFunction(file).ConfigureAwait(false)); } else { CloudFile file = rootDir.GetFileReference(azureFile.FileName); // Ensure that the file exists. return(await delFunction(file).ConfigureAwait(false)); } } else { throw new Exception("Share not found."); } }
/// <summary> /// Obtiene el contenido de un fichero como un array de bytes /// </summary> /// <param name="filePath">Ruta completa al fichero</param> /// <returns></returns> public async Task <byte[]> GetFileContentAsync(string filePath) { var azureFile = AzureFilePath.FromFilePath(filePath); var containerService = blobServiceClient.GetBlobContainerClient(azureFile.Container); BlobClient blobClient = containerService.GetBlobClient(azureFile.FileName); if (!await blobClient.ExistsAsync()) { throw new FileNotFoundException(); } using (MemoryStream stream = new MemoryStream()) { await blobClient.DownloadToAsync(stream); return(stream.ToArray()); } }
/// <summary> /// Obtiene el contenido de un fichero como una cadena de texto en UFT8 /// </summary> /// <param name="filePath">Ruta completa al fichero</param> /// <returns></returns> public async Task <string> GetFileContentStringAsync(string filePath) { var azureFile = AzureFilePath.FromFilePath(filePath); var containerService = blobServiceClient.GetBlobContainerClient(azureFile.Container); BlobClient blobClient = containerService.GetBlobClient(azureFile.FileName); if (!await blobClient.ExistsAsync()) { throw new FileNotFoundException(); } using (MemoryStream stream = new MemoryStream()) { await blobClient.DownloadToAsync(stream); stream.Position = 0; using (StreamReader reader = new StreamReader(stream, Encoding.UTF8)) { return(reader.ReadToEnd()); } } }